[c++] 文件操作相关的bulabula

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

const int MAXN = 10000000;
const int MAXS = 6;//60*1024*1024;

int numbers[MAXN];
char buf[MAXS];

void fread_analyse()
{
    freopen("data.txt","rb",stdin);
    int len = fread(buf,1,MAXS,stdin);
    buf[len] = '\0';
    string str(buf,len);
    cout<<"str : "<<str<<endl;
}

#include <fstream>
void fun()
{
    ifstream fin;
    fin.open("data.txt",ios::in);
    string buf;
    while(!fin.eof())
    {   
        getline(fin,buf,'\n');
        if (buf.size() == 0)
            continue;
        cout<<"->"<<buf<<"<-"<<endl;
    }   
    return ;
}

#include <cstring>
void fun_c()
{
    char line[256];
    FILE *f = fopen("data.txt", "rb");

    int count = 0;                                                                                                                                                                                   
    while(!feof(f) && !ferror(f))
    {
        strcpy(line,"\n");
        fgets(line,sizeof(line),f);
        if(strlen(line) == 0 || (strlen(line) == 1 && line[0] == '\n') )
            continue;
        string buf(line);
        cout<<"->"<<buf<<"<-"<<endl;
        count++;
        cout<<count<<endl;
    }
    fclose(f);
}

int main()
{
    cerr<<"helloworld"<<endl;
//  fread_analyse();
    fun_c();
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/sszzyzzy/article/details/89204323