beginner array help

This is a discussion on beginner array help within the C Programming forums, part of the General Programming Boards category; Hi all. I am trying to find a way to read a file which has 10 lines and 4 columns ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    beginner array help

    Hi all. I am trying to find a way to read a file which has 10 lines and 4 columns of numbers and assign it to my arrays. The file itself is opening ok but for some reason it only reads the last line.
    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<stdlib.h>
    
    int main( void )
    {
    
    	int number[10] = {0};
    	int value1[10] = {0};
    	int value2[10] = {0};
    	int value3[10] = {0};
    
    
    	FILE* readThis;
    	if ((readThis = fopen ("file.txt", "r"
    		)) ==NULL)  {
    			printf("\nError opening file \n");
    			exit(101);
    	}
    
    	while (!feof(readThis))  {
    		fscanf( readThis, "%d %d %d %d", &number, &value1, &value2, &value3 );
    
    	}
    		fclose(readThis);
    
    		printf("\n\n%d\n\n%d\n\n%d\n\n%d\n\n", number[0], value1[0], value2[0], value3[0]);//This prints the last line of the file
    		printf("\n\n%d\n\n%d\n\n%d\n\n%d\n\n", number[1], value1[1], value2[1], value3[1]);//This prints all zeros
    
    
    
    		return 0;
    	}
    any help would be appreciated thanks.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to iterate the data into your arrays:

    &value1[i], etc., in your while loop

    Just before the while loop, set i = 0, then last line in the while loop, increment i by 1.

    You're going through the whole file, but you're overwriting all but the very last part of the data.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Pro-tip: use a 2 dimensional array (1x4).
    Last edited by abraham2119; 04-22-2009 at 09:42 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,639
    Quote Originally Posted by abraham2119
    use a 4 dimensional array.
    I think that you are suggesting the use of an array of arrays of size 4, but I think that it would be better to use an array of struct objects.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by laserlight View Post
    I think that you are suggesting the use of an array of arrays of size 4
    Yes, my mistake.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Quote Originally Posted by Adak View Post
    You need to iterate the data into your arrays:

    &value1[i], etc., in your while loop

    Just before the while loop, set i = 0, then last line in the while loop, increment i by 1.

    You're going through the whole file, but you're overwriting all but the very last part of the data.
    thanks, it works perfect now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array with input? please help..beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 01:16 AM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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