| 
 | 
 
1鱼币 
#include<iostream> 
#include <fstream> 
#include"head.h" 
#include<string>  
using namespace std; 
//-----------------------------删除学生--------------------- 
void delstudent() 
{  system("cls"); 
    fstream in("test.txt",ios::in); if(!in){cout<<"asd";}; 
   char buffer[200];//临时缓存  
  
    //显示文本所以内容 
    while(!in.eof())  
    {  
        in.getline(buffer,100);//读入打开文件的每一行  
        cout<<buffer<<endl;  
    }  
   
    string sourcefile;   
    int seeklength;   
    string seekword,replaceword;//定义要查找的学号和替换为的学号字符串变量   
    char seekchar[20],replacechar[20];   
   
    cout<<"请输入要删除的学号:"<<endl;   
    cin>>seekword; 
 replaceword=" ";//用空白来替换要查找的学号 
    strcpy(seekchar,seekword.c_str());//将查找单词字符串转换为字符数数组   
    strcpy(replacechar,replaceword.c_str());//将替换单词字符串转换为字符数数组   
    seeklength = seekword.length();//记录要查找单词的长度   
    while(!in.eof())   
    {   
        in.getline(buffer,200);//读入打开文件的每一行   
        sourcefile.append(buffer);//将读取文件的文件内容存到字符串中   
        sourcefile.append("/r/n");//追加换行符   
    }   
    int index=0;   
    bool mark=true;   
    while(mark)   
    {   
        index = sourcefile.find(seekword,index);//定位要替换的单词,记录其开始位置   
       if(index == string::npos)//替换完毕或没发现单词的处理语句   
      {   
           cout << "没有找到该学号" << endl;   
           mark=false;   
      }   
       else   
            sourcefile.replace(index,seeklength,replaceword);//用替换字符串替换指定位置的单词   
   
    }   
    in.clear();//打开已存在的流对象,必须在每次偏移循环时清空   
    in.seekp(0,ios::beg);//定位到文件开始位置   
    in<<sourcefile;//重新写入替换后的文件内容   
    in.close();  
  
} ; 
 |   
 
 
 
 
 
 |