问题解决了马上就给分!

aitforever 2003-03-03 10:40:50
已知一文本文件,例如格式:id pro dec
001 A test1
002 B test2
003 C test3
输入一个id 001,如何让其匹配上产品001,输出其优先级(pro)及描述(dec)?在下正初学c++,望高手指点,先谢谢了!
已写的部分程序:
void main()
{fstream infile;
infile.open("table.txt",ios::in|ios::out|ios::binary); //table.txt 即文本
if (!infile)
{
cout <<"table.txt can't open.\n");
abort();
}
.........
...全文
44 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunwhite 2003-03-03
  • 打赏
  • 举报
回复
if(strstr(id, "001"))
{
strcpy(pro, "A");
strcpy(dec, "test1");
}
else if(strstr(id, "002"))
{
strcpy(pro, "B");
strcpy(dec, "test2");
}
else if(strstr(id, "003"))
{
strcpy(pro, "C");
strcpy(dec, "test3");
}
langziji 2003-03-03
  • 打赏
  • 举报
回复
在id,pro,dec之间进行存储的时候最好是用一位进行区分,当然最好的是按长度区分。这样就方便了。或者是定字段长度,例如:id :3,pro:1,dec:20.或者是以struct content{ int len;int m_content;}这样的格式来存储,当然这种方法会好一些。这样才容易处理一些。
zhanyidan 2003-03-03
  • 打赏
  • 举报
回复
姑且试一试
zhanyidan 2003-03-03
  • 打赏
  • 举报
回复
#include <fstream>
#include <string>
#include <iostream>
#include <stdlib>



ifstream infile( "zhj.txt", ios::in||ios::binary );
char p = '\n';
char *p1 = new char;
char *id =new char;
cout << "please enter the id" << endl;
cin >> id;
if( !infile )
{
cout << " file open failed ";
exit( 1 );
}
while(infile && !infile.eof() )
{
if( p == '\n' )
{
infile.read(p1, 3);
if( !strcmp( p1,id ) )
{
do
{
infile.read( &p, 1);
cout << p;
}while( p != '\n' );
delete p1;
delete id;
return 0;
}
}
infile.read( &p, 1);
}
cout << "can't find the match id" << endl;
delete p1;
delete id;
return 0;
bjzhaoxiao 2003-03-03
  • 打赏
  • 举报
回复
1。先读出table.txt的一行,再判断所输入的id是不是在这一行中如果是转2否则转3
2。将所读的字符串以空格分开,就是其id,pro,des
3。转1
4。结束
aitforever 2003-03-03
  • 打赏
  • 举报
回复
如果文本中的产品id号对于程序员来说是未知的,也就是如何判断输入的id号是否在文本中存在,如果存在再对其内容输出。谢谢...在线等候

69,371

社区成员

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

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