簡體   English   中英

在 C++ 中的文本中查找單詞並打印一些接下來的特定行

[英]find word in a text in C++ and print some next specific lines

我用 C++ 寫了一個代碼,它寫了一個 .txt 文件。 然后我想再次打開代碼並提供一些信息,這樣我就可以根據我作為輸入提供的內容獲得新文本。 例如,我想給出月份的名稱,並在另一個 .txt 文件中打印“November”這個詞之后的所有行。 我找到了一些解決方案,但沒有一個對我有用!

我在堆棧溢出時發現的一種解決方案如下:

void Keyword(ifstream & stream, string token) {
    string line;
    while (getline(stream, line)) {
        if (line.find(token) != string::npos) {
            cout << line << endl;
        }
    }
    cout << token << " not found" << endl;
}

我無法用上面的代碼打印下一行。

任何建議都會有所幫助! 謝謝!

如果您想對諸如“讀取”和/或“寫入”之類的文件執行操作,您可能需要在網上(或者如果您有一本 C++ 書籍)上搜索諸如“使用 C++ 進行文件 I/O 操作”之類的主題. 無論如何,C++ 有 2 個基本類來處理ifstreamofstream文件。 要使用它們,您必須包含頭文件fstream (即#include<fstream>)或將它們分別包含為#include<ifstream>#include<ofstream> ifstream基本上用於所有輸入操作,例如讀取文件等。類似的ofstream用於所有輸出操作,例如將數據寫入文件。

您可以通過執行以下操作打開文件並向其中寫入數據,

ofstream myFile("filename");// Create an instance of ofstream and open file for writing data

並將數據寫入文件使用<<運算符,如下所示,

myFile<<data;

同樣,您可以按如下方式打開文件並讀取數據,

ifstream myFile("filename");//Create an instance of ifstream and open file to read data

並使用>>操作符從文件中讀取數據,如下所示,

myFile>>data;

您還可以使用方法void open(const char *filename, ios::openmode mode); 如下所示,

//Writing only
ofstream outFile;
outFile.open("filename.txt",ios::out);


//Reading only 
ifstream inFile;
inFile.open("filename.txt",ios::in);

//For reading and writing
fstream file;
file.open("filename.txt",ios::in|ios::out);


//For closing File
outFile.close();
//or
inFile.close();
//or
file.close(); 

請注意open()方法采用各種標志,例如ios::in用於讀取模式, ios::out用於寫入模式, ios::app用於將數據添加到末尾等。所有這些也可以通過使用位 OR 組合運營商| 如下所示,

outFile.open("filename.txt",ios::out|ios::app);

IO 中還有很多。 我剛剛介紹了開始所需的東西。

這是您的問題的解決方案。 試着理解它。

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;



int main()
{ 
  ofstream outFile;
  ifstream inFile;
  char fileName[10],data[50];
  int noLines;

 cout<<"Enter Month:"<<endl;
 cin>>fileName;
 cout<<"Enter Number of lines you want to enter:"<<endl;
 cin>>noLines;

 outFile.open(fileName,ios::out);
 cout<<fileName<<"(Enter Data):";
 for(int i=0;i<=noLines;i++)
 {
  cin.getline(data,50);
  outFile<<data<<endl;
 }
 outFile.close();

 cout<<"Openening "<<fileName<<" :"<<endl;
 inFile.open(fileName,ios::in);
 for(int i=0 ;i<=noLines ;i++)
 {
  inFile.getline(data,50);
  cout<<data<<endl;
 }
 inFile.close();



 return 0; 
}

OP 已經找到了大部分解決方案:

string line;
while (getline(stream, line)) {
    if (line.find(token) != string::npos) {
        cout << line << endl;
    }
}
cout << token << " not found" << endl;

但這只會打印帶有關鍵字的行。 並始終打印“未找到”消息。 哎呀。

相反,我投球:

string line;
bool found = false;
while (!found && getline(stream, line)) 
{ // search for keyword
    if (line.find(token) != string::npos)  
    {
        found = true; // found keyword. Stop looking
    }
}
if (found)
{ // print out all remaining lines in the file
    while (getline(stream, line)) 
    {
        cout << line << endl;
    }
}
else
{
    cout << token << " not found" << endl;
}

上面將token的查找和剩余文件的打印分為兩個階段,以提高可讀性。 它可以被壓縮成一個循環,但有兩件事使它成為一個傻瓜賭注:

  1. 這個程序將是 IO 綁定的。 它將花費大部分時間來讀取文件,因此不解決將文件放入內存的小調整會浪費時間。

  2. 組合循環需要在循環中添加邏輯,隨着運行,這將使切換循環的微不足道的成本相形見絀。

試試這個: http : //www.cplusplus.com/doc/tutorial/files/和這個: http : //www.cplusplus.com/forum/beginner/14975/

它是關於在 C++ 中讀取和寫入文件以及在文件中搜索。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM