1

I'm learning C and as a practice, I'm trying read a file with different sets of numbers in each line, and print (or save to a different file) each number per line (also as string).

You can see the code here:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 40

int main(){

    char numbers[SIZE];
    FILE *fileRead;
    FILE *fileWrite;
    char *token, *tofree, *str;

    fileRead = fopen("/Users/USER/Documents/readfile.txt", "r");
    if (fileRead==NULL){
        printf("File not found\n");
        return 1;
    }

    fileWrite = fopen("/Users/USER/Documents/writefile.txt", "w");
    if (fileWrite==NULL){
        printf("File not found\n");
        return 1;
    }

    while (!feof(fileRead)){
        fgets(numbers, SIZE, fileRead);
        tofree = str = strdup(numbers);
        while ((token = strsep(&str, " \n"))) fprintf(fileWrite,"%s\n", token);
        free(tofree);

    }
    puts("Succeed");
    return 0;
}

When I remove the '\n' character at printing/writing, all the numbers are printed one after the other. I expected that. The problem is that when I include the \n char, the output includes some empty lines. I include an example of the input and the output files:

Input:

43 19 01 23 05 28 35
41 32 20 19 28 34 07
25 02 36 04 18 23 43
15 41 45 36 32 26 11
13 32 23 09 14 12 36
18 43 03 24 08 13 20
23 12 21 03 27 36 17
09 01 23 17 22 20 33
11 01 18 19 14 02
30 16 39 44 32 03
36 40 25 41 05 44
02 20 06 08 41 43
13
15

Output:

43
19
01
23
05
28
35

41
32
20
19
28
34
07

25
02
36
04
18
23
43

15
41
45
36
32
26
11

13
32
23
09
14
12
36

18
43
03
24
08
13
20

23
12
21
03
27
36
17

09
01
23
17
22
20
33

11
01
18
19
14
02

As you can see, the output includes an empt line when the original file had a newline, but if I remove the \n, it prints/writes just one number after other without space:

4319012305283541322019283407250236041823431541453632261113322309141236184303240813202312210327361709012317222033110118191402301639443203364025410544022006084143210219201733320528113826140803412735352236132117381233133230312645364405180322300236422806112726081307442523112145041827394426382243380510372504360104250602164226284117350617433404412518161910373624201317450344241202013435071104422329080106090526224243142223331215143012373820100331390117334234164401454327114125153130103225420930240137274114261206431112330135024321053027451435420809430807370327140127112337412506030824420320113615214544383436282305210401363805453529341319224405160121111525171338310322033738183632421824451732341905182302233945312133194425134030260524

What character is being inserted if \n is an explicit delimiter?

Nooblhu
  • 552
  • 15
  • 33
  • 1
    Just a guess, but if there's a space at the end of a line, you'll get an empty token between the `' '` and the `'\n'` characters. That's how `strsep` works. If you want to skip both, use `strtok` or `strtok_r`, which skip all delimiters before providing the next token. –  May 05 '18 at 02:24
  • 1
    Note that you can print the string length after the string to verify this. If it's anything other than 0, then you might consider dumping each byte of the problematic string in a hexadecimal format (`%02x`) to see what the byte values are. –  May 05 '18 at 02:31
  • You are right @Chrono, strtok and strtok_r do the trick. I actually used strsep() because I read in many answers that strtok had problems. Many recommended using strsep instead, after your comment, I read about strtok_r. I'm still grasping how it works the third argument. [This answer](https://stackoverflow.com/questions/2227198/segmentation-fault-when-using-strtok-r) helped me a lot for that. – Nooblhu May 05 '18 at 09:19

0 Answers0