文件读写

来源:互联网 发布:逆战免费刷枪软件 编辑:程序博客网 时间:2024/06/05 22:50

简单的文件读写,发现我的代码能力实在是太弱了,我需要勤加练习呀。
文件读写用到的函数是
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;}

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

0 0