从包含数字和字符的文本文件中读取

时间:2014-02-10 20:18:28

标签: c++

我最近学会了如何从文本文件中读取数据,但我想继续扩展我对此问题的了解。我想阅读包含数字和字符的文件。有人可以给我一些建议吗?

以下是我为阅读数字而编写的代码:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {
    ifstream infile("input.txt", ios::in);

    if (!infile) {
        cout << "Inputxxxxx file could not be opened" << endl;
        exit(1);
    }

    ofstream outfile ("outputt.txt", ios::out);
    if (!outfile) {
        cout << "inputssss file could not be opened " <<endl;
        exit(1);
    }

    int number;
    int answer[10];

    for (int i = 0; i < 9 ; i++) {
        infile >> number;
        answer[i]=number;

        cout << answer[i] << " ";
        outfile <<answer[i]<< " ";
    }

    return 0;
}

0 个答案:

没有答案