Read text file, scan and store data into variables?

This is a discussion on Read text file, scan and store data into variables? within the C Programming forums, part of the General Programming Boards category; I have created my program, however we have just been introduced to text files. I can get my code to ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    Question Read text file, scan and store data into variables?

    I have created my program, however we have just been introduced to text files. I can get my code to read the file however, I have tried everything to get it to scan the file and store the data into variables, and still have no luck.

    My text files resembles the following...
    4 6000
    3 9
    5 60
    7.00 800.00
    2.00 6000.00

    Code:
    	char  t1[6], t2[6], t3[6], t4[6], t5[6], t7[6], t8[6], t9[6], t10[6];
    	FILE *tp;
    
    	tp = fopen("report.txt", "rt");
    	if (tp == NULL)
    	{
    		printf("Error opening source file\n");
    		perror("Error opening report.txt");
    		getchar();
    	}
    	else
    	{
    		while (fscanf(tp, "%d%d%d%d%d%s%lf%lf%lf%lf", &t1, &t2, &t3, &t4, &t5, &t6, &t7, &t8, &t9, &t10) != EOF)
    		{
    			printf("%d", t1); // wanted to see if it would print '4' my first number in text file.
    		
    		
    			fclose(tp);
    		}
    	}
    Last edited by wisdom30; 04-18-2011 at 02:36 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    Quote Originally Posted by wisdom30 View Post
    Code:
    	char  t1[6], t2[6], t3[6], t4[6], t5[6], t7[6], t8[6], t9[6], t10[6];
    ...
    		while (fscanf(tp, "%d%d%d%d%d%s%lf%lf%lf%lf", &t1, &t2, &t3, &t4, &t5, &t6, &t7, &t8, &t9, &t10) != EOF)
    Why are you scanning into char arrays? Read your scanf docs. The format specifiers tell you what type of data to expect, and what type of parameters you need to store them in. You start with 5 %d's, so you need 5 int's. Then one %s, that takes a char array. Then 4 %lf's, which is 4 doubles. Fix the types of t1, t2, t3, t4 and t5 to be ints, and t7, t8, t9 and t10 to be doubles. t6 can stay a char array, but you need to make sure it's a sufficient size (remember, 1 extra char for the trailing '\0'!).

    EDIT: t6 can stay a char array, but you need to drop the & from that one only. The name of an array is the address of the first element, which is what fscanf wants for the %s param.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Code:
    	int t1[6], t2[6], t3[6], t4[6], t5[6],t6[6]; 
    	double t7[6], t8[6], t9[6], t10[6];
    	FILE *tp;
    
    	tp = fopen("report.txt", "rt");
    	if (tp == NULL)
    	{
    		printf("Error opening source file\n");
    		perror("Error opening report.txt");
    		getchar();
    	}
    	else
    	{
    		while (fscanf(fp, "%d%d%d%d%d%d%lf%lf%lf%lf", t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) != EOF)
    		{
    			printf("%d", t1);
    
    			fclose(tp);
    		}
    	}
    I made the neccesary changes but still not printing the first number correctly in the file? What am I missing?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You are defining your t values as arrays then you're reading them as single values.
    I'm thinking you need to make up your mind here...

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Why not just use integers instead a bunch of arrays...

    like:

    int t1, t2, t3, t4, t5, t6;
    double t7, t8, t9, t10;

    This will solve your printing problem. Right now when you print, you are tell it to print array t1 but you aren't failing to reference the index of the array to print.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by DuckCowMooQuack View Post
    Why not just use integers instead a bunch of arrays...

    like:

    int t1, t2, t3, t4, t5, t6;
    double t7, t8, t9, t10;

    This will solve your printing problem. Right now when you print, you are tell it to print array t1 but you aren't failing to reference the index of the array to print.
    That's going to depend on the file contents... He might be trying to read lines of numbers instead of single variables.
    Either way, he needs to make a decision and adjust his methods accordingly.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    What do you mean I am reading them as single values? Please correct me if I am wrong, but when I use the fscanf function, isn't the data being scanned stored in my t array(s)? Is this what I am missing? That the first character in the array for t1[6] my number 4 - the first number in my text file?

    Code:
     
    printf("%d", t1[1]);

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No scanf does not read entire arrays... scanf("%d"... reads a single integer value. if you want to read an array of 6 values you need to either create a loop or use more than one %d in your formatting string and specify the array indices...
    Code:
    // read a 3 element array
    scanf("%d%d%d",array[0],array[1],array[2]);
    
    // read with loop
    for (i = 0; i <3; i++)
      scanf("%d",array[i];
    C can't read arrays in one cut, because it doesn't know how big they are.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Figured it out! I got rid of the arrays and stored everything into variables! However even after I did that it still wasn't printing what was in the text file, so I went to check my text file and low and behold! The text file had a bunch of numbers I never put in there. A friend of mine said something to the effect of me needing to clear what was stored? Nonetheless I cleared the mess and reentered the numbers and my program worked!!!

    Thanks guys! I was a bit timid posting on this site, but you guys helped greatly!

    Oh... I am a 'she'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read text file and store it in an array
    By look2hook in forum C Programming
    Replies: 2
    Last Post: 12-03-2010, 10:47 PM
  2. read and store text file as an array
    By abotaha in forum C++ Programming
    Replies: 1
    Last Post: 08-02-2010, 08:57 PM
  3. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  4. Read variables and stuff from a text file
    By ChrisJ in forum C++ Programming
    Replies: 1
    Last Post: 06-26-2003, 11:06 AM
  5. Replies: 5
    Last Post: 02-09-2003, 09:03 AM

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