Thread: problems reading input file into two arrays

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    21

    problems reading input file into two arrays

    Hi, it's me again. *waves* I'm having some trouble, and I could really use a fresh set (or sets) of eyes.

    This program is supposed to take two lists of integers of the same length ended by -1 and store them in two arrays, x and y, which each have 20 elements. The lists are not necessarily 20 numbers long. There's more after that, but it seems most likely that the problem is in my read_file function:

    Code:
    #define SENTINEL -1
    
    void read_file(int x[20], int y[20], int *count){
    	FILE *fp;
    	char filename[80], skip_ch;
    	int value, status, i;
    	printf("Please enter the file name>> ");
    	scanf("%s", &filename);
    	scanf("%c", &skip_ch); //eat \n
    	fp = fopen(filename, "r");
    	status=fscanf(fp, "%d", &value);
    	for(i=0; value!=SENTINEL && status==1 && i<20; i++){
    		x[i]=value;
    		status=fscanf(fp, "%d", &value);
    	}//read first line into x[]
    	if(status!=1 || value==SENTINEL){
    		do{
    			fscanf(fp, "%c", &skip_ch);
    		}while(skip_ch!='\n');
    	}//if status!=1 || value==SENTINEL
    	for(i=0; value!=SENTINEL && status==1 && i<20; i++){
    		y[i]=value;
    		status=fscanf(fp, "%d", &value);
    	}//read second line into y[]
    	*count=i;
    }//read_file
    
    int main()
    {
    	int x[20], y[20], z[20], count;
    	read_file(x, y, &count); //prompts for a file name and reads the file into the two arrays
    	multiply_arrays(x, y, z, count); //multiplies x and y and puts the results in z
    	display_arrays(x, y, z, count); //displays the three-column table
    	z_sqrt(z, count); //computes the sum of z, then displays the sum and its square root
    	return 0;
    }//main
    Here's the output I get on running it:

    Code:
    teyla:~ quasigreat$ cat lists1.dat
    10 2 3 17 18 -1
    99 8 26 35 40 -1
    
    teyla:~ quasigreat$ ~quasigreat/a.out
    Please enter the file name>> lists1.dat
    The sqrt of 0 is   0.00
    Help is much appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    You forget "status=fscanf(fp, "%d", &value);"
    before starting the second line reading.
    You should also check the return of fopen() (what happens if the file cannot be opened?)

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    21
    Thank you! That's fixed it. In the instructor's executable, entering a filename for a file that doesn't exist causes a segmentation fault, so it's not something I need to worry about in this context. (Needless to say, in real life, it'd be a good idea.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File reading problems
    By VR6_Nut in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 05:55 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  3. problem in reading alphabets from file
    By gemini_shooter in forum C Programming
    Replies: 6
    Last Post: 03-09-2005, 01:49 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Reading from a .txt file or use arrays.
    By []--JOEMAN--[] in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2001, 07:55 AM