关于C++读取文件

来源:百度知道 编辑:UC知道 时间:2024/05/26 12:06:20
我有一个解集包含一些可行解,我想从这些可行解里面找到满足-x2+x3-x5-x7+x10+x12==2这个方程的所有解。我想从txt文件中读取这些可行解,然后把求出来的满足方程的解输出到output文件中。我自己编写的程序如下,但是编译不通过,请教高手帮我修改一下。我觉得是读取文件那里出了问题,但是我自己不知道改怎么修改,谢谢大家了
# include <iostream>
# include <fstream>
using namespace std;

int main()
{
int i,j,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12;
int aa[5000][20];
ifstream infile("input.txt");
if(infile == NULL)
{
cerr << "Can't read input.txt!" << endl;
return false;
}
infile.read (int aa[i][j]);
for (i=0;i<5000;i++)
{
x1=aa[i][0];
x2=aa[i][1];
x3=aa[i][2];
x4=aa[i][3];
x5=aa[i][4];
x6=aa[i][5];
x7=aa[i][6];
x8=aa[i][7];
x9=aa[i][8];
x10=aa[i][9];
x11=aa[i][10];
x12=aa[i][11];
if (-x2+x3-x5-x7+x10+x12==2)
{
ofstream outfile("output.txt",ios::binary);
if (! outfile)

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

int main()
{
int x[13];
ifstream infile("input.txt");
ofstream outfile("output.txt",ofstream::app);
if(infile == NULL)
{
cerr << "Can't read input.txt!" << endl;
return false;
}
while(infile)
{
for(int j = 1; j <= 12; j++)
{
infile >> x[j];
}
if (-x[2]+x[3]-x[5]-x[7]+x[10]+x[12]==2)
{
if (! outfile)
{
cerr<<"open error!"<<endl;
return false;
}
for (int k = 1; k <= 12;k++)
outfile << x[k] << " ";
outfile << endl;
}
}
infile.close();
outfile.close();
return 0;
}
调试运行成功!
可以不定义数组的,那样效率高些,也更优雅!
以是否到infile的文件末尾做循环条件更好!
你给的数据没一个等于