我嘗試了很多解決方案,沒有任何工作,絕對和相對路徑。我也改變了目錄,等等。我的代碼總是以這種方式工作,我不知道什麼是錯的。FStream讀取txt文件
兩個我所做的例子:
// Read a file into memory
#include <iostream> // std::cout
#include <fstream> // std::ifstream
int main() {
std::ifstream is ("test.txt", std::ifstream::binary);
if (is) {
// Get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char * buffer = new char [length];
std::cout << "Reading " << length << " characters... ";
// Read data as a block:
is.read (buffer,length);
if (is)
std::cout << "all characters read successfully.";
else
std::cout << "error: only " << is.gcount() << " could be read";
is.close();
// ...buffer contains the entire file...
std::cout << buffer;
system("PAUSE");
delete[] buffer;
}
return 0;
}
和
char pfad[256]; //The path to the application is stored here.
_getcwd(pfad, 256);
std::string truepfad;
truepfad = pfad;
truepfad.append("\\test.txt");
fstream f("C:\\Users\\Etix\\Documents\\test.txt");
string s;
if (!f)
std::cout << truepfad;
else
{
std::cout << "open file";
while (getline(f, s)){
std::cout << s;
}
}
任何錯誤訊息? – lxg 2014-09-13 13:43:30