如何通过c++将txt文件内容转换 为希望的格式?急!

xiaobaiwen 2008-12-14 11:10:46
现在有一个txt文件,急需把他录入到数据库中,所以要改一下文件类容,用C++实现文件的转换
大家帮忙看看应该如何转换
现有的txt文件类容格式如下:(student.txt)
050201 李丽 女 2005 02
050205 林达 男 2005 02
050207 高雄 男 2005 02
050212 黎明 男 2005 02
050311 陈淋 女 2005 03

希望通过软件转换后类容如下:(insertData.txt)
insert into student values('050201', '李丽', '女', '2005', '02');
insert into student values('050205', '林达', '男', '2005', '02');
insert into student values('050207', '高雄', '男', '2005', '02');
insert into student values('050212', '黎明', '男', '2005', '02');
insert into student values('050311', '陈淋', '女', '2005', '03');


以前没有碰到这样的情况,一点思路都没有,明天就要交数据库的课设了,还有很多数据没有来得急录入,
希望大家帮帮忙阿!! 谢谢咯!!
要是问题没有写清楚,大家也回帖说说,我再改! 不要看了不鸟我阿!! 很急阿!!!
...全文
425 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
SearchLife 2008-12-14
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;


int main( void )
{
ifstream fin("student.txt");
if (!fin)
{
cout <<"file error!";
exit(EXIT_FAILURE);
}
ofstream fout("insertData.txt");
if (!fout)
{
cout <<"file error!";
exit(EXIT_FAILURE);
}
string strLine;
string s,s0;
while (getline(fin,strLine))
{
s0="(";
stringstream ss(strLine);
while (ss>>s)
{
s0 = s0 + "'" + s + "',";
}
s0[s0.size() - 1] = ')';

s0 = "insert into student values" + s0 + ";";
fout << s0;
}

fin.close();
fout.flush();


return 0;
}

程序通过
nullah 2008-12-14
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

int main()
{
fstream in("aaa.txt");
fstream out("data.txt");
stringstream ina;
string word;
string tmp;
while(getline(in,word))
{
ina.str(word);
out << "insert into student values(";
int i = 0;
while(ina>>tmp)
{
out << "'" << tmp << "'";
if(i < 4)
{
out << ",";
}
i++;
}
out << ")" << endl;
ina.clear();
}

in.close();
out.close();

return 0;
}

data.txt
insert into student values('050201','李丽','女','2005','02')
insert into student values('050205','林达','男','2005','02')
insert into student values('050207','高雄','男','2005','02')
insert into student values('050212','黎明','男','2005','02')
insert into student values('050311','陈淋','女','2005','03')
SearchLife 2008-12-14
  • 打赏
  • 举报
回复
从student.txt每读一行就往insertData.txt写一行数据。
用ifstream和ofstream
yshuise 2008-12-14
  • 打赏
  • 举报
回复
这是很简单的操作。你那点不会?
xiaobaiwen 2008-12-14
  • 打赏
  • 举报
回复
大家有空就看看吧!!!!!!!!!!!
xiaobaiwen 2008-12-14
  • 打赏
  • 举报
回复
非常感谢楼上各位的回复,问题已经搞定,谢谢啦!!!!
wiowei 2008-12-14
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 SearchLife 的回复:]
楼上请问一下:
你是怎么复制代码的啊?怎么如此整齐?我从vc6里拷过来怎么这么乱啊
[/Quote]
buddy,用脚本啦,工具栏上"插入源代码" or自己在你的代码前后加"code=C/C++"和"/code"(把"换成[])啦
kuguakugua 2008-12-14
  • 打赏
  • 举报
回复
数据库一般都有导入工具的,只要是格式固定就行,不用这么麻烦吧
SearchLife 2008-12-14
  • 打赏
  • 举报
回复
楼上请问一下:
你是怎么复制代码的啊?怎么如此整齐?我从vc6里拷过来怎么这么乱啊

65,211

社区成员

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

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