我正在尝试使用原始 I/O 函数从文件中读取数据并将数据输出到另一个文件,但是,我的代码似乎无法工作,我发现 read() 无法终止。但是,我不知道在这种情况下如何终止循环,我的代码是这样的:
int main(){
int infile; //input file
int outfile; //output file
infile = open("1.txt", O_RDONLY, S_IRUSR);
if(infile == -1){
return 1; //error
}
outfile = open("2.txt", O_CREAT | ORDWR, S_IRUSR | S_IWUSR);
if(outfile == -1){
return 1; //error
}
int intch; //character raed from input file
unsigned char ch; //char to a byte
while(intch != EOF){ //it seems that the loop cannot terminate, my opinion
read(infile, &intch, sizeof(unsigned char));
ch = (unsigned char) intch; //Convert
write(outfile, &ch, sizeof(unsigned char));
}
close(infile);
close(outfile);
return 0; //success
}
有人可以帮我解决这个问题吗?十分感谢