Thread: Reading data from a text file

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    13

    Reading data from a text file

    I've got a text file that has 2 columns and 35 rows of numbers. I am trying to read in the text file and output the 2 columns and 35 rows. With my code I am only getting the last two pieces of data from the file and it's printing the same data 35 times. Any thoughts?

    Code:
    #include <stdio.h>
    int main()
    {
    int i,j;
    float x,y;
    FILE *fp;
     fp=fopen("Data.txt", "rb");
    for(j=0; j<35; j++)
     for(i=0; i<2; i++)
     fscanf(fp,"%f, %f", &x, &y);
    for(j=0; j<35; j++)
     for(i=0; i<2; i++)
      
      printf("%f  %f\n", x, y);
    fclose(fp);
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    x holds 1 value.
    y holes 1 value.

    This is what you are doing:
    set x to 1
    set y to 1
    set x to 4
    set y to 11
    ...
    set x to 78
    set y to 128

    print x, which was last set to 78, 35 times.
    print y, which was last set to 128, 35 times.

    Sure, it's not those exact numbers, but that's the process you have. You can't simply declare a single variable of a given type and expect it to hold an infinite series of data.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    13
    I had this earlier, but it wasn't working either. Is it any closer? I appreciate your help. I'm still learning.

    Code:
    #include <stdio.h>
    int main()
    {
    int i,j;
    double data[2][35];
    FILE *fp;
     fp=fopen("Data.txt", "r");
    for(j=0; j<35; j++)
     for(i=0; i<2; i++)
     fscanf(fp,"%lf, %lf", &data[i][j]);
    for(j=0; j<35; j++)
     for(i=0; i<2; i++)
      
      printf("%f  %f\n",data[i][j]);
    fclose(fp);
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Does it give you the output you expect? Why are you using %lf for input but %f for output?

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    13
    It looks like the output is correct for the first column, but the second column is a REALY long number and it just repeats itself on each row. I changed the output to %lf and it still gives me the wrong answer for the second row.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    13
    It just doesn't seem to be reading the numbers in the second column properly. I've tried editing the text file with a space between the numbers on each row and with a comma in between. It seems to like have a comma better.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for(j=0; j<35; j++)
     for(i=0; i<2; i++)
     fscanf(fp,"%lf, %lf", &data[i][j]);
    Ok, make up your mind here as to what you are actually doing. 35 rows. Got it, that's fine. 2 columns, or four columns? Why are you reading 2 numbers at a time?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    13
    It should just be two columns. I'm reading two numbers at a time becuase that's what I thought I needed to get each column. Here's a sample of what the text file looks like.

    2.05,293.21
    1.60,299.16
    1.37,182.14
    0.76,163.41
    0.99,200.43
    0.53,86.39
    1.14,225.11

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can read one at a time twice, or two at a time once. But don't do two at a time twice, because what's two times two? Four. That means you've just read four numbers when you should have only read two.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    13
    Ok, I think I've almost got it. The only problem is that it is outputting the last row of numbers from the text file twice. Here's my revised code.

    Code:
    #include <stdio.h>
    int main()
    {
    int i, n, gotnum;
    FILE *fp;
    float col1[35];
    float col2[35];
    fp=fopen("Data.txt", "r");
     i=0;
     do
     {
      gotnum=0;
      gotnum=fscanf(fp,"%f %f", &col1[i], &col2[i]);
      if(gotnum>1);
      printf("%10.2f  %10.2f\n", col1[i], col2[i]);
     
     }
     while(gotnum>1);
      
    fclose(fp);
    return 0;
     }
    Here's what the last part of the output is doing:

    2.51 367.25
    2.13 345.55
    1.90 268.52
    2.36 422.57
    2.36 422.57

  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    13
    Actually got it! Here's what I ended up with.

    Code:
    #include <stdio.h>
    int main()
    {
    int i;
    FILE *fp;
    float col1[35];
    float col2[35];
    fp=fopen("Data.txt", "r");
     for(i=0; i<35; i++)
      fscanf(fp,"%f %f", &col1[i], &col2[i]);
     for(i=0; i<35; i++)
      printf("%10.2f  %10.2f\n", col1[i], col2[i]);
      
    fclose(fp);
    return 0;
    }
    Thanks for the pointers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading data from text file
    By torquemada in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2012, 09:28 PM
  2. Reading data from a text file into a C struct
    By someone2088 in forum C Programming
    Replies: 11
    Last Post: 12-07-2011, 10:14 AM
  3. Reading data from Text File
    By DarrinTE in forum C Programming
    Replies: 8
    Last Post: 03-25-2011, 03:11 PM
  4. Reading data from a text file
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2008, 02:30 PM
  5. Reading in data from Text file
    By fortune2k in forum C Programming
    Replies: 214
    Last Post: 04-10-2008, 11:12 AM