欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)刪除txt文件中指定內(nèi)容的示例代碼

 更新時(shí)間:2020年12月18日 10:54:40   作者:SWUST-巧樂茲  
這篇文章主要介紹了C++實(shí)現(xiàn)刪除txt文件中指定內(nèi)容的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

默認(rèn)明白C++的文件輸入輸出流

方法:
新建一個(gè)中間文件,逐行讀取原文件(test.txt)的內(nèi)容并寫入到中間文件(temp.txt),遇到需要?jiǎng)h除的內(nèi)容則跳過。
再將中間文件的內(nèi)容寫入原文件,刪除中間文件。

fstream in("C:\\Users\\Administrator\\Desktop\\test.txt", ios::in);//原文件
  fstream out("C:\\Users\\Administrator\\Desktop\\temp.txt", ios::out);//中間文件
  string name, pass, str, estr;
  cout << "輸入1:" << endl;
  cin >> name;
  cout << "輸入2:" << endl;
  cin >> pass;
  str = name.append(" ").append(pass);//test.txt中需要?jiǎng)h除的某一行內(nèi)容,可根據(jù)自己的需要修改
  while (getline(in, estr))//得到test.txt中一行的內(nèi)容
  {
    if (!estr.compare(str))//比較test.txt每一行的內(nèi)容和要?jiǎng)h除的是否一致,一致就跳過(不懂為啥跳過看文章開頭的方法)
      continue;
    out << estr << "\n";//不一致的內(nèi)容寫到temp.txt中,注意換行
  }
  in.close();//關(guān)閉流
  out.close();
  fstream outfile("C:\\Users\\Administrator\\Desktop\\test.txt", ios::out);
  fstream infile("C:\\Users\\Administrator\\Desktop\\temp.txt", ios::in);
  while (getline(infile, estr)) //將temp.txt的內(nèi)容寫到test.txt
  {
    outfile<< estr << "\n";
  }
  const char* path = "C:\\Users\\Administrator\\Desktop\\temp.txt";
  remove(path);//刪除temp.txt
  outfile.close();//關(guān)閉流
  infile.close()

放個(gè)圖叭
需要?jiǎng)h除的內(nèi)容為:emp3 333

test.txt原文件

刪除指定內(nèi)容后的原文件

啪!很快啊,傳統(tǒng)編程,講究點(diǎn)到為止!

到此這篇關(guān)于C++實(shí)現(xiàn)刪除txt文件中指定內(nèi)容的示例代碼的文章就介紹到這了,更多相關(guān)C++刪除txt指定內(nèi)容內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論