150 likes | 277 Views
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
E N D
fread fwritefgets 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 • A stream provides the channel by which the programs and files communicate
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
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
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
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
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
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”
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
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
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
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
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