0

I am trying to change the text format of a text file using C. Following is the code that I have tried. The problem with the output is that the code skips every second line of the text file.

Following is the file I am trying to format.

Protein
26709
    1MET      N    1  1.826  3.097  3.556 -0.457  0.365 -0.163
    1MET      H    2  1.769  3.168  3.512 -0.606 -0.305 -1.071
    1MET     H2    3  1.781  3.006  3.565  0.271  0.041  0.264
    1MET     H3    4  1.857  3.125  3.649  1.251 -2.295  0.144
    1MET     CA    5  1.948  3.077  3.475 -0.538 -0.282 -0.098
    1MET     HA    6  1.987  2.977  3.504 -2.137 -0.912 -0.071
    1MET     CB    7  1.914  3.075  3.321  0.389 -0.161 -0.089
    1MET    HB2    8  1.848  2.987  3.306 -0.909  0.569  1.297
    1MET    HB3    9  1.869  3.176  3.311 -1.707 -1.073 -0.224
    1MET     CG   10  2.034  3.050  3.220  0.171  0.344 -0.271

And I get the output as :

Protein

26709

   1MET       H     2   1.769   3.168   3.512  -0.606  -0.305  -1.071
   1MET      H3     4   1.857   3.125   3.649   1.251  -2.295   0.144
   1MET      HA     6   1.987   2.977   3.504  -2.137  -0.912  -0.071
   1MET     HB2     8   1.848   2.987   3.306  -0.909   0.569   1.297
   1MET      CG    10   2.034   3.050   3.220   0.171   0.344  -0.271
   1MET     HG3    12   2.017   2.974   3.140   2.388   1.184  -1.570
   1MET      CE    14   2.245   3.113   3.036   0.088  -0.370   0.744
   1MET     HE2    16   2.311   3.190   2.990  -0.737  -0.490  -0.665
   1MET       C    18   2.043   3.195   3.504  -0.052  -0.237  -0.714

Following is the code that I have tried :

#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(int argc, char *argv[])
{
    FILE *fp;
    char buff[200],res[10],atm[10],atmNum[10],x[10],y[10],z[10],vx[10],vy[10],vz[10];
    if (argc<2){
        printf("SYTEM ABORT ! \n Provide the input GRO file");
        exit(1);
    }
    else
        fp=fopen(argv[1],"r");
    while(fgets(buff,200,fp)!=NULL){
        if (strlen(buff) > 10) {
            fscanf(fp,"%s %s %s %s %s %s %s %s %s",res,atm,atmNum,x,y,z,vx,vy,vz);
            if (strcmp(atm,"Na")){
                printf("%7s %7s %5s %7s %7s %7s %7s %7s %7s",res,atm,atmNum,x,y,z,vx,vy,vz);
            }
            else
                printf("%8s %7s %5s %7s %7s %7s %7s %7s %7s",res,atm,atmNum,x,y,z,vx,vy,vz);
        }
        else
            printf("%s",buff);

    }
    fclose(fp);
    return 0;
}

As it can be seen from the output file the first line is skipped, I guess that is due to the usage of fgets in while loop. But after that it prints every alternate lines.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
PythonNoob
  • 139
  • 2
  • 7
  • 3
    In the positive (length > 10) case of your `if`-block, I'm pretty sure you should be using `sscanf` and the buffer you just pulled with `fgets` as the data source rather than than throwing it out and using `fscanf` to get the next line. Frankly you should also be checking the result of your `scanf` to ensure all the parameters you're about to use were actually populated. Don't assume anything; assumption is the mother of all... – WhozCraig Jul 16 '16 at 04:42
  • @WhozCraig Thanks a lot, that was an easy fix. – PythonNoob Jul 16 '16 at 04:46

1 Answers1

3

But you use fgets() and then fscanf() here:

while(fgets(buff,200,fp)!=NULL)  
  if (strlen(buff) > 10) {
    fscanf(fp,"%s %s %s %s %s %s %s %s %s",res,atm,atmNum,x,y,z,vx,vy,vz);

so you will read one line with fgets() and you will read the next line with fscanf, resulting in skipping the line, as you say.


As WhozCraig said, you should use sscanf(), instead of fscanf().

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Yes, thats true but I didn't know that it will also skip every alternate line. But yes it runs in a while loop. That makes sense. Thanks :) – PythonNoob Jul 16 '16 at 04:48
  • You are welcome @PythonNoob, you made a good question, that's why we were able to help. ;) – gsamaras Jul 16 '16 at 04:55