#include "unistd.h"
#include "fcntl.h"
#include <iostream>
using namespace std;
int main()
{
int fd = open("test.txt",O_RDWR | O_CREAT | O_APPEND);
if(write(fd,"test",4)!=4){
cout << "error" << endl;
}
close(fd);
int newFd = dup(1);
cout << "当前系统最小的fd:" << newFd << endl;
}
当前系统最小的fd:3
写文件
FILE* file_ptr = fopen("a.txt","wb");
if(file_ptr==NULL){
return;
}
fprintf(file_ptr,"hello world\n");
fflush(file_ptr);
fclose(file_ptr);
读文件
fstream fs("b.txt", ios::in);
if(!fs){
return;
}
string line;
while (getline(fs, line))
{
//
}
fs.close();