linux printf往文件里面写入内容

文章讨论了使用C语言的printf函数向控制台和文件写入字符串时,fsync和fflush在同步文件内容上的区别。在不同代码版本中,fsync导致log.txt无内容,而fflush却能正常工作,原因在于stdio库函数的行为与fcntl.h库函数的不同。

printf用于向控制台打印字符串,而这里面的控制台其实是标准输出,fd值为1,故可以用下面代码写文件:

int main()
{
	close(1);
	int i = 1;

	int fd1 = open("/chkpnt/log.txt",O_WRONLY | O_CREAT, 0666);
	printf("i = %d\n",i);


	return 0;
}

代码里面,先将fd=1关闭掉,然后open一个文件,出来的文件描述符fd1的值就是1,然后printf向fd为1的文件里面写入东西,执行时,最终可以看到log.txt里面的字符串。

现在将程序做修改如下:

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>



int main()
{
	close(1);
	int i = 1;

	int fd1 = open("/chkpnt/log.txt",O_WRONLY | O_CREAT, 0666);
	
	while(1)
	{
		printf("i = %d\n",i);
		i++;
		sleep(1);
	}
	


	return 0;
}

此种请看下,程序一直在运行,每隔1秒写入一行内容,但是可以发现log.txt中看不到任何内容。

将此程序进行修改,去掉sleep(1)。此时相当于疯狂往log.txt里面写入,很快发现log.txt文件很大。

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>



int main()
{
	close(1);
	int i = 1;

	int fd1 = open("/chkpnt/log.txt",O_WRONLY | O_CREAT, 0666);
	
	while(1)
	{
		printf("i = %d\n",i);
		i++;
	}
	


	return 0;
}

此时再将sleep(1)加上,同时加上文件刷新fsync(fd1),代码如下所示:

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>



int main()
{
	close(1);
	int i = 1;

	int fd1 = open("/chkpnt/log.txt",O_WRONLY | O_CREAT, 0666);
	
	while(1)
	{
		printf("i = %d\n",i);
		fsync(fd1);
		i++;
	}
	


	return 0;
}

此时发现,log.txt里面依然没有内容。

最后将fsync(fd1);换成fflush(stdout);,然后发现log.txt里面有内容了,代码如下:

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>



int main()
{
	close(1);
	int i = 1;

	int fd1 = open("/chkpnt/log.txt",O_WRONLY | O_CREAT, 0666);

	while(1)
	{
		sleep(1);
		printf("i = %d\n",i);

		i = i + 1;
		fflush(stdout);
	}


	return 0;
}

为何fsync不行,fflush可以,难道是因为printf和fflush都是stdio.h里面的吗,有大神知道原因的,请不吝赐教。

<think>我们正在处理用户的问题:如何在Linux环境下使用C语言创建文件并向其中写入内容。根据引用[1]和引用[2]中的示例代码,我们可以总结出以下步骤:1.包含必要的头文件:如<sys/types.h>,<sys/stat.h>,<fcntl.h>,<stdio.h>,<unistd.h>,<string.h>,<stdlib.h>等。2.使用open函数创建并打开文件。open函数的第二个参数可以使用O_CREAT标志来创建文件(如果文件不存在),同时可以指定其他标志如O_RDWR(可读可写)、O_TRUNC(如果文件存在则截断为0)等。当使用O_CREAT时,需要提供第三个参数(文件权限模式),如0644(表示用户可读写,组和其他可读)。3.检查open的返回值(文件描述符)以确定是否成功。4.使用write函数向文件写入内容。write函数需要文件描述符、要写入的数据的指针和要写入的字节数。5.检查write的返回值以确定写入的字节数,并处理可能的错误。6.使用close函数关闭文件。因此,我们可以编写一个示例代码,创建一个文件写入字符串内容。注意:用户要求创建文件写入内容,所以我们需要在open函数中使用O_CREAT标志。如果文件不存在则创建,如果存在则可以选择截断(O_TRUNC)或追加(O_APPEND)等。这里我们使用O_CREAT|O_RDWR|O_TRUNC,这样如果文件不存在则创建,存在则清空。文件权限:我们使用0644(八进制),即rw-r--r--。下面给出示例代码:</think>### Linux C语言创建文件写入内容示例 在Linux环境下使用C语言创建文件写入内容,主要通过`open()`、`write()`和`close()`系统调用实现。以下是详细步骤和示例代码: #### 示例代码 ```c #include <sys/types.h> // 文件系统类型定义 #include <sys/stat.h> // 文件状态操作 #include <fcntl.h> // 文件控制选项 #include <stdio.h> // 标准输入输出 #include <unistd.h> // POSIX系统调用 #include <string.h> // 字符串操作 #include <stdlib.h> // 标准库函数 int main() { int fd; // 文件描述符 char *content = "Hello, Linux File System!"; // 要写入内容 // 创建并打开文件 (O_CREAT: 不存在则创建, O_RDWR: 读写模式, 0644: 文件权限) fd = open("newfile.txt", O_CREAT | O_RDWR, 0644); if (fd == -1) { perror("文件创建失败"); // 错误处理 return 1; } printf("文件创建成功! 文件描述符: %d\n", fd); // 写入内容文件 ssize_t bytes_written = write(fd, content, strlen(content)); if (bytes_written == -1) { perror("写入失败"); close(fd); return 1; } printf("成功写入 %zd 字节\n", bytes_written); close(fd); // 关闭文件 return 0; } ``` #### 关键步骤说明 1. **文件创建** `open()` 函数使用标志位组合: - `O_CREAT`:文件不存在时创建 - `O_RDWR`:以读写模式打开 - `0644`:权限设置(用户:读写,组和其他:只读) 2. **内容写入** `write()` 函数参数: - 文件描述符 `fd` - 数据缓冲区指针 `content` - 数据长度 `strlen(content)` 3. **错误处理** - 检查系统调用返回值(`-1`表示失败) - 使用 `perror()` 输出错误信息 #### 编译与运行 ```bash gcc create_file.c -o create_file # 编译 ./create_file # 运行 cat newfile.txt # 验证内容 ``` 输出结果: ``` 文件创建成功! 文件描述符: 3 成功写入 25 字节 Hello, Linux File System! ``` #### 注意事项 1. 文件权限 `0644` 对应权限位 `rw-r--r--` 2. 文件路径支持绝对路径(如`/home/user/file.txt`)或相对路径 3. 追加内容需添加 `O_APPEND` 标志[^1][^2] 4. 二进制文件写入需使用 `fwrite()` 函数[^3]
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值