• c++中文件读取


    对于C++编译运行文件,我使用过两个编译器,一个是visual studio 2013,另外一个是devcpp,推荐使用devcpp。

    vs的特点是界面整洁清晰,但是需要收费,这是微软的,并且在电脑上的运行速度是非常慢的; 而devcpp的特点是界面比较简陋,但是该软件免费下载,运行速度快,软件响应快。

    例0.0:

    这里介绍cin和cout对象的方法: cin.get()以及cout.put(),前者是将用户的输入获取,返回获取到的一个字符; 后者接受一个参数,返回值就是在控制台输出这个字符。如下所示:

    #include <iostream>
    using namespace std;
    int main()
    {
        char c;
        cout << "Input the words: " << endl;
        while ((c= cin.get()) != EOF)
        {
            cout.put(c);
        }
        cout << "That's the end!" << endl;
        return 0;
    }

    这里就是将用户输入的依次获取,并且输出,知道判断为EOF,则停止程序。结果如下:

    例0.1

    这里在介绍getline函数和read函数。

    其中 getline函数的作用是从输入流中读取一行字符,其用法和上面将的get函数输入一行字符的功能类似,如cin.getline();

    read函数可以从输入流中读取指定书目的字符并将他们存放在指定的数组当中,格式如下: 

    cin.read(char *buf, int size);

    eof函数是end of file的缩写,表示文件结束。 如果读取数据到达末尾,eof函数值为真,表示遇到文件结束符;eof函数值为假,表示没有遇到文件结束符。

    另外,peek函数的作用是从输入流中返回下一个字符,但他只是观测,指针仍然停留在当前位置,遇到流结束标志时返回EOF。 调用形式为: c= cin.peek(); 

    而cin.putback函数作用是将前面用get或者getline函数从输入流中读取的字符ch返回到输入流,插入到当前指针位置,供后面读取。 

    例1:

    如下所示,首先创建一个文件流对象,接着打开文件,进行输入,最后关闭。 读取文件内容与之相似。

    #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
        cout << "this is the begining!" << endl;
        int n;
        double d;
        ofstream outfile;
        outfile.open("C:\Users\Administrator\Desktop\fileTest\fileTest\test.txt", ios::out);
        //outfile.open("./test.txt", ios::out);
        outfile << 50 << endl;
        outfile << 3.8 << endl;
        outfile.close();
        cout << "successful!" << endl;
        ifstream infile("C:\Users\Administrator\Desktop\fileTest\fileTest\test.txt", ios::in);
        //ifstream infile("./test.txt", ios::in);
        infile >> n >> d;
        cout << n << "," << d << endl;
        infile.close();
        return 0;
    }

    这里绝对路径使用“\”是转义后的结果,相对路径暂未成功。

    程序执行完毕,可发现内容为50 3.8,成功输入。

    例2

    #include <iostream>
    #include <fstream>
    
    using namespace std;
    int main()
    {
        cout << "this is the beginning!" << endl;
        fstream outfile;
        outfile.open("C:\Users\Administrator\Desktop\fileTest\fileTest\test1.txt", ios::out);
        if (!outfile) 
        {
            cout << "open failed!" << endl;
            exit(1);
        }
        outfile << "Hello world! 
    ";
        outfile << "github ours github! 
    ";
        outfile.close();
        return 0;
    }

    如果打开文件失败,outfile返回0,则 exit(1)退出程序。

    另外,注意 ios::out 这个实参必填。最后要 close 即可。

    且如果没有该文件可打开,则会自动创建文件,并完成后续输入。

    例3

    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        fstream outfile;
        outfile.open("C:\Users\Administrator\Desktop\fileTest\fileTest\test2.txt", ios::out);
        if (!outfile)
        {
            cerr << "open out failed !" << endl;
            exit(1);
        }
        outfile << "Hello world! 
    ";
        outfile << "github! 
    ";
        outfile.close();
        
        char s[100];
        fstream infile;
        infile.open("C:\Users\Administrator\Desktop\fileTest\fileTest\test2.txt", ios::in);
        if (!infile)
        {
            cerr << "open failed!" << endl;
            exit(1);
        }
        while (!infile.eof())
        {
            infile.getline(s, sizeof(s));
            cout << s << endl;
        }
        infile.close();
        return 0;
    }

    如上所示:我们先通过outfile输入内容,然后使用infile读取磁盘内容,infile.eof() 中eof即end of file,即如果不是file的最后一行,就继续读取文件,并且输出。且每次在操作时,无论读取,都需要进行打开文件已经关闭文件的操作。

  • 相关阅读:
    Delphi中WebBrowser控件打开部分网站报"Invalid floating point operation”解决
    delphi中URL的汉字编码
    python手记(11)
    李维对VCL理解的几个错误
    如何配置MYSQL的MASTER---SLAVE复制备份?
    UniDAC连接Embedded MySQL server
    unigui组件中client javascript delphi组件之间的操作
    Delphi中SendMessage使用说明(所有消息说明) good
    iOS面试题
    Windows的MAX_PATH
  • 原文地址:https://www.cnblogs.com/zhuzhenwei918/p/8565478.html
Copyright © 2020-2023  润新知