1 / 14

fread fwrite fgets fputs

fread fwrite fgets fputs. Group 6 Carlos Rodriguez Luz Comparan Scott Nienaber Adel Alraheem Kenny Gonzalez. Files in C. In C files are viewed as a sequence of bytes When a file is opened in C there is a stream associated with the file

mattox
Download Presentation

fread fwrite fgets fputs

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. fread fwritefgets fputs Group 6 Carlos Rodriguez Luz Comparan Scott Nienaber Adel Alraheem Kenny Gonzalez

  2. Files in C • In C files are viewed as a sequence of bytes • When a file is opened in C there is a stream associated with the file • A stream provides the channel by which the programs and files communicate

  3. fopen • Each file used in a program is required to have a unique name, and will have a unique file pointer returned by fopen • Example: fopen("direct.txt", "wb") with “direct.txt” being the file name parameter. The second parameter, “wb” is the mode parameter, and determines the way the file will be opened

  4. fopen • fopen opens the file whose name is specified in the filename parameter • fopen associates a stream with the file that is identified by the file object whose pointer is returned. The mode parameter determines which operations are allowed on the stream, and how these will be performed • If the file has been successfully opened, the function will return a pointer to a file object, that is used to identify the stream in further operations involving it. If not, a null pointer is returned

  5. fclose • fclose closes the file associated with the stream and disassociates it. The function receives the pointer to a file to specify the stream to be closed

  6. fread • fread reads a specified number of bytes from a file into memory • Ex: fread(array2, sizeof(int), SIZE, fp) where “sizeof(int)” determines the number of bytes read from the file refrenced by “array2.” The file pointer “fp” specifies the location in the file from where the bytes are read. “SIZE” specifies the number of array elements to be read

  7. fread • The fread function returns the number of items successfully input. If this number is less than the number specified in the argument of the function call, a read error is returned

  8. fwrite • The fwrite function writes a specific number of bytes of data to a file • Ex: fwrite(array1, sizeof(int), SIZE, fp) Where items from structure array “array1” of size “sizeof(int)” are written to the file pointed to by “fp”

  9. fwrite • The fwrite function stops writing bytes when the number of items of data specified by the “sizeof(int)” parameter have been written, or when an error condition is encountered

  10. fgets • The fgets (get string) function reads a limited number of characters from a file stream source, into an array of characters • fgets reads characters from a stream and stores them as a C string until (num – 1) characters have been read, or newline or end-of-file is reached

  11. fgets • Ex: fgets(buf, BUFLEN, fp); where “buf” is a string pointer to an array of characters where the string read is stored, “BUFLEN” (num) is the max number of characters to be read, and “fp” is a file pointer that identifies the stream where the characters are read from • The function returns the same string referenced by “buf” if successful. If not, a null pointer is returned

  12. fputs • The fputs (put string) writes a string to a stream • The function begins copying from the specified address until it reaches the terminating null character. This null character is not copied into the stream

  13. fputs • Ex: fputs(msg, fp) where “msg” is the array of characters to be written, including the final null character. “fp” is the file pointer that identifies the stream where the string is to be written • A non-negative value is successfully returned. If there is an error, end-of-file is returned

  14. Thank you

More Related