[英]read word by word from file in C++
這個 function 應該一個字一個字地讀取一個文件,它會一直工作到最后一個字,運行停止的地方
void readFile( )
{
ifstream file;
file.open ("program.txt");
string word;
char x ;
word.clear();
while ( ! file.eof() )
{
x = file.get();
while ( x != ' ' )
{
word = word + x;
x = file.get();
}
cout<< word <<endl;
word.clear();
}
}
任何人都知道問題是什么以及如何解決?
首先,不要循環while (!eof())
,它不會像你期望的那樣工作,因為在由於文件結束而導致讀取失敗之后才會設置eofbit
。
其次,普通輸入操作符>>
在空格上分隔,因此可用於讀取“單詞”:
std::string word;
while (file >> word)
{
...
}
我為你編輯了這個功能,
void readFile()
{
ifstream file;
file.open ("program.txt");
if (!file.is_open()) return;
string word;
while (file >> word)
{
cout<< word << '\n';
}
}
你在這里做的是從輸入流一次讀取一個字符,並假設“”之間的所有字符代表一個單詞。 但是在最后一個單詞之后它不太可能是“”,所以這可能是它不起作用的原因:
"word1 word2 word2EOF"
如果我可以為您提供相同任務的新代碼,在我的代碼中您可以創建一個所謂的“文檔”(不是真的)並保存,然后可以再次打開。 它也存儲為字符串文件(不是文檔)。 這是代碼:
#include "iostream"
#include "windows.h"
#include "string"
#include "fstream"
using namespace std;
int main() {
string saveload;
cout << "---------------------------" << endl;
cout << "|enter 'text' to write your document |" << endl;
cout << "|enter 'open file' to open the document |" << endl;
cout << "----------------------------------------" << endl;
while (true){
getline(cin, saveload);
if (saveload == "open file"){
string filenamet;
cout << "file name? " << endl;
getline(cin, filenamet, '*');
ifstream loadFile;
loadFile.open(filenamet, ifstream::in);
cout << "the text you entered was: ";
while (loadFile.good()){
cout << (char)loadFile.get();
Sleep(100);
}
cout << "" << endl;
loadFile.close();
}
if (saveload == "text") {
string filename;
cout << "file name: " << endl;
getline(cin, filename,'*');
string textToSave;
cout << "Enter your text: " << endl;
getline(cin, textToSave,'*');
ofstream saveFile(filename);
saveFile << textToSave;
saveFile.close();
}
}
return 0;
}
只需使用此代碼並更改它以滿足您的目的。 夢想大,想大,做大
文本文件:
代碼:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream file("DB_name_age.txt");
int index;
string name;
int age;
if(file.is_open())
{
while(file >>index >> name >>age)
{
cout << index <<" "<<name <<" "<<age << endl;
}
}else{
cout<< "file open fail" <<endl;
}
return 0;
}
正如其他人所說的那樣,你可能正在閱讀文件的末尾,因為你只是檢查x != ' '
。 相反,你還必須檢查內循環中的EOF(但在這種情況下不要使用char,但是要使用足夠大的類型):
while ( ! file.eof() )
{
std::ifstream::int_type x = file.get();
while ( x != ' ' && x != std::ifstream::traits_type::eof() )
{
word += static_cast<char>(x);
x = file.get();
}
std::cout << word << '\n';
word.clear();
}
但話說回來,你可以只使用流的流媒體運營商,這些運營商已經在空白處分開(並且更好地考慮了多個空間和其他類型的白色空間):
void readFile( )
{
std::ifstream file("program.txt");
for(std::string word; file >> word; )
std::cout << word << '\n';
}
更進一步,您可以使用標准算法完全擺脫手動循環:
#include <algorithm>
#include <iterator>
void readFile( )
{
std::ifstream file("program.txt");
std::copy(std::istream_iterator<std::string>(file),
std::istream_itetator<std::string>(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.