I'm trying to process some files. The file paths are stored in a txt file. There are 4 filenames per line, each separated with a space. I want to read a line into a buffer, divide the buffer into those 4 filepaths and process them afterwards. When I'm done, I want to do the same thing for the filepaths from the next line.

I have the following code to process the filenames:

```
int i, j, check, fileidx, filenum;
char buf[100000], *testfile;
char filename_out[500], filename_mess[500], filename_ref[500], filename_dark[500];

filenum = getFileNumber("filelist.txt"); //get number of lines in the txt file

filelist = fopen("filelist.txt","r");

for (fileidx=0; fileidx<filenum; fileidx++){
    memset(&filename_mess[0], 0, sizeof(filename_mess)); //clear filenames
    memset(&filename_dark[0], 0, sizeof(filename_dark));
    memset(&filename_ref[0], 0, sizeof(filename_ref));
    memset(&filename_out[0], 0, sizeof(filename_out));
    memset(&buf[0],0,sizeof(buf));

    for (check = 0;check<fileidx+1;check++){  //up to the current file index
        testfile = fgets(buf,100000,filelist); //get each line into the buffer
            if ((testfile == NULL)&(check == fileidx)){ //if at the correct file index and line is empty
                printf("Error in filelist (line %i is empty!)",check+1);
                goto fileerror;
            };
    };

    if (fileidx==filenum-1)
            sprintf(buf,"%s\0",buf); //desperate try - not helping

    if ((buf[0] == '\n') | (buf[0] == ' ') | (buf[0] == EOF)){
        printf("Error in filelist (line %i)!",fileidx+1);
        goto fileerror;
    };

    i=0;
    while (buf[i]!=' '){ //while not hitting the first space
        if (buf[i] == '\n'){ //if a filename is missing, the end of the line is found too early
            printf("Error in filelist (line %i)! Element missing!",fileidx+1);
            goto fileerror; //goto the next file
        };
        filename_mess[i] = buf[i]; //copy the buffer to the first filename array
        i++;
    };
    i++; //skip the space between the filepaths
    j = i; //save start of second filepath
    while (buf[i]!=' '){ //repeat the process for second filename
        if (buf[i] == '\n'){
            printf("Error in filelist (line %i)! Element missing!",fileidx+1);
            goto fileerror;
        };
        filename_ref[i-j] = buf[i];
        i++;
    };
    i++;
    j = i;
    while (buf[i]!=' '){ //repeat process for third filename
        if (buf[i] == '\n'){
            printf("Error in filelist (line %i)! Element missing!",fileidx+1);
            goto fileerror;
        };
        filename_dark[i-j] = buf[i];
        i++;
    };
    i++;
    j = i;
    while ((buf[i]!='\n') & (buf[i]!= EOF) & (buf[i]!=' ')){ //we're at the last element, check for end of line or EOF here, too
        if (buf[i] == ' '){
            printf("Error in filelist (line %i)! Too many elements!",fileidx+1);
            goto fileerror;
        };
        filename_out[i-j] = buf[i];
        i++;
    };

//read the files, process them

};

```

This code works perfectly fine for all the lines except for the last one. In the last line, only the last element is stored in "filename\_out", the others (mess,ref and dark) remain empty. The buffer "buf" looks fine, tho (checking it with `printf("%s",buf)` shows the expected line). When I add an empty line below my last line in the text file, the reading works fine. I also tried to use `sprintf(buf,"%s\n",buf)` to add this (seemingly necessary) character to the buffer each time - didnt work.

To clarify: reading a txt file containing

```
R1E1 R1E2 R1E3 R1E4
R2E1 R2E2 R2E3 R2E4

```

Will give me:

```
File1:
filename_mess = R1E1
filename_ref = R1E2
filename_dark = R1E3
filename_out = R1E4

File2:
filename_mess = 
filename_ref = 
filename_dark = 
filename_out = R2E4

```

While it should be:

```
File1:
filename_mess = R1E1
filename_ref = R1E2
filename_dark = R1E3
filename_out = R1E4

File2:
filename_mess = R2E1
filename_ref = R2E2
filename_dark = R2E3
filename_out = R2E4

```

Adding a blank line to the end of the text file will return:

```
File1:
filename_mess = R1E1
filename_ref = R1E2
filename_dark = R1E3
filename_out = R1E4

File2:
filename_mess = R2E1
filename_ref = R2E2
filename_dark = R2E3
filename_out = R2E4

File3:
*Error Message*

```

Edit: removed some errors.