Unformatted text preview:

Chapter 7 FilesObjectivesFileFile AccessFile UtilitiesAccess File by File PointerAccess File by File Pointer - fopenAccess File by File Pointer – fopen (Cont.)Slide 9Slide 10Access File by File Pointer – fscanf, fprintfAccess File by File Pointer – getc, putcAccess File by File Pointer – fgets, fputsAccess File by File Pointer – fread, fwriteAccess File by File Pointer – rewindAccess File by File Pointer – fseekAccess File by File Pointer – ftellAccess File by File Pointer – feofAccess File by File Pointer – fcloseAccess File by File Pointer (Cont.)Other Utilities Using File PointerAccess File by File DescriptorAccess File by File Descriptor - openAccess File by File Descriptor – open (Cont.)Slide 25Slide 26Slide 27Access File by File Descriptor – read, writeAccess File by File Descriptor – read, write (Cont.)Access File by File Descriptor – lseekAccess File by File Descriptor – closeAccess File by File Descriptor (Cont.)Slide 33Interacting with Operating EnvironmentInteracting with Operating Environment (Cont.)Interacting with Operating Environment - systemInteracting with Operating Environment – popen, pcloseReferencesChapter 7 FilesBy C. ShingITEC DeptRadford UniversitySlide 2ObjectivesUnderstand how to use file utilitiesUnderstand how to use files by file pointersUnderstand how to use files by file descriptorsUnderstand how to interact with operating environmentSlide 3FileA stream of bytesText file: user readableBinary file: machine readable, more efficientSlide 4File AccessCan access (by opening file first) byFile pointer: address of a structure FILE * defined in stdio.hHas buffer availableFile descriptor: non-negative number represents fileNo data structure, flexibleNeeds to define buffer to hold more than 1 characterNo formatting specifiedSlide 5File UtilitiesCreate a temporary filetmpnam (NULL)Returns a string of a temporary file nameDelete fileremove (filenamestring);Change filenamerename (oldname string, newnamestring);Example: char *tmpfile;tmpfile=tmpnam(NULL);remove (tmpfile);Slide 6Access File by File PointerFile pointer: declare for each file usedPre-defined: stdin (keyboard input), stdout (output to screen),stderr (error message to screen)Declared asFILE *filepointername;Example:FILE *infile, *outfile;Slide 7Access File by File Pointer - fopenOpen file:Form: fopen (“filename”, ”permission”)Returns a file pointer Starts from beginning of the fileNeed to check successful when use fopenSlide 8Access File by File Pointer – fopen (Cont.)Open file: (Cont.)Permission:Unix: both text and binary filer: read, for inputw: write, for outputIf file not exist, create itIf file exists, erase file content a: append to end of file, for updatingIf file not exist, create itr+. w+: read and writeSlide 9Access File by File Pointer – fopen (Cont.)Open file: (Cont.)Example:infile=fopen (“/usr/include/stdio.h”, “r”); or outfile=fopen (“current_dir_file”, “w”); or outfile=fopen (argv[1], “a”);Slide 10Access File by File Pointer – fopen (Cont.)Open file: (Cont.)Permission: (Cont.)MS-DOSTex file: same as in UnixBinary file:rb: readwb: writeab: appendr+, w+: read and writeSlide 11Access File by File Pointer – fscanf, fprintfRead/Write file: for text fileAny data typeInput form:fscanf(inputfilepointer, “format”, variable_addr)Output form:fprintf(outputfilepointer, “format”, variable_list)Example:char character;while (fscanf(infile,”%c”,&character) != EOF)fprintf (outfile, ”%c”, character);Slide 12Access File by File Pointer – getc, putcRead/Write file:CharacterInput form: getc (inputfilepointer)Output form: putc (character_variable, outputfilepointer)Example:char character;while ((character=getc(infile)) != EOF)putc (character, outfile);Slide 13Access File by File Pointer – fgets, fputsRead/Write file:StringInput form: fgets (string, n, inputfilepointer)Read at most n-1 characters into string from inputfileOutput form: fputs (string, outputfilepointer)Writes the string (except NULL character into outputfileExample:while (fgets(instring, n, infile) != EOF)fputs (instring, outfile);Slide 14Access File by File Pointer – fread, fwriteRead/Write file: for binary fileInput form: fread (arrayaddress, cellsize, n, inputfilepointer)Read at most n*cellsize bytes into arrayaddress from inputfileOutput form: fwrite (arrayaddress, cellsize, n, outputfilepointer)Writes at most n*cellsize bytes from arrayaddress into outputfileExample:int arrayA[n];while (fread(arrayA, 4, n, infile) > 0)fwrite (arrayA, 4, n, outfile);Slide 15Access File by File Pointer – rewindMove pointer to the beginning of the file:Form: rewind (filepointer);Example:rewind (infile);Slide 16Access File by File Pointer – fseekMove pointer to any place of the file: fseekForm: fseek (filepointer, offset, position);(Byte) Offset (int): relative to position-: to previous+: to nextPosition:0: beginning1: current position2: endExample:fseek (outfile, 0, 2); // go to the end of the fileSlide 17Access File by File Pointer – ftellcheck the current file position: ftellForm: ftell (filepointer);Example:while (ftell (infile)>0) putc(getc(infile), outfile);Slide 18Access File by File Pointer – feofcheck the end of file: feofForm: feof (filepointer);Example:while (!feof (infile)) putc(getc(infile), outfile);Slide 19Access File by File Pointer – fcloseForm: fclose (filepointer);Example:fclose (infile);fclose (outfile);Slide 20Access File by File Pointer (Cont.)Class Example: Example1(Bubble) Sort Structure using Files:Program, Data, OutputSlide 21Other Utilities Using File Pointertmpfile(): returns a file pointerExample:FILE *tmpfileptr; tmpfileptr=tmpfile();putc(getc(infile),tmpfileptr);Slide 22Access File by File DescriptorFile descriptor: nonnegative integer for each file Reserved: 0 (stdin), 1 (stdout), 2 (stderr)The rest files starts using 3, created when use open()Slide 23Access File by File Descriptor - openOpen file:Starts from beginning of the fileDefined in unistd.h (in MS-DOS, use io.h)check successful when use openForm: open (“filename”, options, permission_octal)Returns a file descriptorSlide 24Access File by File Descriptor – open


View Full Document

Radford ITEC 310 - Lecture Notes

Documents in this Course
Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?