Thread: First entry of a file and transfer rest of file to an array

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    First entry of a file and transfer rest of file to an array

    I need to get the first number from the file as it gives the maximum elements of the array and then print the arrays. The file is called expt1.txt and contains the following

    3
    7 4
    8 7
    9 9

    Where 3 is the maximum elements in the array and the left column is the first array which I have called xarray and the right column is the second array which I have called yarray.

    I have written a program to find the max number of elements from the file and the two arrays but the program only reads the array on the right hand side. Can anybody see what I'm doing wrong?

    Code:
    include <stdio.h>			
    #include <math.h>			
    #include <ctype.h>			
    #include <stdlib.h>
    #include <string.h>
    
    
    #define READONLY "r"
    #define WRITEONLY "w"
    #define MAX_ELEMENTS 100
    
    int main (void)
    {
    	FILE *fp;
    	char filename[FILENAME_MAX + 1];
    	double *xarray, *yarray;
    	int datapoints, i, j;
    	
    	
    	
    	printf("\n\nWhat file do you wish to analyse: ");		/*Opening file*/
    	scanf("%s", filename);
    			
    				
    	if((fp = fopen(filename, READONLY)) == NULL){			/*Check that there are no errors in file*/
    					
    		printf("Cannot open file\n");
    		exit(EXIT_FAILURE);
    				
    	}
    	
    	printf("\n\nReading values from the file %s", filename);
    	
    	/* Read the number of datapoints in the array from the input file */	
    	fscanf(fp, "%d", &datapoints);
    	
    	if (datapoints > MAX_ELEMENTS){
    		fprintf(stderr, "Array size exceeds maximum number of datapoints.\n");
    		exit(EXIT_FAILURE);
    	}
    	
    	printf("\n\nThere are %d data points", datapoints);
    	
    	
    	xarray = (double *) malloc(datapoints * sizeof(double));
    	yarray = (double *) malloc(datapoints * sizeof(double));
    	
    	if((xarray = (double *)malloc(datapoints * sizeof(double))) == NULL ||
    	(yarray = (double *)malloc(datapoints * sizeof(double))) == NULL){
    		
    		fprintf(stderr, "Failed to allocate memory.\n");
    		exit(EXIT_FAILURE);
    	
    	}
    	
    	for(i=0; i < datapoints; i++){
    		
    		fscanf(fp, "%lf", &yarray[i]);
    			
    			if(!fscanf(fp, "%lf", &yarray[i])){
    				
    				fprintf(stderr, "Error reading element %d of vector2\n", i+1);
    				exit(EXIT_FAILURE);
    			
    			}
    		printf("\n%lf", yarray[i]);
    	}
    	
    	for(i=0; i < datapoints; i++){
    		
    		fscanf(fp, "%lf", &xarray[i]);	
    			
    			if(!fscanf(fp, "%lf", &xarray[i])){
    				
    				fprintf(stderr, "Error reading element %d of vector1\n", i+1);
    				exit(EXIT_FAILURE);
    			
    			}
    		printf("\n%lf", xarray[i]);
    	}
    	
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    xarray = (double *) malloc(datapoints * sizeof(double));
    yarray = (double *) malloc(datapoints * sizeof(double));
            
    if((xarray = (double *)malloc(datapoints * sizeof(double))) == NULL ||
      (yarray = (double *)malloc(datapoints * sizeof(double))) == NULL){
    fprintf(stderr, "Failed to allocate memory.\n");
    exit(EXIT_FAILURE);
    }
    You have a memory leak here. You're allocating memory, then immediately overwriting it. You meant to do something like this:
    Code:
    xarray = malloc(datapoints * sizeof *xarray);
    yarray = malloc(datapoints * sizeof *yarray);
    if(xarray == NULL || yarray == NULL)
    {
      fprintf(stderr, "error allocating memory\n");
      exit(EXIT_FAILURE);
    }
    As for the reason you're not reading properly, it's because you're (apparently) assuming that fscanf() knows you want to read column-by-column. It has no way of knowing this. What fscanf() does is read the file from beginning to end. If you're reading in values, it will see them in this order: 3 7 4 8 7 9 9.

    The answer to the problem is to read your arrays in parallel:
    Code:
    for(i = 0; i < datapoints; i++)
    {
      if(fscanf(fp, "%lf%lf", &xarray[i], &yarray[i]) != 2) /* bad */;
    }
    As a side note, when printing out doubles, %f should be used, not %lf. In C89 (the first C standard, and most widely implemented one), %lf is undefined. It might work, it might not. %f works for doubles because of how C's type system is implemented: when a float is passed to printf(), it is automatically converted to a double. Thus %f works for both floats and doubles because you can never actually pass a float to printf().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading file characters into an int array
    By spongefreddie in forum C Programming
    Replies: 9
    Last Post: 10-28-2010, 12:23 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  4. file transfer problem - Help!
    By John_2007 in forum C Programming
    Replies: 3
    Last Post: 09-19-2007, 04:28 PM
  5. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM

Tags for this Thread