#include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <fcntl.h> #include<pthread.h> #include <semaphore.h> #define infile "infile.dat" #define outfile "outfile.dat" sem_t f1; sem_t f2; void inputdata() { int infd; char buf[50]; if ((infd = open(infile, O_WRONLY | O_TRUNC | O_CREAT, 10700)) == -1) { printf("ERROR, OPEN READ FILE FAILED:%s \n", sys_errlist[errno]); exit(255); } printf("Please enter the data:\n"); printf("(Ctrl+D -- quit)\n"); while(fgets(buf, sizeof(buf), stdin)) { if (write(infd, buf, strlen(buf)) != strlen(buf)) { printf("ERROR, WRITE FILE FAILED:%s \n", sys_errlist[errno]); exit(255); } } close(infd); sem_post(&f1); } void copydata() { sem_wait(&f1); int infd, outfd; char buf[50]; if ((infd = open(infile, O_RDONLY))==-1) { printf("ERROR, OPEN READ FILE FAILED:%s \n", sys_errlist[errno]); exit(255); } if ((outfd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, 10700))==-1) { printf("ERROR, OPEN WRITE FILE FAILED:%s \n", sys_errlist[errno]); exit(255); } int count; while((count = read(infd, buf, sizeof(buf))) > 0) { if (count != write(outfd, buf, count)) { printf("ERROR, WRITE FILE FAILED:%s \n", sys_errlist[errno]); exit(255); } } close(infd); close(outfd); sem_post(&f2); } void showdata() { sem_wait(&f2); int outfd; char buf[50]; if ((outfd = open(outfile, O_RDONLY, 10700))==-1) { printf("ERROR, OPEN WRITE FILE FAILED:%s \n", sys_errlist[errno]); exit(255); } int count; printf("\nThe data in the file:\n"); while((count = read(outfd, buf, sizeof(buf))) > 0) { printf("%s", buf); } close(outfd); } void main() { pthread_t t1,t2,t3,t4; sem_init(&f1,0,0); sem_init(&f2,0,0); pthread_create(&t1,NULL, (void *)showdata, NULL); pthread_create(&t2,NULL, (void *)copydata, NULL); pthread_create(&t3,NULL, (void *)inputdata, NULL); pthread_join(t1,NULL); }
本人想问一下,本人觉得这段程序已经没有问题了,但是最后一步读文件时,会出现上面标记的乱码,这是为什么?
解决方案
20
#include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <fcntl.h> #include <pthread.h> #include <semaphore.h> #define infile "infile.dat" #define outfile "outfile.dat" sem_t f1; sem_t f2; void inputdata() { int infd; char buf[50]; if ((infd = open(infile, O_WRONLY | O_TRUNC | O_CREAT, 10700)) == -1) { printf("ERROR, OPEN READ FILE FAILED:%s \n", sys_errlist[errno]); //printf("ERROR, OPEN READ FILE FAILED:%s \n", strerror(errno)); exit(255); } printf("Please enter the data:\n"); printf("(Ctrl+D -- quit)\n"); while(fgets(buf, sizeof(buf), stdin)) { if (write(infd, buf, strlen(buf)) != strlen(buf)) { printf("ERROR, WRITE FILE FAILED:%s \n", sys_errlist[errno]); //printf("ERROR, WRITE FILE FAILED:%s \n", strerror(errno)); exit(255); } } close(infd); sem_post(&f1); } void copydata() { sem_wait(&f1); int infd, outfd; char buf[50]; if ((infd = open(infile, O_RDONLY))==-1) { printf("ERROR, OPEN READ FILE FAILED:%s \n", sys_errlist[errno]); //printf("ERROR, OPEN READ FILE FAILED:%s \n", strerror(errno)); exit(255); } if ((outfd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, 10700))==-1) { printf("ERROR, OPEN WRITE FILE FAILED:%s \n", sys_errlist[errno]); //printf("ERROR, OPEN WRITE FILE FAILED:%s \n", strerror(errno)); exit(255); } int count; while((count = read(infd, buf, sizeof(buf))) > 0) { if (count != write(outfd, buf, count)) { printf("ERROR, WRITE FILE FAILED:%s \n", sys_errlist[errno]); //printf("ERROR, WRITE FILE FAILED:%s \n", strerror(errno)); exit(255); } } close(infd); close(outfd); sem_post(&f2); } void showdata() { sem_wait(&f2); int outfd; char buf[50]; if ((outfd = open(outfile, O_RDONLY, 10700))==-1) { printf("ERROR, OPEN WRITE FILE FAILED:%s \n", sys_errlist[errno]); //printf("ERROR, OPEN WRITE FILE FAILED:%s \n", strerror(errno)); exit(255); } int count; printf("\nThe data in the file:\n"); memset(buf, 0, sizeof(buf)); /*初始化缓存为0*/ while((count = read(outfd, buf, sizeof(buf) - 1)) > 0) /*防止读到的数据是50个字符,最后一个字符用于字符串结束标记 "\0" */ { printf("count = %d\n", count); printf("%s", buf); memset(buf, 0, sizeof(buf)); /*初始化缓存为0*/ } close(outfd); } int main(void) { pthread_t t1,t2,t3,t4; sem_init(&f1,0,0); sem_init(&f2,0,0); pthread_create(&t1,NULL, (void *)showdata, NULL); pthread_create(&t2,NULL, (void *)copydata, NULL); pthread_create(&t3,NULL, (void *)inputdata, NULL); pthread_join(t1,NULL); return 0; }
出现乱码,是字符串没有找到”\0″,即字符串结束标记;需要对缓存在读之前做初始化操作(初始化为0, 0 值等于”\0″),另一个方面,若文件中的字符大于缓存长度,所以每次读取都最大读取缓存长度减1。