1 / 11

File Input/Output

File Input/Output. File information. Name z:Visual Studio 2005Projects fileio fileio myinput.txt \file-svr-1classECE-160pviallmyoutput.txt Read/Write Type (binary or ASCII text) Access (security; single/multiple user) Position in file

jsale
Download Presentation

File Input/Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 successNULL (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 <stdio.h> #include <stdlib.h> void main() { FILE *infile; FILE *outfile; intx,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) • Project • Add new item • Utility • Text file • Enter file name This creates another tab where you may input your myinput.txt file

  11. Viewing your myoutput.txt file (way 2) • Project • Add existing item • Change file type to all files • Click myoutput.txt – this will cause myoutput.txt to appear in the file list on the left • Enter file name This creates another tab where you may input your myinput.txt file

More Related