I am working on a program that reads data from a file and then I save that data in a char *fname
variable.
I must use the method read()
. I am able to read the data but I do not know how to skip the space to read the next chunk.
My file:
hello world i am C 1100011 555 000 4554 4554545 4545454.
My code:
void FileWork(char * fname) {
size_t nbytes;
ssize_t bytes_read;
int fd = open(fname, O_RDONLY, 0666);
if(fd == -1) {
perror("File Error\n");
exit(1);
}
int count =8; // tetting
while(count != 0) {
printf("\nCount = %d\n",count);
bytes_read = read(fd,data,8);
for(int i = 0; i < sizeof(data); i++) {
printf("%c",data[i]);
}
count --;
}
}
Output:
Count = 8
01010010
Count = 7
0110111
Count = 6
0 111 1
Count = 5
11 10010
Count = 4
11 10010
Count = 3
11 10010
Count = 2
11 10010
Count = 1
11 10010
Any help will be very welcomed. Thanks!