zoukankan      html  css  js  c++  java
  • 读取文本文件

    #include <iostream>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fstream>
    
    using namespace std;
    
    void dofile(char *filename)
    {
        FILE *f;long len;char *data;
    
        f=fopen(filename,"rb");
        fseek(f,0,SEEK_END);
        len=ftell(f);
        fseek(f,0,SEEK_SET);
        data=(char*)malloc(len+1);
        fread(data,1,len,f);
        fclose(f);
    
        cout << data << endl;
        free(data);
    }
    
    void dofile2(char *filename)
    {
        ifstream input(filename);
        if(!input){
            cout << "open file error" << endl;
        }
    #if 1
        string s;
        string str;
        while(input >> s)
            str += s;
    
        cout << str << endl;
    #else
        char buf[1024];
        char c;
        int n = 0;
        while(input >> c){
            sprintf(buf+n,"%c",c);
            ++n;
        }
    
        cout << buf << endl;
    #endif
    }
    
    int main()
    {
        cout << "Hello World!" << endl;
    
    
    //    dofile("./test1.txt");
    //    dofile2("./test1.txt");
    
    //    dofile("./test2.json");
    //    dofile2("./test2.json");
    //    dofile2("./test3.json");
        dofile2("./test4");
    
    
    
        return 0;
    }
  • 相关阅读:
    抽象工厂模式
    工厂方法模式
    单例模式
    适配器模式
    外观模式
    简单工厂模式
    设计模式开篇闲谈
    android ui更新
    android获取Context
    android 事件绑定
  • 原文地址:https://www.cnblogs.com/nanqiang/p/13166436.html
Copyright © 2011-2022 走看看