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 10:42 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02:16 AM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 01:56 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  4. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 01:20 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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