Like Tree1Likes
  • 1 Post By AndiPersti

Reading in comma separated numbers from a data file

This is a discussion on Reading in comma separated numbers from a data file within the C Programming forums, part of the General Programming Boards category; Hello C programming gurus! I am attempting to read in a file that has 4128 sets of 21 numbers separated ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    Reading in comma separated numbers from a data file

    Hello C programming gurus! I am attempting to read in a file that has 4128 sets of 21 numbers separated by commas and write it into an array. I now know that in order to use fseek, I have to make my array a character array, but I need my function to read in decimals (ex: 0.172635). I'm reading in 0.102598,0.000000,0.000000,0.000000,0.000000,0.307 793,0.000000,0.410391,0.102598,0.000000,0.102598,0 .102598,0.000000,0.000000,0.102598,0.102598,0.8207 83,0.000000,0.000000,0.000000,0.000000 and keep getting numbers like 48 49 50...etc. here is my code, what am I doing wrong?
    Code:
    void CSread(char filename[100], char array[22], char array2[22], unsigned int arraysize)
    {
     char genename[32];
     double temp = 0;
     
     FILE *CSfile;
     CSfile = fopen(filename, "r");
     
     if ( CSfile == NULL ) 
     {
      perror ( "Unable to open file" );
      exit ( EXIT_FAILURE );
     }
     
     fseek(CSfile, 0, SEEK_SET);
     fgets(array, 21, CSfile); 
     printf("The values of the first array are: ");
     for (int i=0; i < 22; i++)
     {
      printf("%d ",array[i]);
     }
     
     printf("\n");
     
     fseek(CSfile, 21, SEEK_SET);
     fgets(array2, 21, CSfile);
     printf("The values of the second array are: ");
     for (int j=0; j < 22; j++)
     {
      printf("%d ",array2[j]);
     }
     printf("\n");

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    It looks like you're printing your array of character elements as if they were integers. I'm surprised you're not segfaulting. You should use %c as your format element if you want to print characters.
    Last edited by AAnderson; 04-18-2013 at 08:14 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    fgets(array, 21, CSfile);
    This line will read 20 characters from the file. fgets() doesn't know anything about comma seperated values.

    Quote Originally Posted by littlemslexii View Post
    I now know that in order to use fseek, I have to make my array a character array
    ???
    fseek() has nothing to do with arrays.

    It looks like you are lacking some fundamentals. Is this for homework?

    Bye, Andreas
    Salem likes this.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    Quote Originally Posted by AndiPersti View Post
    Code:
    fgets(array, 21, CSfile);
    This line will read 20 characters from the file. fgets() doesn't know anything about comma seperated values.


    ???
    fseek() has nothing to do with arrays.

    It looks like you are lacking some fundamentals. Is this for homework?

    Bye, Andreas
    I am definitely lacking some fundamentals. No, it's not for homework. I just need a code to do something specific for my research. I've been trying to feel my way through with a book and some websites, but many times the lingo is hard to follow. I just need to know how to read a file in with 6 digit decimals separated by commas. All the forums keep telling me to use fgets(), but it reads in numbers like 48. fseek was a last resort but it was working as far as printing the numbers into the array.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,376
    Have a read: fscanf - C++ Reference

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,287
    Either use fscanf() or read in the entire file into memory and use memchr() to first search for newlines to find the end of lines, then use memchr() to find tabs or commas. You could also use strtok() to search for multiple delimeters.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    Thank you all! Just to clarify, I was using fseek to force the function to read from a specific position in the data file. I am trying to take the dot product between neighboring genes and need to make a sort of sliding window. I have changed it to fscanf(), but my function is still not reading in the decimal numbers. Instead of 0.2 I'm getting a large number (526041265228644680000000000000000000000.000000) and then 0's for the rest of my values. I don't understand why it's not reading the values from my data file. Here is the updated code:
    Code:
    void CSread(char filename[100], float array[22], float array2[22], unsigned int arraysize, int n, int m)
    {
     char genename[32];
     float temp = 0;
     
     FILE *CSfile;
     CSfile = fopen(filename, "r");
     
     if ( CSfile == NULL ) 
     {
      perror ( "Unable to open file" );
      exit ( EXIT_FAILURE );
     }
     
     fscanf(CSfile,"%f%*c", array);
     
     for (int i=0; i<22; i++)
     {
      printf("Array 1 = ");
      printf("%f ", array[i]);
      printf("\n");
     } 
     fclose(CSfile);
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You want to read in numbers like this:
    0.102598,0.000000,0.000000,0.000000,0.000000,0.307 793,0.000000,0.410391,0.102598,0.000000,0.102598,0 .102598,0.000000,0.000000,0.102598,0.102598,0.8207 83,0.000000,0.000000,0.000000,0.000000

    Stop using fseek(). sscanf() has an operator for what you want, and could be used like this:

    Use fgets() to take in one full row of numbers at a time
    use sscanf() to pick out the numbers from the buffer that holds the entire row of numbers. (using [^,], as your format specifier).

    So how many numbers do you have per row? 21?

    How are the sets delimited?

    Can you post just a small sample in the format in the same format?

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    fscanf(CSfile,"%f%*c", array);
    This reads just the first value from the file.
    You need a loop to read in all values.

    How does your file look like? Can you post some lines?

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 11-27-2012, 04:45 AM
  2. homework help wanted: reading a comma seperated file
    By DHart07 in forum C++ Programming
    Replies: 8
    Last Post: 11-17-2010, 11:54 AM
  3. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  4. Reading a comma separated file
    By nwr in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 01:26 PM

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