当前位置: 首页> 娱乐> 影视 > 用友财务软件_中国排名第一的策划公司_学计算机哪个培训机构好_比较靠谱的电商培训机构

用友财务软件_中国排名第一的策划公司_学计算机哪个培训机构好_比较靠谱的电商培训机构

时间:2025/8/23 9:29:18来源:https://blog.csdn.net/qq_60098634/article/details/142624117 浏览次数:0次
用友财务软件_中国排名第一的策划公司_学计算机哪个培训机构好_比较靠谱的电商培训机构

341、基于 Linux 的文件操作
一、demo3.cpp
// demo3.cpp,本程序演示了 Linux 底层文件的操作-创建文件并写入数据。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
int main()
{
int fd; // 定义一个文件描述符/文件句柄。
// 打开文件,注意,如果创建后的文件没有权限,可以手工授权 chmod 777 data.txt。
fd=open("data.txt",O_CREAT|O_RDWR|O_TRUNC);
if (fd==-1)
{
perror("open(data.txt)"); return -1;
}
printf("文件描述符 fd=%d\n",fd);
char buffer[1024];
strcpy(buffer,"我是一只傻傻鸟。\n");
if (write(fd,buffer,strlen(buffer))==-1) // 把数据写入文件。
{
perror("write()"); return -1;
}
close(fd); // 关闭文件。
}
二、demo4.cpp
// demo4.cpp,本程序演示了 Linux 底层文件的操作-读取文件。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd; // 定义一个文件描述符/文件句柄。
fd=open("data.txt",O_RDONLY); // 打开文件。
if (fd==-1)
{
perror("open(data.txt)"); return -1;
}
printf("文件描述符 fd=%d\n",fd);
char buffer[1024];
memset(buffer,0,sizeof(buffer));
if (read(fd,buffer,sizeof(buffer))==-1) // 从文件中读取数据。
{
perror("write()"); return -1;
}
printf("%s",buffer);
close(fd); // 关闭文件。
}
 

关键字:用友财务软件_中国排名第一的策划公司_学计算机哪个培训机构好_比较靠谱的电商培训机构

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: