我正在尝试将文本文件读入整数数组,但我的读取不会影响任何内容,我的数组保持默认值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个数字。
答案 0 :(得分:0)
您的文件流对象是ofstream
,用于写入文件。相反,您需要ifstream
,这是输入。您还希望删除ios::out
调用中的ios::app
和open
标记。此外,您使用的是运算符<<
,而不是>>
用于输入。
答案 1 :(得分: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;
}