C/C++读写文件

//C语言写文件
void write(){
    char ch[]={"abvjdkfljdancdkjfdaknmvkjfads1234568970fjkdlajf;angkjfjdanjkhfiou435jfg0309uhjfghjhfdjabndasjkvgfjgh\n"};
    FILE* fp= fopen("text.txt","w");
    if(fp)
    {
        int len=sizeof (ch)-1;
        for(int i=0;i<1000000;++i)
        {
            fwrite(ch,len,1,fp);
        }
        fclose(fp);
    }
}
//                FILE* fp = fopen("data.mp4", "ab");
//                fwrite(buffer, 1, dateLen, fp);
//                fclose(fp);
//C语言写文件
void read()
{
     //FILE *fp1 = fopen("/home/ubuntu/work/oos/testDemo/unnamed.jpg", "rb");
    char *bufftotal = (char *) malloc(size);//new char[1000*10*1000];
    int count = 0, i = 0, totalCount = 0;
    if (fp1) {
        char buff[1000] = {'\0'};
        while ((count = fread(buff, 1, 1000, fp1)) != 0) {
            memcpy(bufftotal + i * 1000, buff, count);
            ++i;
            totalCount += count;
        }
    } else {
        return 0;
    }
}
    
//C++读文件
void read(std::string filePath){
    std::ifstream in(filePath.c_str());
    if(!in) {
        return false;
    }
    std::string line;
    std::string jsonConfig;
    while(getline(in, line))
    {
        jsonConfig.append(line);
    }
    in.close();
}
//C++写文件
void Write(){
	std::ofstream out("storageIndex.txt",std::ios::app);
	if(out){
	    for(int i=1000;i<1956;++i){
	        out<<i<<std::endl;
	    }
	    out.close();
	}
	std::ifstream in("storageIndex.txt");
	std::string getData;
	while(getline(in,getData)){
	    int n=std::atoi(getData.c_str());
	    std::cout<<getData<<std::endl;
	}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值