【问题标题】:Fstream class - issue about' >> ' operatorFstream 类 - 关于'>>'操作符的问题
【发布时间】:2013-02-27 06:21:48
【问题描述】:

一个简单的程序,从 .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]' 

有什么想法吗?

【问题讨论】:

  • 与输出流和输入流的区别有关。

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


【解决方案1】:

ofstream 是一个输出文件流;您无法使用 &gt;&gt; 读取它。

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

【讨论】:

    【解决方案2】:

    std::ofstream 是一个输出流,所以它没有输入流操作符>>。如果要流式传输到文件中,则需要std::ifstream

    【讨论】:

      猜你喜欢
      • 2021-07-17
      • 2021-12-19
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多