Fstream课程 - 关于'>>的问题'运营商

时间:2013-03-11 21:48:57

标签: c++ arrays stream text-files fstream

一个简单的程序,它使用.txt文件中的int数字创建数组,看起来像

2 3
5 7
4 2
y x
y x
...

所以它是简单的nx2(其中n可以是无限的行)。然后使用该数组填充新文件(稍后我将添加代码以使用一些有趣的算法编辑此数组)。

我写过:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(){
    ofstream outFile;
    ofstream fout;
    fout.open("krol.txt");
    int l=0;
    int i=2;
    char ch;
    while (fout.good()){
        if (fout >> ch=='\n') l++;
    }
    fout.close();
    fout.open("krol.txt");
    int temp[l][2];
    int savel=l;
    l=0;
        while (fout >> (temp[l][i])){
        i++;
        if(i==2){ 
         i=0; l++;
        }
    }
    outFile.open("save.txt");
    for (int i=0, j=0;j<savel;i++){
        if (i==2) {
        i=0; j++;
        }
        outFile << temp[j][i];
    }
    system("PAUSE");
    return 0;
    }

但它返回:

13 15 C:\Users\Filip\Dysk Google\Infa\krol.cpp [Error] no match for 'operator>>' in 'fout >> ch' 

20 29 C:\Users\Filip\Dysk Google\Infa\krol.cpp [Error] no match for 'operator>>' in 'fout >> temp[l][i]' 

任何想法?

2 个答案:

答案 0 :(得分:4)

ofstream输出文件流;你无法使用>>读取它。

而不是ofstream fout;我怀疑你想要ifstream fin;

答案 1 :(得分:1)

std::ofstream是输出流,因此它没有输入流运算符&gt;&gt;。如果要流式传输到文件,则需要std::ifstream

相关问题