-3

我正在尝试将文本文件读入整数数组,但我的读取不会影响任何内容,并且我的数组保持其默认值 0,我的代码如下

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream myfile;
    int a[1000] = {0};
    myfile.open ("Euler7.txt", ios::out | ios::app | ios::binary);

    for (int i=0;i<1000;i++)
    {
        myfile << a[i];
        cout << a[i] << endl;
    }

    myfile.close();

    int c = 0;
    for (int b=0;b>995;b++)
    {
        if (a[b]*a[b+1]*a[b+2]*a[b+3]*a[b+4] > c)
            c = a[b]*a[b+1]*a[b+2]*a[b+3]*a[b+4];
    }

    cout << c << a[0];

    return 0;
}

我怀疑我在某处某处需要一个 fin.ignore ,但是我对文件的技能可以达到 #include ,我试图打开的文件类似于

6717653133062491922511967442657474235534919493496983520312774506326239578318

数字之间没有空格或分隔符。我需要数组的每个项目来保存一个单独的数字,文件中有 1000 个数字。

4

2 回答 2

0

似乎根本不需要 fstream 技术来解决这个问题。特别感谢 Tacet 提供部分代码:

#include <iostream>
#include <string>
using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{
  int pause;
  string series="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
  unsigned int max = 0; //if it can not be less than zero, use a unsigned type
  const unsigned int end = series.size()-4;
  for (int i=0;i<end;i++)
  if ((series[i]-'0')*(series[i+1]-'0')*(series[i+2]-'0')*(series[i+3]-'0')*(series[i+4]-'0') > max)
max = (series[i]-'0')*(series[i+1]-'0')*(series[i+2]-'0')*(series[i+3]-'0')*(series[i+4]-'0');
  cout << max << '\n';


    cin >> pause;
    return 0;
}
于 2013-09-18T17:54:31.100 回答
0

您的文件流对象是一个ofstream,用于写入文件。相反,您想要ifstream,它用于输入。您还需要在对 的调用中去掉ios::out和标志。此外,您正在使用用于输出而不是输入的运算符。ios::appopen<<>>

于 2013-05-07T21:27:12.813 回答