fgets() is ignoring the first letter of line from text file. Works when a space is added

Viewed 81

I wrote a program to rearrange a text a file. I used fegts() to copy the data line by line into a buffer. Everything worked, except it keeps omitting the first char of line 4 from the following text file

 O     0.580533    -1.111576     1.827241
 N     0.791283    -0.030547     2.529271
 O     0.938920    -0.043192     3.722388
Eu     0.387757     0.171327    -0.654089
 N    -2.454117     0.553935    -1.831953
 O    -3.555931     0.702353    -2.289337
 O     0.859520     1.403791     1.815420
 O     2.535343     1.144752    -2.164160
 N     2.927953    -0.016122    -2.615146
 O     3.864559    -0.147779    -3.357376

I used the fgets() like this,

FILE *fs=fopen(argv[1],"r");
FILE *ft=fopen(argv[2],"w");

//few conditions here so that the file is read after the second line

char buf[256], colma[3], colmb[23], colmc[23], colmd[23];
if (fgets(buf, sizeof(buf), fs) == 0)
   break;
sscanf(buf, "%s %s %s %s", colma, colmb, colmc, colmd);

fprintf(ft, "%12s %10s %10s %15s\n",colmb, colmc, colmd, colma);

The expected output is

    0.580533  -1.111576   1.827241               O
    0.791283  -0.030547   2.529271               N
    0.938920  -0.043192   3.722388               O
    0.387757   0.171327  -0.654089              Eu
   -2.454117   0.553935  -1.831953               N
   -3.555931   0.702353  -2.289337               O
    0.859520   1.403791   1.815420               O
    2.535343   1.144752  -2.164160               O
    2.927953  -0.016122  -2.615146               N
    3.864559  -0.147779  -3.357376               O

The output I get is

    0.580533  -1.111576   1.827241               O
    0.791283  -0.030547   2.529271               N
    0.938920  -0.043192   3.722388               O
    0.387757   0.171327  -0.654089               u
   -2.454117   0.553935  -1.831953               N
   -3.555931   0.702353  -2.289337               O
    0.859520   1.403791   1.815420               O
    2.535343   1.144752  -2.164160               O
    2.927953  -0.016122  -2.615146               N
    3.864559  -0.147779  -3.357376               O

This issue is rectified by adding an additional space to the fourth line of the my source file. The edited file looks like this

 O     0.580533    -1.111576     1.827241
 N     0.791283    -0.030547     2.529271
 O     0.938920    -0.043192     3.722388
 Eu     0.387757     0.171327    -0.654089
 N    -2.454117     0.553935    -1.831953
 O    -3.555931     0.702353    -2.289337
 O     0.859520     1.403791     1.815420
 O     2.535343     1.144752    -2.164160
 N     2.927953    -0.016122    -2.615146
 O     3.864559    -0.147779    -3.357376

It also works if I add a space to each line of the file using sed -i -e 's/^/ /' <file>

Can anyone point out why my program isn't reading that first letter?

Edit1: The looping is done this way,


int ch=0,count=0;
ch=fgetc(fs);
while(ch!=EOF)
    {
        if(ch=='\n')
        {
            count++;
            printf("%d\n",count);
        }
        if (count>1)
        {
            char buf[256], ele[256], xcoord[256], ycoord[256], zcoord[256];
            if (fgets(buf, sizeof(buf), fs) == 0)
                break;
            sscanf(buf, "%s %s %s %s", ele, xcoord, ycoord, zcoord);

            fprintf(ft, "%12s %10s %10s %15s\n",xcoord, ycoord, zcoord, ele);
    }
    ch=fgetc(fs);

ch is initialized as an int because it wasn't compiling when initialized as a char

2 Answers

When fgets fails to read a line, it will exit the loop.

int ch=0,count=0;
char buf[256], ele[256], xcoord[256], ycoord[256], zcoord[256];
while (fgets(buf, sizeof(buf), fs)) {
    if (4 == sscanf(buf, "%s %s %s %s", ele, xcoord, ycoord, zcoord)) {
        fprintf(ft, "%12s %10s %10s %15s\n",xcoord, ycoord, zcoord, ele);
    }
    ++count;
}

regarding the two instances of:

ch=fgetc(fs);   

This is consuming the first character of each line.

Related