C++读取文件中多列数据放入二维数组问题(新学C++,望好心人指导)

lixiaowei1234 2012-08-10 04:14:44
#include <iostream>
#include <fstream>
#include "iomanip"
#include <String>
#include <cstdlib>
#include <stdio.h>
using namespace std;
main()
{

ifstream infile;
ofstream outfile;

infile.open("G://1.txt");
float ft1;
infile>>ft1;float *p=&ft1;

if(!infile)
{
cout<<"读文件打开失败!";
exit(1);
}
cout<<"二维数组为:"<<"\n";
float data[5][4];int i,j;
while(*p!=NULL)

{
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{

data[i][j]=*p;

cout<<data[i][j]<<" ";
p++;

}


cout<<"\n";
}


}
infile.close;
outfile.close;
return 0;
}


...全文
243 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lixiaowei1234 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
#include <fstream>
#include "iomanip"
#include <String>
#include <cstdlib>
#include <stdio.h>
using namespace std;
main()
{

ifstream infile;
ofstream outfile;
infile.open("G://1.txt"……
[/Quote]


这个还是有问题,,,改了之后呢全输出1,行列可以正确
lixiaowei1234 2012-08-10
  • 打赏
  • 举报
回复
error C2100: illegal indirection
还是不行啊,,,肿么办?
lixiaowei1234 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
#include <iostream>
#include <fstream>
#include "iomanip"
#include <String>
#include <cstdlib>
#include <stdio.h>
using namespace std;
main()
{

ifstream infile;
ofstream outfile;

in……
[/Quote]
主要是从文件中读取200行4列的数,进行各种基本算法,再写入文件中去,我目前读数时对空格和换行不会把握。。。求大神指点。。。C++
俊炎 2012-08-10
  • 打赏
  • 举报
回复
#include <fstream>
#include "iomanip"
#include <String>
#include <cstdlib>
#include <stdio.h>
using namespace std;
main()
{

ifstream infile;
ofstream outfile;
infile.open("G://1.txt");
float ft1;
infile>>ft1;float *p=&ft1;
if(!infile)
{
cout<<"读文件打开失败!";
exit(1);
}
cout<<"二维数组为:"<<"\n";
float data[5][4];
int i,j;
int k = 0;
while(*p[0]!=NULL)
{
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{

data[i][j]=*p[0];
cout<<data[i][j]<<" ";
k++;
}
cout<<"\n";
}
}
infile.close;
outfile.close;
return 0;
}
试试这样还有没有问题
俊炎 2012-08-10
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include "iomanip"
#include <String>
#include <cstdlib>
#include <stdio.h>
using namespace std;
main()
{

ifstream infile;
ofstream outfile;

infile.open("G://1.txt");
float ft1;
infile>>ft1;float *p=&ft1;

if(!infile)
{
cout<<"读文件打开失败!";
exit(1);
}
cout<<"二维数组为:"<<"\n";
float data[5][4];int i,j;
while(*p!=NULL) //你这个判断会不会有问题?

{
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{

data[i][j]=*p;//这个也有问题,如果是第一次的话,应该是data[0][0] = *p[0];

cout<<data[i][j]<<" ";
p++;//这个好像也有问题

}


cout<<"\n";
}


}
infile.close;
outfile.close;
return 0;
}
俊炎 2012-08-10
  • 打赏
  • 举报
回复
数据类型不对呀,你的数据是float的,你用整形来读取了,你的是什么样的需求,具体说下看看

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧