Thread: File i/o help!

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97

    File i/o help!

    So my professor gave us a sample program below, and it does not work, the program i must write must have an input of pi (1million numbers of precision) but i have no clue on how to start seeing as how the sample program doesnt even work lol. I understand how to do everything, besides setting up my file input correctly which apparently my professor cant do... Any help would be gracious.

    Edit: Everytime i run the sample it says my "error opening myinput.txt" which means that infile==null, but i dont know what that means, is my input file wrong or can it just not read because of something else.

    Edit 2: Okay now i am not getting the error, but nothing is printing in the output...??? thoughts

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    
    void main()
    {
    	FILE *infile;
    	FILE *outfile;
    	int x,y,z,sum;
    	float avg;
    
    	// Open input file, exit if error
    	infile=fopen("myinput.txt","r+t");
    	if (infile==NULL) 
    	{ 
    		printf("Error opening myinput.txt\n"); 
    		exit(0); 
    	}
    	
    	// Generally file opens are done as below
    	if ((outfile=fopen("myoutput.txt","w+t"))==NULL) 
    	{ 
    		printf("Error opening myoutput.txt\n"); 
    		exit(0); 
    		
    	// read the three values 
    	// its a good idea to  account for \n's in the file
    	fscanf(infile,"%d\n",&x);
    	fscanf(infile,"%d\n",&y);
    	fscanf(infile,"%d\n",&z);
    
    	// sum and avg
    	sum = x+y+z;
    	avg = (float)sum/3.0;
    
    	// print out values
    	fprintf(outfile,"Values: %d, %d, %d\n",x,y,z);
    	fprintf(outfile,"Sum: %d\n",sum);
    	fprintf(outfile,"Avg: %7.2f\n",avg);
    
    	// close the files
    	fclose(infile);
    	fclose(outfile);
    	}
    }
    Last edited by omGeeK; 11-07-2010 at 09:53 PM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Is the file in the same directory as the executable?

    Do you have permissions to read the file?

    Also, void main should be int main(void) and should return 0.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    view next post

  4. #4
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    well i changed this

    Code:
    	// Generally file opens are done as below
    	if ((outfile=fopen("myoutput.txt","w+t"))==NULL) 
    	{ 
    		printf("Error opening myoutput.txt\n"); 
    		exit(0); 
    		
    	// read the three values 
    	// its a good idea to  account for \n's in the file
    	fscanf(infile,"%d\n",&x);
    	fscanf(infile,"%d\n",&y);
    	fscanf(infile,"%d\n",&z);
    
    	// sum and avg
    	sum = x+y+z;
    	avg = (float)sum/3.0;
    
    	// print out values
    	fprintf(outfile,"Values: %d, %d, %d\n",x,y,z);
    	fprintf(outfile,"Sum: %d\n",sum);
    	fprintf(outfile,"Avg: %7.2f\n",avg);
    
    	// close the files
    	fclose(infile);
    	fclose(outfile);
    	}
    }

    to this and it works fine now...

    Code:
    	// Generally file opens are done as below
    	if ((outfile=fopen("myoutput.txt","w+t"))==NULL) 
    	{ 
    		printf("Error opening myoutput.txt\n"); 
    		exit(0); 
    	}	
    	// read the three values 
    	// its a good idea to  account for \n's in the file
    	fscanf(infile,"%d\n",&x);
    	fscanf(infile,"%d\n",&y);
    	fscanf(infile,"%d\n",&z);
    
    	// sum and avg
    	sum = x+y+z;
    	avg = (float)sum/3.0;
    
    	// print out values
    	fprintf(outfile,"Values: %d, %d, %d\n",x,y,z);
    	fprintf(outfile,"Sum: %d\n",sum);
    	fprintf(outfile,"Avg: %7.2f\n",avg);
    
    	// close the files
    	fclose(infile);
    	fclose(outfile);
    	
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM