Download presentation
Presentation is loading. Please wait.
Published byEmery Davidson Modified over 8 years ago
1
File Input/Output
2
File information Name z:\Visual Studio 2005\Projects\fileio\fileio\myinput.txt \\file-svr-1\class\ECE-160\pviall\myoutput.txt Read/Write Type (binary or ASCII text) Access (security; single/multiple user) Position in file All above info is stored in a FILE type variable, pointed to by a file handle
3
File i/o function calls fopen( filename, file_access ) filename: is name of file file_access is: “r+t” for reading text file “w+t” for writing text file many other options available returns: file_handle, that is the address of FILE (a FILE * ) on success NULL (zero) on failure
4
File i/o function calls fclose(file_handle) Closes a file This is recommended for input files (to free up system resources) This is required for output files (as often times the O/S does not write the last bit of a file out to the disk until the file is closed
5
File i/o function calls fprintf( file_handle, format_specifier, 0 or more variables ) file_handle: is address returned by openf() format_specifier: same as for printf() 0 or more variables: same as printf()
6
Sample program Read three integer values from the file myinput.txt Determine sum and average Write the original three values as well as the sum and average to the file myoutput.txt
7
The program (part 1) #define _CRT_SECURE_NO_DEPRECATE #include 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); }
8
The program (part 2) // 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); }
9
Adding a text file to the project (way 1) Use a text editor (notepad, wordpad, etc) to create a text file. Save the file in the proper directory. If your project is named fileio, the proper directory and filename would be: Z:\Visual Studio 2005\Projects\fileio\fileio\myinput.txt
10
Adding a text file to the project (way 2) 1.Project 2.Add new item 3.Utility 4.Text file 5.Enter file name This creates another tab where you may input your myinput.txt file
11
Viewing your myoutput.txt file (way 2) 1.Project 2.Add existing item 3.Change file type to all files 4.Click myoutput.txt – this will cause myoutput.txt to appear in the file list on the left 5.Enter file name This creates another tab where you may input your myinput.txt file
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.