Linux下fopen路径操作实例详解 (linux fopen 路径)

在Linux系统下,文件操作是非常常见的任务。其中,文件的读写操作是一个十分基本和常见的操作。而在C语言中,打开文件和读写文件常常使用fopen函数。fopen函数可以打开一个指定的文件,并返回一个文件指针,由此可以进行文件的读写操作。

fopen函数的语法如下:

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

其中,path表示打开文件的路径,mode表示打开的模式,具体的打开方式如下表所示:

| 模式 | 描述 |

| —- | —- |

| r | 以只读方式打开文件 |

| w | 以只写方式打开文件 |

| a | 以追加方式打开文件 |

| r+ | 以可读可写的方式打开文件 |

| w+ | 以可读可写的方式打开文件(若文件存在则清空文件) |

| a+ | 以可读可写的方式打开文件(若文件存在则从文件尾开始写入) |

值得注意的是,在Linux系统下,文件路径需要使用”/”代替”\”,并且路径中不允许出现空格。

接下来,我们将通过实例详解fopen函数在Linux下的路径操作。

示例1:在当前目录下创建一个文件并写入数据

+ 代码:

“`

#include

int mn()

{

FILE *fp;

char *filename = “test.txt”;

char *mode = “w”;

char *content = “This is a test file.”;

fp = fopen(filename, mode);

if(fp == NULL)

{

printf(“Fled to open file %s.\n”, filename);

}

else

{

fprintf(fp, “%s”, content);

printf(“Write file %s succeeded.\n”, filename);

fclose(fp);

}

return 0;

}

“`

+ 运行结果:

“`

Write file test.txt succeeded.

“`

我们可以看到,程序正常地创建了一个文件,并向其中写入了数据。

示例2:在当前目录下打开一个已有文件并读取数据

+ 代码:

“`

#include

int mn()

{

FILE *fp;

char *filename = “test.txt”;

char *mode = “r”;

char buffer[100];

fp = fopen(filename, mode);

if(fp == NULL)

{

printf(“Fled to open file %s.\n”, filename);

}

else

{

fgets(buffer, 100, fp);

printf(“Read file %s succeeded: %s\n”, filename, buffer);

fclose(fp);

}

return 0;

}

“`

+ 运行结果:

“`

Read file test.txt succeeded: This is a test file.

“`

在这个例子中,我们打开了一个已有的文件,并从中读取了数据。fgets函数可以从文件中读取一行数据,并将其保存在一个字符数组中。

示例3:在上级目录中创建一个文件并写入数据

+ 代码:

“`

#include

int mn()

{

FILE *fp;

char *filename = “../test.txt”;

char *mode = “w”;

char *content = “This is a test file in the parent directory.”;

fp = fopen(filename, mode);

if(fp == NULL)

{

printf(“Fled to open file %s.\n”, filename);

}

else

{

fprintf(fp, “%s”, content);

printf(“Write file %s succeeded.\n”, filename);

fclose(fp);

}

return 0;

}

“`

+ 运行结果:

“`

Write file ../test.txt succeeded.

“`

在这个例子中,我们在上级目录中创建了一个文件,并向其中写入了数据。需要注意的是,我们在路径中使用了”../”来表示上级目录。

示例4:在绝对路径下创建一个文件并写入数据

+ 代码:

“`

#include

int mn()

{

FILE *fp;

char *filename = “/home/user/test.txt”;

char *mode = “w”;

char *content = “This is a test file with absolute path.”;

fp = fopen(filename, mode);

if(fp == NULL)

{

printf(“Fled to open file %s.\n”, filename);

}

else

{

fprintf(fp, “%s”, content);

printf(“Write file %s succeeded.\n”, filename);

fclose(fp);

}

return 0;

}

“`

+ 运行结果:

“`

Write file /home/user/test.txt succeeded.

“`

这个例子展示了如何使用绝对路径来创建一个文件。需要注意的是,不同用户的home目录可能不同,因此需要根据实际情况修改路径。

相关问题拓展阅读:

linux fopen函数 打开文件总是失败

是这样袜吵的再输入文件路径的时候要注意:

你的方向错了应歼唯该是”\”这样的反斜杠而且要输入两个反斜杠

因为

字符串

中的1个反斜告改侍杠的意思就是说他是个转意字符只有\\的时候才会显示出来1个字符向你的那个路径就应该写成fp=fopen(“\\mnt\\yaffs\\red.txt”)

有兴趣共同探讨C就给我留言啊

linux c 的 open(文件路径,O_WRON | O_CREAT) 里面的与运算为什么可以实现打不开就创建

因为第二个参数:O_WRON | O_CREAT

O_CREAT:如果打不开就创建

O_WRON | O_CREAT中间使用“|”,所以支持打不开就创建

open 函数可以打开或创建一个文件。

#include

#include

#include

int open(const char *pathname, int flags);

int open(const char *pathname, int flags, mode_t mode);

返回值:成功返回新分配的文件描述符,出错返回-1并设置errno

在Man Page中open 函数有两种形式,一种带两个参数,一种带三个参数,其实在C代码

中open 函数是这样声明的:

int open(const char *pathname, int flags, …);

最后的可变参数可以是0个或1个,由flags 参数中的标志位决定,见下面的详细说明。

pathname 参数是要打开或创建的文件名,和fopen 一样,pathname 既可以是相对路径也可以是绝

对路径。flags 参数有一系列常数值可供选择,可以同时选择多个常数用按位或运算符连接起

来,所以这些常数的宏定义都以O_开头,表示or。

必选项:以下三个常数中必须指定一个,且仅允许指定一个。

O_RDON 只读打开

O_WRON 只写打开

O_RDWR 可读可写打开

以下可选项可以同时指定0个或多个,和必选项按位或起来作为flags 参数。可选项有很多,这

里只介绍一部分,其它选项可参考open(2)的Man Page:

O_APPEND 表示追加。如果文件已有内容,这次打开文件所写的数据附加到文件的末尾而不

覆盖原来的内容。

O_CREAT 若此文件不存在则创建它。使用此选项时需要提供第三个参数mode ,表示该文件

的访问权限。

O_EXCL 如果同时指定了O_CREAT,并且文件已存在,则出错返回。

O_TRUNC 如果文件已存在,并且以只写或可读可写方式打开,则将其长度截断

(Truncate)为0字节。

O_NONBLOCK 对于设备文件,以O_NONBLOCK 方式打开可以做非阻塞I/O(Nonblock I/O).

在C中,常常使用二进制位来做控制标志,这个办法使得高效且代码短小,在头文件fcntl.h中,可以见到O_WRON的定义值是”01″,八位二进制就是””,O_CREAT是八进制”0100″,二进制就是””,竖线“|”不是“与”,是逐位“或”运算,O_RWON|O_CREAT合起来就是”“,这两个”1″的位置并不冲突,在这里,open()函数得到的值是编译器已经合并好了的值””,open()函数可以根据这两个独立的二进制”位”知道是读写打开或者创建。

这个是位或,不是与。位或、位与是这样计算的。

如:二进制的 010 | 001 结果是 011,而 010 & 001 结果就是0了。

O_WRON 和 O_CREAT 的关系就相当于上面的 010 和 001。他们位或的值不是0,位与的值就是0了。0表示什么都不做。用了位或后,就在一个整型的值上设置了不同的标志位,open函数会检测对应的标志位,如果该标志位设置为1了,就执行对应的操作。

O_CREAT的意思就是创建的意思,在这里就是将 创建文件 的标志位设置为1,这样open函数无法写这个文件的时候就会创建他。

#include

int open(const char *pathname, int oflag, … /*

mode_t mode */ );

We show the third argument as …, which is the ISO C way to specify that the number and types of the remaining arguments may vary. For this function, the third argument is used only when a new file is being created, as we describe later. We show this argument as a comment in the prototype.

The pathname is the name of the file to open or create. This function has a multitude of options, which are specified by the oflag argument. This argument is formed by ORing together one or more of the following constants from the header:

O_RDON

Open for reading only.

O_WRON

Open for writing only.

O_RDWR

Open for reading and writing.

Most implementations define O_RDON as 0, O_WRON as 1, and O_RDWR as 2, for compatibility with older programs.

One and only one of these three constants must be specified. The following constants are optional:

O_APPEND

Append to the end of file on each write. We describe this option in detail in Section 3.11.

O_CREAT

Create the file if it doesn’t exist. This option requires a third argument to the open function, the mode, which specifies the access permission bits of the new file. (When we describe a file’s access permission bits in Section 4.5, we’ll see how to specify the mode and how it can be modified by the umask value of a process.)

O_EXCL

Generate an error if O_CREAT is also specified and the file already exists. This test for whether the file already exists and the creation of the file if it doesn’t exist is an atomic operation. We describe atomic operations in more detail in Section 3.11.

O_TRUNC

If the file exists and if it is successfully opened for either write-only or readwrite, truncate its length to 0.

O_NOCTTY

If the pathname refers to a terminal device, do not allocate the device as the controlling terminal for this process. We talk about controlling terminals in Section 9.6.

O_NONBLOCK

If the pathname refers to a FIFO, a block special file, or a character special file, this option sets the nonblocking mode for both the opening of the file and subsequent I/O. We describe this mode in Section 14.2.

fopen的路径参数不能是char*?

照理说file应该声明为char数组啊宏洞,getcwd是将当前路径复制到之一个参数指向的空间,你只声明了一个指针铅谈,槐绝碰没有分配空间。你试试

char file;

linux fopen 路径的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于linux fopen 路径,Linux下fopen路径操作实例详解,linux fopen函数 打开文件总是失败,linux c 的 open(文件路径,O_WRON | O_CREAT) 里面的与运算为什么可以实现打不开就创建,fopen的路径参数不能是char*?的信息别忘了在本站进行查找喔。


数据运维技术 » Linux下fopen路径操作实例详解 (linux fopen 路径)