Linux C file input and output functions fopen (), getc (), PUTC (), fclose (), fprintf (), fscanf (), fgets (), fputs (), fseek (), Ftell (), Fgetpos (), Fsetpos () detailed

Source: Internet
Author: User

fopen (Open file)

Defining functions

FILE * fopen(const char * path,const char * mode);

Function description

Parameter The path string contains the file path and filename to open, and the parameter mode string represents the flow pattern. The

Mode has the following pattern strings:

r       Open a read-only file that must exist.
R+    Opens a writable file that must exist.
W      Open write-only file, if the file exists then the file length is clear to 0, that is, the contents of the file will disappear. If the file does not exist, the file is created.
w+ opens a read-write file, and if the file exists, the file length is zeroed, meaning the contents of the file disappear. If the file does not exist, the file is created.
a+   Opens a read-write file in an attached way. If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained.

The pattern string above can be added with a B character, such as RB, W+b, or ab+, and a B character is used to tell the library that the file opened is a binary file, not a plain text file. However, in a POSIX system, including Linux ignores the character. A new file created by fopen () will have a s_irusr| s_iwusr| s_irgrp| s_iwgrp| s_iroth| S_iwoth (0666) permission, this file permission will also refer to the Umask value.

return value

When the file is opened successfully, the file pointer to the stream is returned. If the file fails to open, it returns NULLand the error code exists in errno.

Additional Instructions

In general, after opening the file will make some file read or write action, if the file fails to open, the next reading and writing action can not be carried out smoothly, so after fopen () Please make error judgment and processing.

Example

#include <stdio.h>int main (int argc, char **argv) {FILE *FP;FP = fopen ("/users/jianbao/clionprojects/apue/123.c", "A +"), if (fp = = NULL) {printf ("open fail\n");} else{printf ("Open success\n");} return 0;}

int getc (FILE * stream);
&NBSP; getc (one character read by file)

Define functions

Function description

getc () is used to read a character from the file that the parameter stream refers to. Returns eof if the end of the file is read and there is no data. Although getc () and fgetc () has the same effect, but getc () is defined as a macro, Not a real function call.

Note: getchar () and #include <stdio.h>int main (int argc, char **argv) {FILE *fp;int C;FP = fopen ("/users/jianbao/clionprojects/apue/123.c", "R"), while ((c = fgetc (fp)) = EOF) {printf ("%c\n", c);} Fclose (FP); return 0;}

The

Linux C file input and output functions fopen (), getc (), PUTC (), fclose (), fprintf (), fscanf (), fgets (), fputs (), fseek (), Ftell (), Fgetpos (), Fsetpos () detailed

Related Article

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.