鱼C论坛

 找回密码
 立即注册
查看: 4390|回复: 5

c++怎么把写入文本的文本直接指向指定的字符

[复制链接]
发表于 2013-7-30 10:47:56 | 显示全部楼层 |阅读模式
1鱼币
1.比如我txt里面有
   123  张三
   456
   789 李四

当我输入456时,先匹配到456 是存在的
然后我想让文本指针指向到456 后面
然后在一个命名的函数里给456添加姓名
结果成  456 赵六
?怎么才能把文本指针指向到456后面而不会指向到其他位置?
求教~
本人刚学不久很多不会。
就是根据自己输入的数字,然后指针指到该数字的后面

2.下面STU *stu=new STU[n]建立空间后
for(int a=0;a<n;a++)
        in>>stu[a].num>>stu[a].subject;
    cin>>num;
    for(int i=0;a<n;i++)
        if(num == stu[i].num)
cout<<"学号:"<<stu[i].num<<endl<<"科目:"<<stu[i].subject<<endl;
却显示不出来?
求解
-----------------------------------------------------------------代码----------------------------------------------------------
#include<iostream>
#include <fstream>
#include <string>
#include <string.h>
using namespace std;

typedef struct STU
{
    int num;
    string subject;
}STU;
void main()
{

//向文本文件中插入数据,我已经在文本中插入
/*
txt中:123  张三
           456
           789 李四
就是456的姓名是空的,然后我要用456去登陆,然后让指针指到456后面去,再为456添加姓名
*/

string number;
string name;
ofstream outstuf ;  
outstuf.open( "test.txt" , ios::app|ios::binary ) ;
if ( !outstuf )   
     { cerr << "文件不存在" << endl ;    abort();  }
cin>>number;
number=number+" ";
cin>>name;

outstuf.seekp(0,ios::end);   
outstuf<<number.c_str();
outstuf<<name.c_str()<<"\r\n";

outstuf.close();   


//检测文本的行数
int num;
ifstream in("test.txt",ios::in);
string line;
int n;
while(getline(in,line))
{  n++;}
n=n-1;

//建立空间,这里貌似也有点问题,不知道为什么下面的cout的内容显示不出来,能顺便看下么
STU *stu=new STU[n];  
    for(int a=0;a<n;a++)
        in>>stu[a].num>>stu[a].subject;
    cin>>num;
    for(int i=0;a<n;i++)
        if(num == stu[i].num)
cout<<"学号:"<<stu[i].num<<endl<<"科目:"<<stu[i].subject<<endl;
   delete []stu;

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-30 11:56:45 | 显示全部楼层
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <iterator>
  7. using namespace std;

  8. struct STU{
  9.         int num;
  10.         string name;
  11. };

  12. istream& operator>>( istream& in, STU& stu ) {
  13.         in>>stu.num;
  14.         if( stu.num == 456 ) stu.name = "赵六";
  15.         else in>>stu.name;
  16.         return in;
  17. }

  18. ostream& operator<<( ostream& out, const STU& stu ) {
  19.         return out<<stu.num<<"\t"<<stu.name;
  20. }

  21. int main() {
  22.         typedef istream_iterator<STU> ISIT;
  23.         typedef ostream_iterator<STU> OSIT;
  24.         vector<STU> vec;
  25.         ifstream in("text.txt");

  26.         copy( ISIT(in), ISIT(), back_inserter(vec) );

  27.         in.close();
  28.         ofstream out("text.txt");
  29.         copy( vec.begin(), vec.end(), OSIT(out,"\n") );
  30. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-30 12:37:53 | 显示全部楼层

if( stu.num == 456 ) stu.name = "赵六";
还有其他还代替这句的么;
因为我输入的数字使随机的;
也可能不只是456才是空的,
也可能789也是空的;
我是用cin》》number,cin》》name。
可以不可以if( stu.num == number ) stu.name = name;?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-30 13:24:33 From FishC Mobile | 显示全部楼层
:Q谁来帮帮我
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-30 15:35:46 | 显示全部楼层
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <map>
  7. #include <iterator>
  8. using namespace std;

  9. struct STU{
  10.         int num;
  11.         string name;
  12. };

  13. map<int,string> replace_map;
  14. void init_map() {
  15.         //在这里添加要替换的学号和姓名
  16.         //可以添加多个
  17.         replace_map[456]="赵六";
  18.         replace_map[234]="曹操";
  19. }

  20. istream& operator>>( istream& in, STU& stu ) {
  21.         in>>stu.num;
  22.                 if( replace_map.find(stu.num) != replace_map.end() )
  23.                         stu.name = replace_map[stu.num];
  24.         else in>>stu.name;
  25.                 return in;
  26. }


  27. ostream& operator<<( ostream& out, const STU& stu ) {
  28.         return out<<stu.num<<"\t"<<stu.name;
  29. }


  30. int main() {
  31.         typedef istream_iterator<STU> ISIT;
  32.         typedef ostream_iterator<STU> OSIT;
  33.                 init_map();
  34.         vector<STU> vec;
  35.         ifstream in("text.txt");
  36.         copy( ISIT(in), ISIT(), back_inserter(vec) );
  37.         in.close();
  38.         ofstream out("text.txt");
  39.         copy( vec.begin(), vec.end(), OSIT(out,"\n") );
  40. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-30 16:23:24 | 显示全部楼层

2楼不是在帮你?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-17 10:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表