C++ 怎么从txt文件需要的位置开始读取数据,并存入数组

adnana 2011-11-16 04:19:06
一个txt文档,前面部分是关于设置的,不需要读取,要读取的是在中间部分是输出结果,要做的是把一行里的5个数据存入不同的数组中,求高手指点
Step Time XX0001 XX0008 XX0009 XX0010

0 0.0 0.0 8284.51544 -4164.7537 -4119.7617
500 .25E-3 .182949848 8261.02951 -3589.5704 -4670.9102
1000 .5E-3 .759112568 8186.71113 -2991.8512 -5192.5825
1500 .75E-3 1.72350195 8062.20225 -2375.4681 -5681.5635
2000 .1E-2 3.06911774 7888.49492 -1744.336 -6134.9514
2500 .00125 4.80059818 7666.83464 -1102.3032 -6550.1294
3000 .0015 6.9776973 7398.65209 -453.017 -6924.7017
3500 .00175 9.48312771 7085.54949 199.505165 -7256.605
4000 .002 11.808714 6729.23348 851.116104 -7544.9232
4500 .00225 13.7718778 6331.47043 1497.51633 -7787.6709
5000 .0025 15.3715757 5894.17974 2134.59477 -7982.6596
5500 .00275 16.6155607 5419.61819 2758.2452 -8128.0166
...全文
520 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
动感超哥 2011-11-16
  • 打赏
  • 举报
回复
用偏移量 也可以撒
hongwenjun 2011-11-16
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>

using namespace std;

int main()
{
stringstream oss("Step Time XX0001 XX0008 XX0009 XX0010 \n"
"1 2 3 4 5 6 \n"
"1 2 3 4 5 6 \n"
"1 2 3 4 5 6 \n"
"1 2 3 4 5 6 \n"
"1 2 3 4 5 6 \n"

);
string Title; // 标题行
int step; // 步数
double data[5]; // 装载数据
int cnt = 0; // 数据计数

while (getline(oss, Title) && (Title.find("Step") != string::npos))
; // 跳过开头标题

while (oss) { // 读取一行数据
oss >> step;
oss >> data[0] >> data[1] >> data[2]
>> data[3] >> data[4] ;
cnt++;
}
// 测试一行
cout << data[0] << data[1] << data[2]
<< data[3] << data[4] ;
return 0;
}
柯大侠爱喝水 2011-11-16
  • 打赏
  • 举报
回复
具体转化自己实现,c中有函数的
柯大侠爱喝水 2011-11-16
  • 打赏
  • 举报
回复
1:循环读fscanf()出每一行

2:把其存入char数组,用strstr()找空格,截取出五个数字串

3:依次把五个数值串找出来,再转为数值存入你说的数组
zzcmx2008 2011-11-16
  • 打赏
  • 举报
回复
ifstream打开文件,
循环getline读取每一行的内容,进行提取,知道getline返回失败
hongwenjun 2011-11-16
  • 打赏
  • 举报
回复
没注意看错题目了。

可以 设置一个 数据开始标记
比如 #date_start
然后从这个接一下来的一行才开始输出数据
hongwenjun 2011-11-16
  • 打赏
  • 举报
回复

// readfile.cpp

#include <wx/textfile.h>

int main(int argc, char **argv)
{

wxTextFile file(wxT("test.c"));

file.Open();

wxPrintf(wxT("Number of lines: %d\n"), file.GetLineCount());
wxPrintf(wxT("First line: %s\n"), file.GetFirstLine().c_str());
wxPrintf(wxT("Last line: %s\n"), file.GetLastLine().c_str());

wxPuts(wxT("-------------------------------------"));

wxString s;

for ( s = file.GetFirstLine(); !file.Eof();
s = file.GetNextLine() )
{
wxPuts(s);
}

file.Close();
}


使用 wxTextFile 库,可以自由输出 文本文件里的任意行

Output

Number of lines: 8
First line: #include
Last line: }
-------------------------------------
#include <glib.h>
#include <glib/gstdio.h>

int main() {

g_mkdir("/home/vronskij/test", S_IRWXU);

}
a707000646 2011-11-16
  • 打赏
  • 举报
回复
不好意思楼主
按照你的问题
那应该是用循环,然后用fscanf加站位字符串去读取
turing-complete 2011-11-16
  • 打赏
  • 举报
回复
内存映射
a707000646 2011-11-16
  • 打赏
  • 举报
回复
int fseek( FILE *stream, long offset, int origin );
Parameters
stream
Pointer to FILE structure
offset
Number of bytes from origin
origin
Initial position
Libraries
All versions of the C run-time libraries.

Return Values
If successful, fseek returns 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined.

Remarks
The fseek function moves the file pointer (if any) associated with stream to a new location that is offset bytes from origin. The next operation on the stream takes place at the new location. On a stream open for update, the next operation can be either a read or a write. The argument origin must be one of the following constants, defined in Stdio.h:

SEEK_CUR
Current position of file pointer
SEEK_END
End of file
SEEK_SET
Beginning of file

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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