Reading a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahulcodes
    New Member
    • Jul 2006
    • 2

    #1

    Reading a text file

    This is a input file

    DEL 0050 XYZ 0050 0310 CCU 0710
    MAA 1325 TRZ 10415
    KWI 0050 FJR 0325 0325 CCJ
    KWI 0050 FJR 0325 0425 CCJ 0925 1005

    i.e maximum items(column entries) in a row can be 8
    but their data type will be in same order
    i.e char int char int int char int int

    i am simply trying to read it
    here's my code


    #include<stdio. h>
    #include<string .h>
    main()
    {
    FILE *fp;
    struct air
    {
    char a[50];
    int b;
    char c[50];
    int d;
    int e;
    char f[50];
    int g;
    int h;
    };
    struct air ia;

    fp=fopen("SC.tx t","rb");
    if(fp==NULL)
    {
    puts("Cannot open file");
    exit(0);
    }

    while(fscanf (fp,"%s %d %s %d %d %s %d %d\n",ia.a,&ia. b,ia.c,&ia.d,&i a.e,ia.f,&ia.g, &ia.h) != EOF)
    {
    printf("%s %d %s %d %d %s %d %d\n",ia.a,ia.b ,ia.c,ia.d,ia.e ,ia.f,ia.g,ia.h );
    }

    fclose(fp);

    }

    But the output contains garbage value...
    it is reads the1st line till 7th column enrty but after that it prints garbage value
    similarly it reads the 2nd line till 4th entry but after that it prints garbage value
    but in last line it is showing the entire line...
    please help me...i need to read each element in file cuz i have to use and compare these characters with some value.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Don't use fscanf.

    You have told it to read 8 values but some of the lines do not contain 8 values. Then you try to output all 8 values whether or not 8 values have been read so on the lines with less than 8 values you get the garbage that was already in the variable in question as there was nothing to read from the file. Additionally fscanf is fraught with buffer overrun dangers, especially when reading a string.

    Use fgets (and don't bother opening the file in binary mode, text mode should be OK i.e. "r" not "rb"). This reads the line into a string but makes sure you can not overwrite the end of the string.

    Then use a pointer(or 2) and the function strtol to scan down the line reading in the data and counting the number of data points at the same time. Once you know how many data points are on the line set default values for the data points not present and only output what you have actually read.

    Comment

    • Ashish_CPP
      New Member
      • Jul 2006
      • 35

      #3
      Originally posted by rahulcodes
      This is a input file

      DEL 0050 XYZ 0050 0310 CCU 0710
      MAA 1325 TRZ 10415
      KWI 0050 FJR 0325 0325 CCJ
      KWI 0050 FJR 0325 0425 CCJ 0925 1005

      i.e maximum items(column entries) in a row can be 8
      but their data type will be in same order
      i.e char int char int int char int int

      i am simply trying to read it
      here's my code


      #include<stdio. h>
      #include<string .h>
      main()
      {
      FILE *fp;
      struct air
      {
      char a[50];
      int b;
      char c[50];
      int d;
      int e;
      char f[50];
      int g;
      int h;
      };
      struct air ia;

      fp=fopen("SC.tx t","rb");
      if(fp==NULL)
      {
      puts("Cannot open file");
      exit(0);
      }

      while(fscanf (fp,"%s %d %s %d %d %s %d %d\n",ia.a,&ia. b,ia.c,&ia.d,&i a.e,ia.f,&ia.g, &ia.h) != EOF)
      {
      printf("%s %d %s %d %d %s %d %d\n",ia.a,ia.b ,ia.c,ia.d,ia.e ,ia.f,ia.g,ia.h );
      }

      fclose(fp);

      }

      But the output contains garbage value...
      it is reads the1st line till 7th column enrty but after that it prints garbage value
      similarly it reads the 2nd line till 4th entry but after that it prints garbage value
      but in last line it is showing the entire line...
      please help me...i need to read each element in file cuz i have to use and compare these characters with some value.

      Hi,
      The garbage values are getting printed becoz of the reason what Bafna has mentioned. To avoid this u can try fgetc( ); function in a while loop and fetch the characters & store it in an array until u receive the ascii value of the keyboard key 'ENTER'. The ENTER key value will indicate that the string has come to an end. As u recv the ENTER value, append null in the array.

      Comment

      • rahulcodes
        New Member
        • Jul 2006
        • 2

        #4
        Thanks alot to all you guys...

        The problem just got solved...

        so once again thanks aton to all you who helped me out...and to those you read my query/problem.

        Bye

        Comment

        • mynameis
          New Member
          • Aug 2006
          • 1

          #5
          Hi all,

          I had written a code to read text file and I can already run it. Now, I want to expand my code so that, after reading that file, it can insert the contents of the file to the table that had been created in MySQL. Hope anyone can help me on how to do it..
          Thanks

          p/s: I'm using C

          Comment

          Working...