我尝试了很多解决方案,但都不起作用,无论是绝对路径还是相对路径。我也更改了目录等等。我的代码总是这样工作的,我不知道哪里出了问题。
我所做的两个例子:
// 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;
}
}
我在使用您的代码时遇到的错误是:
Error: Expected a declaration.
Error: This declaration has no storage class or type specifier.
我认为问题可能是你错过了一些#include。我看到你需要#include<string>和其他几个语句,以及第44行的一个不正确的if语句(带有#include<string>)。