文件读写

简单的文件读写,发现我的代码能力实在是太弱了,我需要勤加练习呀。
文件读写用到的函数是
fgets(char[], int len, FILE*)

#include <fstream>
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <stdio.h>

int main(int argc, char* argv[])
{
    if(argc < 3)
        printf("Usage :  ./filecopy infile outfile\n");
    //std::string str1 = std::string(argv[1]);
    //std::string str2 = std::string(argv[2]);
    FILE * file_in;
    FILE * file_out;
    file_in = fopen(argv[1], "rb");
    file_out = fopen(argv[2], "wb");
    if(NULL == file_in || NULL == file_out)
        printf("open file failure\n");
    const int LEN = 10240;
    char line[LEN];
    int len = 0;
    while(fgets(line, LEN, file_in))
    {   
        len = strlen(line);
        if('\n' == line[len - 1]) 
            line[len - 1] = '\0';
        fprintf(file_out, "%s\n", line);
    }   

    fclose(file_in);
    fclose(file_out);
    return 0;

}

我还有很长的路要走,期望我自己能够坚持下去

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值