Ansi c file operation functions

Source: Internet
Author: User

1. open the file:

Extern file * fopen (_ const char * _ restrict _ filename, _ const * _ restrict _ modes); if the execution is successful, the pointer to the file to be opened is returned. If the execution fails, null is returned.
2. close the file: extern int fclose (File * _ stream); if the execution is successful, 0 is returned; otherwise, EOF is returned, and the error identification space error global variable is set.
3. Close all stream objects opened by the current process: extern int fcloseall (void); if the execution is successful, 0 is returned; otherwise, EOF is returned, and the error identification space error global variable is set.
4. Update the buffer content: extern int fflush (File * _ stream); if the execution is successful, 0 is returned; otherwise, EOF is returned, and the error identification space error global variable is set.
5. character read/write file stream: (read) extern int fgetc (File * _ stream); extern int GETC (File * _ stream ); // If the macro definition is successful, the read content is returned. If the macro definition fails, the EOF (-1) extern int getchar (void) is returned ); // read one character from the standard input stream (written) extern int fputc (INT _ c, file * _ stream); extern int putc (INT _ c, file * _ stream); extern int putchar (INT _ C); // If a character C is successfully written to the standard output, the content written to the stream is returned; EOF (-1) is returned if the request fails)
6. row read/write file stream: (read) extern char * fgets (char * _ restrict _ s, int _ n, file * _ restrict _ stream ); this function reads characters from the stream into the memory unit pointed to by s until it reads n-1 characters, line breaks, or encounters a file end sign. The string ends with a null character. After successful completion, return S. If the stream is at the end of the file, set the file end indicator for the stream and return a null pointer. If a read error occurs, the error indicator of the stream is set and errno is set to indicate this error. a null pointer is returned. (Write) extern int puts (_ const char * _ s); writes the characters whose s points to end with an empty character into the stdout of the standard output stream. And returns a line break. Extern int fputs (_ const char * _ restrict _ s, file * _ restrict _ stream); writes the specified s to the specified output stream, but do not append line breaks. Neither of the above functions is written with a null ending character. If the call succeeds, a non-negative number is returned. Otherwise, an EOF is returned and an error indicator is set for the stream. errno is set to indicate an error.
7. block read/write file stream: (read) extern size_t fread (void * _ restrict _ PTR, size_t _ size, size_t _ n, file * _ restrict _ stream); this function reads n size objects from the specified stream. The first parameter is the pointer to the object space to be read, that is, the storage location of the object to be read; the second parameter is the size of the read object. The third parameter is the number of read objects. The fourth object is the number of actually read objects returned by the read stream (not the size of read/write bytes ). Note: fread does not return the number of bytes. The returned value is the number of bytes only when the following values are used.

Char buff [size];
File * FP;
...
Fread (buff, 1, size, FP );
...
(Write) extern size_t fwrite (_ const void * _ restrict _ PTR, size_t _ size, size_t _ n, file * _ restrict _ s ); this function writes n size objects from the specified stream. The first parameter is the pointer to the object space to be written, that is, the storage location of the object to be written; the second parameter is the size of the written object. The third parameter is the number of written objects. The fourth object is the successful write of the stream. The returned number is the number of objects actually written to the specified stream (not the written bytes ). size ), otherwise,-1 is returned.
8. file stream Detection: extern int feof (File * _ stream); If the read ends, 1 is returned; otherwise, 0 is returned; extern int ferror (File * _ stream ); if no error occurs, 0 is returned. Otherwise, an error occurs. For plain text ASCII files, you can determine whether the ASCII code value is = EOF (-1) to determine whether the end Of the file stream is reached.
9. file stream location: (1) returns the current read/write location: extern long int ftell (File * _ stream); If successful, returns the number of bytes from the current pointer location to the start of the file. -1.
(2) modify the current read/write location: extern int fseek (File * _ stream, long int _ off, int _ whence ); the first parameter is the streaming object of the operation. The second parameter is the offset of the third parameter (modify benchmark). The third parameter is the benchmark of the modified location. Three of them: # define seek_set 0 # define seek_cur 1 # define seek_end 2 is successful, 0 is returned, otherwise-1 is returned.
(3) reset the current read/write location: extern void rewind (File * _ stream );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.