0

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!

DeiDei
  • 10,205
  • 6
  • 55
  • 80
TheRonGuy
  • 1
  • 2
  • 2
    I've roughly reformatted your code to make it humanly readable. Please, write pretty code :) – DeiDei Feb 09 '20 at 02:46
  • The code you posted does not produce the output you posted. Please provide [A Minimal, Complete, and Verifiable Example (MCVE)](http://stackoverflow.com/help/mcve). – David C. Rankin Feb 09 '20 at 08:53
  • [https://stackoverflow.com/questions/33968157/how-to-read-characters-from-a-file-until-a-space-and-store-that-as-one-string-c](https://stackoverflow.com/questions/33968157/how-to-read-characters-from-a-file-until-a-space-and-store-that-as-one-string-c) – DevStranding Feb 10 '20 at 09:32

0 Answers0