刚学习C++与C混合用,在从文件读一个多行多列的数据并写入一个二维数组中时,前面读的数对,可后面出现-842150451,这是什么情况,大姐,帮帮忙哦
刚学习C++与C混合用,在从文件读一个多行多列的数据并写入一个二维数组中时,前面读的数对,可后面出现-842150451,这是什么情况,各位大哥大姐,帮帮忙哦
#include <iostream>
#include <fstream>
#include "iomanip"
#include <String>
#include <cstdlib>
#include <stdio.h>
using namespace std;
int main(int argc, char* argv[])
{
ifstream infile;
ofstream outfile;
int r=0;int row,col;int i,j;
infile.open("G://10.txt",ios_base::in);
if(!infile)
{
cout<<"读文件打开失败!";
exit(1);
}
char *p=new char[40];infile.getline(p,40);
sscanf(p,"%d%d",&row,&col);
int** data=new int *[row];
for(i=0;i<row;i++)
{
data[i]=new int[col];
}
while(!infile.eof()&r<=row)
{
infile.getline(p,40);
sscanf(p,"%d %d %d %d",&data[r][0],&data[r][1],&data[r][2],&data[r][3]);
r++;
}
cout<<"二维数组为:"<<"\n";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<data[i][j];cout<<" ";
}
cout<<"\n";
}
infile.close;
outfile.close;
return 0;
}
运行结果(VC6.0){16 17 18 19 -842150451 -842150451
-842150451 -842150451 -842150451 -842150451 -842150451
20 21 22 23 -842150451 -842150451 -842150451 -842150451 -842150451等等
跪求大神帮忙!!!
------解决方案--------------------
你使用过C,应该知道调试一说吧.
下个断点, 一步步跟踪就好了
#include <iostream>
#include <fstream>
#include "iomanip"
#include <String>
#include <cstdlib>
#include <stdio.h>
using namespace std;
int main(int argc, char* argv[])
{
ifstream infile;
ofstream outfile;
int r=0;int row,col;int i,j;
infile.open("G://10.txt",ios_base::in);
if(!infile)
{
cout<<"读文件打开失败!";
exit(1);
}
char *p=new char[40];infile.getline(p,40);
sscanf(p,"%d%d",&row,&col);
int** data=new int *[row];
for(i=0;i<row;i++)
{
data[i]=new int[col];
}
while(!infile.eof()&r<=row)
{
infile.getline(p,40);
sscanf(p,"%d %d %d %d",&data[r][0],&data[r][1],&data[r][2],&data[r][3]);
r++;
}
cout<<"二维数组为:"<<"\n";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<data[i][j];cout<<" ";
}
cout<<"\n";
}
infile.close;
outfile.close;
return 0;
}
运行结果(VC6.0){16 17 18 19 -842150451 -842150451
-842150451 -842150451 -842150451 -842150451 -842150451
20 21 22 23 -842150451 -842150451 -842150451 -842150451 -842150451等等
跪求大神帮忙!!!
------解决方案--------------------
你使用过C,应该知道调试一说吧.
下个断点, 一步步跟踪就好了