C i/o

This is a discussion on C i/o within the C Programming forums, part of the General Programming Boards category; Hi, all I am writing to you to seek some help about a small C I/O problem. I have a ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    C i/o

    Hi, all

    I am writing to you to seek some help about a small C I/O problem.

    I have a simple code like this:



    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    
    int main ()
    {
       FILE * pFile, * wFile;
       int i, index;
       int LINESZ = 1024; 
       double tempElementNum, tempx, tempy, tempz; 
       char AtomType[10], buff[1024]; 
    
    
       pFile = fopen ("001.txt","r");
       wFile = fopen ("002.txt","w");
    
      if(pFile==NULL) {
        printf("Error: can't open file.\n");
        return 1;
      }
      else {
        printf("File opened successfully.\n");
    
        i = 0 ;   
    
        while (fgets (buff, LINESZ, pFile)) {
            /* Process buff here. */
        
          sscanf(buff,"%d %s %lf %lf %lf",&index,&AtomType,&tempElementNum,&tempx,&tempy,&tempz);
          fprintf(wFile," %-18s %-18.8lf %-18.8lf %-18.8lf \n",AtomType, tempx, tempy, tempz);
          i++;
          printf("File opened successfully time = %d\n", i);
        }
      }
    
        fclose (pFile); 
        fclose (wFile); 
      return 0;
    }

    I intend to convert 001.txt

    1 O 8.0000 -1.62667717 -0.43210228 1.44938569
    2 O 8.0000 1.88432283 -0.07010228 1.44938569
    3 O 8.0000 -0.05267717 -2.00610228 -1.30361431
    4 O 8.0000 0.30932283 1.50489772 -1.30361431

    To 002.txt like this

    O -1.62667717 -0.43210228 1.44938569
    O 1.88432283 -0.07010228 1.44938569
    O -0.05267717 -2.00610228 -1.30361431
    O 0.30932283 1.50489772 -1.30361431


    But my code seems not working properly. Could you help me identify the problems I have.

    Thank you very much for your help.

    Best regards

  2. #2
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    107
    One %lf in sscanf() is missing.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    Quote Originally Posted by DRK View Post
    One %lf in sscanf() is missing.

    thank you so much,

    my problem solved.

    thx

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21