文件操作的对比(C语言、C++、管道)

C语言文件操作

#include<stdio.h>

int main(int argc,char* argv[]){
    char s[40];
    //scanf("%s",&s);//可以从文件读也可以从终端读
    //printf("%s\n",s);//可以写到文件,也可以写到终端
    FILE* pfile = fopen(argv[1],"r");
    if(NULL==pfile){
       printf("open file %s error\n",argv[1]);
       return 1;
    }
    //fscanf(pfile,"%s",&s);//把pfile这个文件指针类型里的argv[1]这个文件内容输入到s里
    //fprintf(stdout,"%s\n",&s);//输出到终端

    fgets(s,sizeof(s),pfile);//可以把全部文件的所有内容(不受空格限制)输入到s里,按s的大小
    fclose(pfile);
    pfile=NULL;
    printf("%s\n",s);

    FILE* pfile2 = fopen(argv[1],"w");//以可写打开fopen
    if(NULL==pfile2){
       printf("open file %s error\n",argv[1]);
       return 1;
    }
    scanf("%s",&s);
    fprintf(pfile2,"%s\n",s);//把s中的数据把输出到pfile2指向的文件中中
    fclose(pfile2);
    pfile2 = NULL;
}
                                              

C++文件操作

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(){
    string s;
    cin >> s;

    ofstream ofs("./test",ios::app);
    if(ofs){
        ofs << s;
        ofs.close();
    }
  
    ifstream ifs("./test");
    string t;
    if(ifs){
       ifs >> t;
       ifs.close();
    }
    cout << t << endl;
}
                                              
//cout/cin   printf()/scanf()
//ostringstream/istringstream   sprintf()/sscanf()
//ofstream/ifstream   fprintf()/fscanf()

管道

匿名管道

单工管道

#include <iostream>
#include <cstdlib>
using namespace std;

int main(){
    FILE* pf = popen("./output","r");
    if(NULL != pf){
        char buff[30] = {"\0"};
        fread(buff,1,sizeof(buff),pf);
        cout << "read: " << buff << endl;
        pclose(pf);
        pf = NULL;
    }
}

#include <iostream>
#include <cstdlib>
using namespace std;

int main(){
    FILE* pf = popen("./output","w");
    if(NULL != pf){
        char buff[] = "abjckjnada12345666";
        fwrite(buff,1,sizeof(buff),pf);
        pclose(pf);
        pf = NULL;
    }
}

半双工管道

一边发 一边收

#include <iostream>
using namespace std;
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
int main(){
    int fd[2];
    pipe(fd);
    cout << getpid() << endl;
    if(0 == fork()){
         cout << getpid() << ":";
         string s;
         cin >> s;
         write(fd[1],s.c_str(),s.size()+1);
    }else{
         fcntl(fd[0],F_SETFL,O_NONBLOCK);
         char buff[30] = {'\0'};
         while(-1 == read(fd[0],buff,sizeof(buff))){
             sleep(1);
             cout << "wait..." << endl;
         }
         cout << getpid() << ":" << buff << endl;
    }
    close(fd[0]);
    close(fd[1]);
}

两边都可以收发 (非同时)

#include <iostream>
using namespace std;
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
int main(){
    int fd1[2];
    int fd2[2];
    pipe(fd1);
    pipe(fd2);

    cout << getpid() << endl;
    if(0 == fork()){
        for(;;){
         cout << getpid() << ":";
         string s;
         cin >> s;
         write(fd1[1],s.c_str(),s.size()+1);

         char buff[30] = {'\0'};
         read(fd2[0],buff,sizeof(buff));
         cout << getpid() << ":" << buff << endl;
        }
    }else{
        for(;;){
         //fcntl(fd[0],F_SETFL,O_NONBLOCK);
         char buff[30] = {'\0'};
         while(-1 == read(fd1[0],buff,sizeof(buff))){
             sleep(1);
             cout << "wait..." << endl;
         }
         cout << getpid() << ":" << buff << endl;
         cout << getpid() << ":";
         string s;
         cin >> s;
         write(fd2[1],s.c_str(),s.size()+1);
        }
    }

命名管道

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值