#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Bristol
    Posts
    13
    Rep Power
    0

    Reading in a tab delimited file.


    Hey again.

    Right, this time I'm trying to read in data in four columns from a .txt file. All the values are showing up but like this:

    value1, 0.00000, 0.00000, 0.00000,
    value2, 0.00000, 0.00000, 0.00000,
    value3, 0.00000, 0.00000, 0.00000,
    value4, 0.00000, 0.00000, 0.00000,
    value5, 0.00000, 0.00000, 0.00000,
    value6, 0.00000, 0.00000, 0.00000,
    value7, 0.00000, 0.00000, 0.00000,
    value8, 0.00000, 0.00000, 0.00000,
    etc

    whereas I want it to read

    value1, value2, value3, value4,
    value5, value6, value7, value8,
    etc

    It's in the correct columns in the text file and we cannot edit the text file.

    Here's the coding:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        FILE *file_ptr;
        float x,y,sigmax,sigmay, myarray[10000][4];
        int i;
    
        file_ptr=fopen("C:\\Data\\LinearTestData.txt","r"); //open file from computer
        if (file_ptr == NULL) printf("Error: File not found.\n");
        else
        {
            for (i=0; i<10000; i++)
            {
                myarray[i][0]=0;
                myarray[i][1]=0;
                myarray[i][2]=0;
                myarray[i][3]=0;
            }
    
            for (i=0;!feof(file_ptr);i++)
            {
    
            fscanf(file_ptr,"%f,%f,%f,%f",&x,&y,&sigmax,&sigmay);
    
            printf("%f,%f,%f,%f\n",x,y,sigmax,sigmay);
    
            myarray[i][0]=x;
            myarray[i][1]=y;
            myarray[i][2]=sigmax;
            myarray[i][3]=sigmay;
            }
    
        fclose(file_ptr);
    
        }
    
        return 0;
    }
    Thank you!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,363
    Rep Power
    1870
    > Reading in a tab delimited file.
    ...
    > fscanf(file_ptr,"%f,%f,%f,%f",&x,&y,&sigmax,&sigmay);

    You need to be more precise about how your file is formatted.
    Is it tabs, spaces or commas?

    Your code says commas at the moment.

    If it really is tabs, then use "%f %f %f %f"

    Also, your loop is broken for reading the file.
    Try
    Code:
    for ( i = 0 ; 
        i < 10000 && fscanf(file_ptr,"%f %f %f %f",&x,&y,&sigmax,&sigmay) == 4 ;
        i++ ) {
        // do your thing
    }
    Or perhaps better in the long run
    Code:
    char buff[BUFSIZ];
    i = 0;
    while ( i < 10000 && fgets( buff, BUFSIZ, file_ptr ) != NULL ) {
      if ( sscanf( buff, "%f %f %f %f",&x,&y,&sigmax,&sigmay) == 4 ) {
        // do your thing
        i++;
      } else {
        // complain about a badly formatted line
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Bristol
    Posts
    13
    Rep Power
    0
    Originally Posted by salem
    > Reading in a tab delimited file.
    ...
    > fscanf(file_ptr,"%f,%f,%f,%f",&x,&y,&sigmax,&sigmay);

    You need to be more precise about how your file is formatted.
    Is it tabs, spaces or commas?

    Your code says commas at the moment.

    If it really is tabs, then use "%f %f %f %f"

    Also, your loop is broken for reading the file.
    Try
    Code:
    for ( i = 0 ; 
        i < 10000 && fscanf(file_ptr,"%f %f %f %f",&x,&y,&sigmax,&sigmay) == 4 ;
        i++ ) {
        // do your thing
    }
    Or perhaps better in the long run
    Code:
    char buff[BUFSIZ];
    i = 0;
    while ( i < 10000 && fgets( buff, BUFSIZ, file_ptr ) != NULL ) {
      if ( sscanf( buff, "%f %f %f %f",&x,&y,&sigmax,&sigmay) == 4 ) {
        // do your thing
        i++;
      } else {
        // complain about a badly formatted line
      }
    }
    Sorted it out, just needed to remove the commas. Thank you.

IMN logo majestic logo threadwatch logo seochat tools logo