读写格式文档的问题

sunnysmile 2006-01-09 04:27:00
现我有一个文件,内容如下:
0123456789ABCDEF-1-13102005-13102006-36985-1
0383029D00E56002-2-13102005-13102006-36985-2
0123456789ABCDEF-13-13102005-13102006-36985-34
0383029D00E56002-125-13102005-13102006-36985-124
.
.
.
0123456789ABCDEF-87-13102005-13102006-36985-1234
0383029D00E56002-3-13102005-13102006-36985-56

一共六个字段,基中第2第6个字段长度不定,现在要把每个记录的内容读出来使用,然后把结果写回去,第2第6个字段的长度可能会改变,现在不知道怎么读.
每一条记录后有 0x0D 0x0A 回车换行
...全文
191 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cunsh 2006-01-10
  • 打赏
  • 举报
回复
也可以给getline指定第三个参数为'-'来一部分一部分的读取.
jixingzhong 2006-01-09
  • 打赏
  • 举报
回复
如果感觉麻烦,
用 sscanf() 也可以实现 ...
jixingzhong 2006-01-09
  • 打赏
  • 举报
回复
拆分用 strtok()
jixingzhong 2006-01-09
  • 打赏
  • 举报
回复
按行读取啊 ...

getline()就是了 ....

然后在拆分处理 ...
iwantfat 2006-01-09
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <stddef.h>
#include <vector>
using namespace std;

int main()
{

ifstream infile1("001.txt");
if(!infile1)
{
cerr<<"error"<<endl;
}
string out_string="002.txt";
ofstream outfile(out_string.c_str());
if(!outfile)
cerr<<"error"<<endl;
string buffer1;
vector<string> text1;
while(infile1>>buffer1)
{
text1.push_back(buffer1);
cout<<buffer1<<endl; //buffer1为每个记录的内容,取每个字段
//可以以“-”为标准来分别取出每个字段

}
vector<string>::iterator iter=text1.begin();
for(cnt=1;iter!=text1.end();iter++)
outfile<<*iter<<endl;
}
-------------------------
如果要用 c实现,建议楼主好好研究一下 fseek()函数,另外在存储在文本文件的记录的换行也占用文件的长度,而且占用两个字节,这在文件指针位置定位时候需要细细考虑
cenlmmx 2006-01-09
  • 打赏
  • 举报
回复
getline(...)
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
屋顶上的老猫 2006-01-09
  • 打赏
  • 举报
回复
fgets()整行读取,不过写回去要比较麻烦一些!

一般处理文件数据问题,使用结构体整体进行文件数据的更改!
C200501111 2006-01-09
  • 打赏
  • 举报
回复
easy
如果是用的MFC
用CString 的format,如果是纯c++,用string
你中间不是有分割副吗?就用它分就可以啊

70,026

社区成员

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

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