Thread: reading file and storing to arrays

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    Question reading file and storing to arrays

    Hi,

    I am trying to read a file and store its content into 2 seperate arrays (one char** the other a int array) - the content of the int array is modified and then rewritten to the file (overwritting file).

    sample text file:

    aaaa 304
    bbbb 120
    ccccc 441
    dddd 5
    eeee 61

    Code:
    #include <stdio.h>
    
    void main()
    {
    	FILE *pFile;
    	int x=0;
    	int num[5][100];
    	char arr[5][100];
    	int buff[100];
    
    	pFile = fopen ("log.txt", "r");
    
    	for (x=0; x<5; x++)
    	{
    		fscanf (pFile, arr[x]);
    		fscanf (pFile, num[x]);
    	}
    	for (x=0; x<5; x++)
    	{
    		printf ("Arr: %s", arr[x]);
    		printf ("Num: %d\n", num[x]);
    	}
    }
    It seems that i am reading garbage into arrays...
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  2. #2
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    nm: solved
    Code:
    		fscanf (pFile, "%s", arr[x]);
    		fscanf (pFile, "%d", &num[x]);
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Still using void main huh?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    ^ nah, was just testing out...
    didnt u use to have that animated gif of void main nuke
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > nah, was just testing out...
    And that's supposed to make it OK?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  2. Reading all the numbes from a file and storing in an array
    By derek tims in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2006, 03:01 PM
  3. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  4. File Reading and storing to 1 variable
    By Rare177 in forum C Programming
    Replies: 34
    Last Post: 07-13-2004, 12:58 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM