fread file contents in to 2D Array!

This is a discussion on fread file contents in to 2D Array! within the C Programming forums, part of the General Programming Boards category; Hi! I have created a text file containing 20 rows of 20 ints. I am attempting to read the contents ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Question fread file contents in to 2D Array!

    Hi!

    I have created a text file containing 20 rows of 20 ints. I am attempting to read the contents of the file into a 20x20 2D array of int. But unfortunately its not working and after many hours I am come to ask for expert help.

    Here is my code thus far:

    Code:
    #include <stdio.h>
    
    #define MAX 20
    #define FTWENTY "twenty.txt"
    
    main()
    {
    	int my_array[MAX][MAX] = {0};
    	int i, j;
    	FILE *fp;
    
    	fp = fopen(FTWENTY, "r");
    
    	if (fp == NULL)
    	{
    		perror("Couldn't open the file" FTWENTY);
    		exit(1);
    	}
    	
    	if (fread(my_array, sizeof(int), (MAX*MAX), fp) < (MAX*MAX))
    	{
    		perror("Couldn't read the file" FTWENTY);
    		fclose(fp);
    		exit(1);
    	}
    
    	for (i=0; i < MAX; ++i)
    	{
    		for (j=0; j < MAX; ++j)
    			printf("%2d", my_array[i][j]);
    			putchar('\n');
    	}
    
    
    	fclose(fp);
    
    	return 0;
    
    }
    There definetley seems to be something wrong with the fread line. This is the first time I've ever used it. When I run the program i get ...

    Couldn't read the filetwenty.txt: No error


    Can anyone help please?

    Thanks!
    Last edited by strokebow; 11-28-2006 at 04:44 PM.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    A text file is text. If you are attempting to fread() then this should be a binary file. ints stored in a file are in 4 bytes, not 1 (a char). You'll have to read these in with fgets(), fscanf(), or fread() into a string then process these chars into ints.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    How do you mean? Because in the text file all the data is numbers.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >How do you mean? Because in the text file all the data is numbers.
    If it's a text file, you can't use fread. You can either:
    (1) use a for-loop, using fscanf to read each number, or:
    (2) Read a whole line with fgets, followed by a for-loop with sscanf.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    its better that u use fscanf function to read each integer from the file and store that in the array. care should be taken that after reading 20 ints from the file u got increment the array count by one so that it would point to array[i][..]. If u know what i mean.

    using fgets and sscanf the code becomes too sscanf had to scan for 20 ints in the file which is need read which becomes a quite mess. Again care should be taken while reading the right ints from the line

    some hints

    ssharish2005

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Would my code work if I changed the file I was reading to a binary file? My understanding of this topic is at the moment pretty poor.

    Please Help

    Thanks

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Yes, it would, however, how do you plan to write this file? You need to focus in on what you have been told. You need to read the file in as text and convert the text to int.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Thumbs up

    I have since modified my code - to this but it still dont work

    any help??

    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    
    #define MAX 20
    #define FTWENTY "twenty.txt"
    
    main()
    {
    	int my_array[MAX][MAX] = {0};
    	int i, j, num;
    	FILE *fp;
    	char ch;
    
    	fp = fopen(FTWENTY, "r");
    
    	if (fp == NULL)
    	{
    		perror("Couldn't open the file" FTWENTY);
    		exit(1);
    	}
    
    	for (i=0; ch != EOF; ++i)
    	for (j=0; ch != '\n'; ++j)
    	{
    		fscanf(fp, "%d", &num);		
    		ch = getc(fp);
    		my_array[i][j] = num;		
    	}
    	
    	for (i=0; i < MAX; ++i)
    	{
    		for (j=0; j < MAX; ++j)
    			printf("%2d", my_array[i][j]);
    			putchar('\n');
    	}
    
    	/*
    	if (fread(my_array, sizeof(int), (MAX*MAX), fp) < (MAX*MAX))
    	{
    		perror("Couldn't read the file" FTWENTY);
    		fclose(fp);
    		exit(1);
    	}
    	*/
    
    	for (i=0; i < MAX; ++i)
    	{
    		for (j=0; j < MAX; ++j)
    			printf("%2d", my_array[i][j]);
    			putchar('\n');
    	}
    
    
    	fclose(fp);
    
    	return 0;
    
    }
    Please help and give advice!!!
    Thanks!!!!!

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    for (i=0; ch != EOF; ++i)
    first time condition is checked before ch is initialized
    no check is done for array bounds
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    when the first line is finished the ch contains \n
    Because it is not reset all next iteratian of the external loop skip the internal loop
    Because the ch is not changed - the external loop will be endless

    You should rethink your conditions in the for statement
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    This might work.
    Code:
    	int done;
    
    	i = 0;
    	done = 0;
    	do {
    		j = 0;
    		do {
    			if (fscanf(fp, "%d", &num) == 1)
    			{		
    				my_array[i][j++] = num;		
    				ch = getc(fp);
    			}
    			else
    			{
    				done = 1;
    				break;
    			}
    		} while (ch != '\n');
    		++i;
    	} while (!done);
    Also, in your original code, ch should be declared as an int, because that's what fgetc returns. Otherwise ch will never match EOF, because EOF is defined as a negative int.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    However, even after changing ch to an int, your original code still won't work due to the problems Vart mentioned. After it hits the first newline, the code is stuck an endless loop.

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Thumbs up

    Thanks guys!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array and fwrite and fread
    By totalnewbie in forum C Programming
    Replies: 14
    Last Post: 01-10-2009, 03:45 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 01:39 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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