Thread: Reading from file and printing double data

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    7

    Reading from file and printing double data

    Hi!

    I am new to C and try to make a program that reads a segment of data from a file and prints it to the screen. When I declare the array that stores the data read from file as float, it works well. But when I declare it as double, the program prints out only zeros. Can anyone explain why? Am I doing something wrong?

    I need to store this data with double precision because of computations that will be done with them later. How can I fix it?

    Here is the code:

    Code:
    # include <stdio.h>
    
    int main (void)
    {
    	FILE *fpIn = fopen( "S001.txt", "r");	// Open input file
    
    	const int nsize = 2048;		
    		
    	double y[200000];
    
    	if (fpIn != NULL) // Successful opening of input file
    	{
    	
    		// Reads from input file first segment of nsize samples into y:	
    		unsigned int i;			
    		int numRead = 0;
    		for (i = 0; i < nsize; i++ )
            	{
    			numRead = fscanf(fpIn, "%f", &y[i]);
    
    			printf("%f\n",y[i]);
    
             	}
    
    		close(fpIn);			// Close input file
    	}
    
    	return 0;
    }
    Thank you in advance!

    Elena

  2. #2
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    do not put two hundred thousand doubles on the stack, please

    change %f to %lf
    Last edited by Tux0r; 07-14-2009 at 05:03 AM.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    Thank you Tux0r! I followed your advice and tried an array of size 1 and %lf, but it still prints out 0.000000. It is very strange.

  4. #4
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Code:
    #include <stdio.h>
    
    int main() {
    	FILE *f=fopen("file.txt","r");
    
    	if(f==NULL)
    		return 1;
    
    	double values[10];
    	unsigned int i;			
    
    	for(i = 0; i < 10; ++i) {
    		fscanf(f, "%lf",&values[i]);
    		printf("%lf\n",values[i]);
         }
    
    	close(f);
    
    	return 0;
    }
    file.txt
    Code:
    0.37
    13.37
    13.37
    13.37
    11.37
    12.37
    19.37
    13.37
    13.37
    13.37
    works here

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    Hurray! It worked!!! I just didn't put %lf on fscanf(), only on printf(), and when I did, it started to work! Thank you a lot, Tux0r!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  4. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM