Thread: Reading from file into Array

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    15

    Reading from file into Array

    OK, I have a text file that has "1,2,3 d, e, 6,6, h,h, ...etc" I want to put all the digits into an array for analysis. So the array size known. I am able to open the file and filter the desired digits, but I can't seem to store the numbers into the array properly.

    In the array I should see "1,2,3,6,6,..." but I dont. Where did I go wrong?

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define ArraySize 40
    
    int ReadDataFromFile(int FileData[ArraySize]);
    void IntializeArray(int FileData[ArraySize]);
    
    int main( void ) {
    	int i;
    	int FileData[ArraySize];
    	int count=0;
    
    	IntializeArray(FileData);
    
    	/*useful data in the array*/
    	count=ReadDataFromFile(FileData);
    
    	for(i=0;i<count;i++)
    		printf("%d",FileData[i]);
    
    	return 0;
    }
    
    void IntializeArray(int FileData[ArraySize]){
    	int i;
    	
    	for(i=0;i<ArraySize;i++)
    		FileData[i]=1010;
    	};
    
    int ReadDataFromFile(int FileData[ArraySize]){
    int i;
    	
    	  FILE *fp = fopen( "testdata", "r" );
    
      if ( fp ) {
    		int ch;
    
    		i=0;
    		while ( ( ch = fgetc( fp ) ) != EOF ) {
    			if(isdigit(ch)){
    				FileData[i]=ch;
    				i++;
    				}
    		}
    
    		fclose( fp );
      } else {
        	perror( "error opening the file" );
      	}
     	return i;
    	}
    I got the code to open the file from the development section of this forum.
    Last edited by lvl99; 09-22-2009 at 09:37 PM. Reason: corrected the number test data

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since "66" is not one character, I would imagine fgetc ("get character from file") would be highly unsuitable for this task. Perhaps some other form of input could be used instead, say perhaps fgets.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Quote Originally Posted by tabstop View Post
    Since "66" is not one character, I would imagine fgetc ("get character from file") would be highly unsuitable for this task. Perhaps some other form of input could be used instead, say perhaps fgets.
    oh sorry it should be just one character, it's one character per line.
    1
    2
    3
    6
    6

    I made the change but still, my output is still the same.
    Last edited by lvl99; 09-22-2009 at 09:37 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Now you're going to have decide which version of reality you live in: does the input look like "1, 2, 3, 66" or does it look like
    1
    2
    3
    6
    6
    ? If the latter, I don't see how you could possibly try to recombine numbers into their constituent parts, so I won't try.

    If the former, just doing s/fgetc/fgets does not actually fix your problem. You need to look at the input fgets gives you and decide what to do with it (hint: while isdigit can be your friend, now is not the time).

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    it's the latter.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define ArraySize 40
    
    int ReadDataFromFile(int FileData[ArraySize]);
    void IntializeArray(int FileData[ArraySize]);
    
    int main( void ) {
    	int i;
    	int FileData[ArraySize];
    	int count=0;
    
    	IntializeArray(FileData);
    
    	/*useful data in the array*/
    	count=ReadDataFromFile(FileData);
    
    	for(i=0;i<count;i++)
    		printf("%d",FileData[i]);
    
    	return 0;
    }
    
    void IntializeArray(int FileData[ArraySize]){
    	int i;
    	
    	for(i=0;i<ArraySize;i++)
    		FileData[i]=1010;
    	};
    
    int ReadDataFromFile(int FileData[ArraySize]){
    int i;
    	
    	  FILE *fp = fopen( "testdata", "r" );
    
      if ( fp ) {
    		int ch;
    
    		i=0;
    		while ( ( ch = fgetc( fp ) ) != EOF ) {
    			if((ch >= 0) && (ch <=9) ){
    				FileData[i]=ch;
    				i++;
    				}
    		}
    
    		fclose( fp );
      } else {
        	perror( "error opening the file" );
      	}
     	return i;
    	}
    I changed that line from using the isdigit function, but I get anything in my array. I will look at it in the morning, I think I'm over thinking it. Thanks, so far.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're testing characters, you probably want '0' and '9', not 0 and 9.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Quote Originally Posted by quzah View Post
    If you're testing characters, you probably want '0' and '9', not 0 and 9.


    Quzah.
    Thanks that did the trick.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int main( void ) {
        int i;
        int FileData[ArraySize];
        int count=0;
    
        IntializeArray(FileData);
    
        /*useful data in the array*/
        count=ReadDataFromFile(FileData);
    
        for(i=0;i<count;i++)
            printf("%d",FileData[i]);
    
    	return 0;
    }
    
    int ReadDataFromFile(int FileData[ArraySize]){
        int i;
    	
        FILE *fp = fopen( "testdata", "r" );
    
        if ( fp ) {
            ...
        } else {
            perror( "error opening the file" );
        }
        return i;
    }
    If your fopen call happens to fail, you'll return an uninitialized variable's value back to the main function. This unknown value will then be used to index the array. You're just asking for a segfault. You should probably add a i = 0; line in that section of code where the perror function is. [edit]Or perhaps just initialize i to 0 right from the start.[/edit]

    Code:
    i=0;
    while ( ( ch = fgetc( fp ) ) != EOF ) {
        if((ch >= 0) && (ch <=9) ){
            FileData[i]=ch;
            i++;
        }
    }
    You are also not doing even just a simple check to ensure that i does not grow to ArraySize or more. You should perhaps modify the condition part of the while loop, maybe add something such that you only enter the loop as long as i is less than ArraySize.
    Last edited by hk_mp5kpdw; 09-23-2009 at 11:36 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Thanks ,hk_mp5kpdw, I'll make those changes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Reading from a file into an array
    By fmsguy06 in forum C Programming
    Replies: 6
    Last Post: 10-18-2008, 09:25 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Reading Characters from file into multi-dimensional array
    By damonbrinkley in forum C Programming
    Replies: 9
    Last Post: 02-24-2005, 01:31 PM