Several minor issues with the C language fopen function in Linux

Source: Internet
Author: User
In Linux, there are several minor issues with the C language fopen function-Linux general technology-Linux programming and kernel information. The following is a detailed description. 1. The difference between quotation marks when using fopen (File opening.

Char filename [20] = "NULL ";
Char directory [20] = "NULL ";

Printf ("please input the file name. \ n ");
Scanf ("% s", filename );

Fp = fopen ("filename", "w"); adding quotation marks will create a file in the current directory in the name of filename itself

Fp = fopen (filename, "w"); files are created in the current directory in the name of the array filename value without quotation marks.

If you want to create a file named after the array filename value in a directory, you can do the following:
Sprintf (directory, "/home/renewjoy/% s", filename); // sprintf can connect two strings

Or: strcpy (directory, "/home/renewjoy/"); // # include
Strcat (directory, filename );

Fp = fopen (directory, "w ");

2. In fopen, it seems that it cannot be parsed "~ "Symbol, but can parse". ","... "path.

Fp = fopen ("~ /Study/c/test. c "," w "); an error is returned.
Fp = fopen ("$ HOME/study/c/test. c", "w"); an error is returned.
Fp = fopen (/home/renewjoy/study/c/test. c, "w"); the absolute path must be used.
Fp = fopen ("../test. c", "w"); you can create the file test. c In the parent directory of the current directory.

3. open the file in write mode: fopen (". /open_file "," w "). The file content cannot be read. You must set the open mode to read and write:" w +"
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: 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.