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 2ObjectivesUnderstand how to use file utilitiesUnderstand how to use files by file pointersUnderstand how to use files by file descriptorsUnderstand how to interact with operating environmentSlide 3FileA stream of bytesText file: user readableBinary file: machine readable, more efficientSlide 4File AccessCan access (by opening file first) byFile pointer: address of a structure FILE * defined in stdio.hHas buffer availableFile descriptor: non-negative number represents fileNo data structure, flexibleNeeds to define buffer to hold more than 1 characterNo formatting specifiedSlide 5File UtilitiesCreate a temporary filetmpnam (NULL)Returns a string of a temporary file nameDelete fileremove (filenamestring);Change filenamerename (oldname string, newnamestring);Example: char *tmpfile;tmpfile=tmpnam(NULL);remove (tmpfile);Slide 6Access File by File PointerFile pointer: declare for each file usedPre-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 - fopenOpen file:Form: fopen (“filename”, ”permission”)Returns a file pointer Starts from beginning of the fileNeed to check successful when use fopenSlide 8Access File by File Pointer – fopen (Cont.)Open file: (Cont.)Permission:Unix: both text and binary filer: read, for inputw: write, for outputIf file not exist, create itIf file exists, erase file content a: append to end of file, for updatingIf file not exist, create itr+. 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-DOSTex file: same as in UnixBinary file:rb: readwb: writeab: appendr+, w+: read and writeSlide 11Access File by File Pointer – fscanf, fprintfRead/Write file: for text fileAny data typeInput 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, putcRead/Write file:CharacterInput 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, fputsRead/Write file:StringInput form: fgets (string, n, inputfilepointer)Read at most n-1 characters into string from inputfileOutput 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, fwriteRead/Write file: for binary fileInput form: fread (arrayaddress, cellsize, n, inputfilepointer)Read at most n*cellsize bytes into arrayaddress from inputfileOutput 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 – rewindMove pointer to the beginning of the file:Form: rewind (filepointer);Example:rewind (infile);Slide 16Access File by File Pointer – fseekMove pointer to any place of the file: fseekForm: fseek (filepointer, offset, position);(Byte) Offset (int): relative to position-: to previous+: to nextPosition:0: beginning1: current position2: endExample:fseek (outfile, 0, 2); // go to the end of the fileSlide 17Access File by File Pointer – ftellcheck the current file position: ftellForm: ftell (filepointer);Example:while (ftell (infile)>0) putc(getc(infile), outfile);Slide 18Access File by File Pointer – feofcheck the end of file: feofForm: feof (filepointer);Example:while (!feof (infile)) putc(getc(infile), outfile);Slide 19Access File by File Pointer – fcloseForm: 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 Pointertmpfile(): returns a file pointerExample:FILE *tmpfileptr; tmpfileptr=tmpfile();putc(getc(infile),tmpfileptr);Slide 22Access File by File DescriptorFile 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 - openOpen file:Starts from beginning of the fileDefined in unistd.h (in MS-DOS, use io.h)check successful when use openForm: open (“filename”, options, permission_octal)Returns a file descriptorSlide 24Access File by File Descriptor – open
View Full Document