以下为测试样例cpp文件,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <iostream>
using namespace std;
int main() { char c; int in, out,nread; int addint = 0; in = open("file.in",O_RDONLY); //cout << in; out = open("file.out",O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); while((nread=read(in,&c,1))>-2 && addint != 10 ) { cout<<"number of read:"<<nread<<"; "; cout<<"and the character is :"<<c<<endl; addint++; //write(out,&c,1); } exit(0);
}
其中file.in的文件内容为
编译运行:
经过测试发现,此程序能完整读出文件内容,而不是循环只读出同一个字符,说明读指针会自动后移 |