关于一文本文件中取数的问题???急

xiaoling9 2003-10-19 02:34:50
0001>  -5 10 0 0 0 387 76
0002> 12154 30080 773 0 0 925 0
0003> 12166  30101 841 0 92 921 4
有这样一个文本文件,要求把第六列的数提取出来,我用C语言的文件操作把这个文件的每一行读出,刚开始我以为这个文件文件中的每一列数据都能读到一个固定的下标处,比如第二列,如果算上空格后都可以从下标为九的数组开始读取,但做过后,发现此文本文件中的空白出没有空格存在,所以在读每列数时不能以固定的下标来读出,请教各位,在这种情况下,我怎么在不破坏原文件格式的基础上把第六列数据读取出来。谢谢大家帮忙!
...全文
38 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
limd 2003-10-20
  • 打赏
  • 举报
回复
UP
winco 2003-10-20
  • 打赏
  • 举报
回复
1 > -5 10 0 0 0 387 76
2 > 12154 30080 773 0 0 925 0
3 > 12166 30101 841 0 92 921 4
4 > 12286 29409 1051 0 26 925 0
5 > 10933 27698 1046 0 60 925 0
6 > 12427 26968 1303 0 68 922 3
7 > 10642 25787 827 0 89 921 4
8 > 12485 25347 1353 0 2 924 1
9 > 10048 22515 904 0 68 925 0
10 > 10243 22209 1302 0 28 924 1
10 > 10243 22209 1302 0 28 924 1
Press any key to continue
结果会多输出一行,但是你如果知道行数,用while(i++<10)也可以。
开始的问题我也不知道为什么。
但是改用标准名空间的<iostream>、<fstream>和<iomanip>就没有上述问题了
winco 2003-10-20
  • 打赏
  • 举报
回复
#include "iostream"
#include "fstream"
#include "iomanip"
using namespace std;
void main()
{
fstream file;
file.open("read.txt", ios::in);
int a0, a1, a2, a3, a4, a5, a6, a7;
a0=a1=a2=a3=a4=a5=a6=a7=0;
char b='\0';
int i=0;
while(file.good())
{
file>>a0>>b>>a1>>a2>>a3>>a4>>a5>>a6>>a7;
cout<<setw(6)
<<a0<<" "
<<setw(6)
<<b<<" "
<<setw(6)
<<a1<<" "
<<setw(6)
<<a2<<" "
<<setw(6)
<<a3<<" "
<<setw(6)
<<a4<<" "
<<setw(6)
<<a5<<" "
<<setw(6)
<<a6<<" "
<<setw(6)
<<a7<<" "
<<endl;
}
file.close();
}
xiaoling9 2003-10-20
  • 打赏
  • 举报
回复
0001> -5 10 0 0 0 387 76
0002> 12154 30080 773 0 0 925 0
0003> 12166 30101 841 0 92 921 4
0004> 12286 29409 1051 0 26 925 0
0005> 10933 27698 1046 0 60 925 0
0006> 12427 26968 1303 0 68 922 3
0007> 10642 25787 827 0 89 921 4
0008> 12485 25347 1353 0 2 924 1
0009> 10048 22515 904 0 68 925 0
0010> 10243 22209 1302 0 28 924 1
我有winco的代码,当while(i++<10)时,输出为
1 > -5 10 0 0 0 387 76
2 > 12154 30080 773 0 0 925 0
3 > 12166 30101 841 0 92 921 4
4 > 12286 29409 1051 0 26 925 0
5 > 10933 27698 1046 0 60 925 0
6 > 12427 26968 1303 0 68 922 3
7 > 10642 25787 827 0 89 921 4
0 8 10642 25787 827 0 89 921 4
0 8 10642 25787 827 0 89 921 4

也就是到了第七行后的所有行全部是同第七行同样的输出,请问这是为什么?怎样解决这个问题???
谢谢!
xiaoling9 2003-10-19
  • 打赏
  • 举报
回复
请回用C可不可以取出呀?
winco 2003-10-19
  • 打赏
  • 举报
回复
//read.cpp
#include "iostream.h"
#include "fstream.h"
#include "iomanip.h"
void main()
{
fstream file;
file.open("read.txt", ios::in);
int a0, a1, a2, a3, a4, a5, a6, a7;
char b;
int i=0;
while(i++<3)
{
file>>a0>>b>>a1>>a2>>a3>>a4>>a5>>a6>>a7;
cout<<setw(6)
<<a0<<" "
<<setw(6)
<<b<<" "
<<setw(6)
<<a1<<" "
<<setw(6)
<<a2<<" "
<<setw(6)
<<a3<<" "
<<setw(6)
<<a4<<" "
<<setw(6)
<<a5<<" "
<<setw(6)
<<a6<<" "
<<setw(6)
<<a7<<" "
<<endl;
}
file.close();
}
OutPut:
1 > -5 10 0 0 0 387 76
2 > 12154 30080 773 0 0 925 0
3 > 12166 30101 841 0 92 921 4
Press any key to continue
xiaoling9 2003-10-19
  • 打赏
  • 举报
回复
我想了很长时间也解决,还请大家帮帮忙,有没有什么好的办法?
tonyzhengjq 2003-10-19
  • 打赏
  • 举报
回复
有难度,学习一下!
panzhaoping 2003-10-19
  • 打赏
  • 举报
回复
建议你用c++中的流
hslinux 2003-10-19
  • 打赏
  • 举报
回复
崩溃~~~~~~~~~~~自己慢慢写~~~~~~

读一字符~~判断是=/-,或是数字还是别的~~,遇空格结束,第一个非空格字符开始~~~~~把读到的字符在用个函数变做一个数值~~~~~~~~~

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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