如何从TXT文本中获取想要的信息?

supkychen 2009-12-11 03:57:52
在一个txt文本中,数据格式可能是按列摆布的,比如
one two three four ...
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
有什么办法能只获取其中的一列,比如我只获取其中的three列?
...全文
207 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZXW0521 2009-12-11
  • 打赏
  • 举报
回复
fgets 得到一行,
fscanf,读其中每个内容
赵4老师 2009-12-11
  • 打赏
  • 举报
回复
fseek
fscanf
gelu1040 2009-12-11
  • 打赏
  • 举报
回复
没有按列读,除非每行占用一样的空间
do_fork 2009-12-11
  • 打赏
  • 举报
回复
文本处理,脚本更方便

python方式
print [x.split()[2] for x in open('txt')]


awk方式
awk '{print $3}' txt


c++方式
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
int col=3;
fstream f("txt");
string line;
vector <string> words;
while (getline(f, line)) {
stringstream in(line);
string sel;
for (int i=0; i<col; i++)
in >> sel;
words.push_back(sel);
}
for (int i=0; i<words.size(); i++)
cout << words[i] << endl;
}
lgccaa 2009-12-11
  • 打赏
  • 举报
回复
一行一行的读取,拿到数据想怎么处理都行

有规律,想要哪列就找哪列就行啦
deng1243 2009-12-11
  • 打赏
  • 举报
回复

ifstream in("xx.txt");
for(string str; getline(in,str);)
mengde007 2009-12-11
  • 打赏
  • 举报
回复
一行行的读;计数;
chenheliang881 2009-12-11
  • 打赏
  • 举报
回复
对文件流 不怎么熟..
路过....

64,688

社区成员

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

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