C ++不从文件中读取任何内容
C++ not reading anything from files
我好像在读取文件时遇到了问题。我正在使用 Visual Studio Community 2013,它会做除读取文件之外的所有事情。我已经检查以确保正在读取和写入的文件位于同一目录中。以下代码是我认为问题所在:
if (inStream.bad())
{
inStream.close();
outStream.open(filename);
outStream << "This is a test file: \nWelcome to the Dark Side!";
outStream.close();
}
inStream.open(filename, ios::in);
if (inStream.good())
{
while (getline(inStream, stream[1]))
{
stream[0] += stream[1] + '\n';
}
inStream.close();
}
else
{
cout << "THIS FILE IS ROYALLY *jacked* UP!!!!" << endl;
}
我得到了 "This file is royally jacked up" 结果。我不明白为什么它不读书。请帮忙。
在打开新文件之前使用 clear
可能会有所帮助,因为 open
可能无法自行清除标志。
inStream.clear();
inStream.open(filename, ios::in);
您也可以使用 is_open
而不是 good
。
if(inStream.is_open()) {
...
尝试调用 inStream.clear() 后再对其进行任何操作。 clear() 清除旧标志,如 bad 为真。
更改行:
if (inStream.bad())
{
inStream.close();
outStream.open(filename);
outStream << "This is a test file: \nWelcome to the Dark Side!";
outStream.close();
}
inStream.open(filename, ios::in);
if (inStream.good())
至
if (inStream.bad())
{
inStream.close();
outStream.open(filename);
outStream << "This is a test file: \nWelcome to the Dark Side!";
outStream.close();
// Clear the state of the stream.
inStream.clear();
// Move this line inside the block.
inStream.open(filename, ios::in);
}
if (inStream.good())
您不想在有效的 ifstream
上调用 open
。
这是一个示例程序,演示了在有效 ifstream
上调用 open
可以使其无效。
#include <iostream>
#include <fstream>
int main()
{
std::ifstream inFile("socc.in");
if ( inFile.good() )
{
std::cout << "ifstream is good.\n";
}
inFile.open("socc.in");
if ( inFile.good() )
{
std::cout << "ifstream is still good.\n";
}
else
{
std::cout << "ifstream is not good any more.\n";
}
return 0;
}
输出:
ifstream is good.
ifstream is not good any more.
我好像在读取文件时遇到了问题。我正在使用 Visual Studio Community 2013,它会做除读取文件之外的所有事情。我已经检查以确保正在读取和写入的文件位于同一目录中。以下代码是我认为问题所在:
if (inStream.bad())
{
inStream.close();
outStream.open(filename);
outStream << "This is a test file: \nWelcome to the Dark Side!";
outStream.close();
}
inStream.open(filename, ios::in);
if (inStream.good())
{
while (getline(inStream, stream[1]))
{
stream[0] += stream[1] + '\n';
}
inStream.close();
}
else
{
cout << "THIS FILE IS ROYALLY *jacked* UP!!!!" << endl;
}
我得到了 "This file is royally jacked up" 结果。我不明白为什么它不读书。请帮忙。
在打开新文件之前使用 clear
可能会有所帮助,因为 open
可能无法自行清除标志。
inStream.clear();
inStream.open(filename, ios::in);
您也可以使用 is_open
而不是 good
。
if(inStream.is_open()) {
...
尝试调用 inStream.clear() 后再对其进行任何操作。 clear() 清除旧标志,如 bad 为真。
更改行:
if (inStream.bad())
{
inStream.close();
outStream.open(filename);
outStream << "This is a test file: \nWelcome to the Dark Side!";
outStream.close();
}
inStream.open(filename, ios::in);
if (inStream.good())
至
if (inStream.bad())
{
inStream.close();
outStream.open(filename);
outStream << "This is a test file: \nWelcome to the Dark Side!";
outStream.close();
// Clear the state of the stream.
inStream.clear();
// Move this line inside the block.
inStream.open(filename, ios::in);
}
if (inStream.good())
您不想在有效的 ifstream
上调用 open
。
这是一个示例程序,演示了在有效 ifstream
上调用 open
可以使其无效。
#include <iostream>
#include <fstream>
int main()
{
std::ifstream inFile("socc.in");
if ( inFile.good() )
{
std::cout << "ifstream is good.\n";
}
inFile.open("socc.in");
if ( inFile.good() )
{
std::cout << "ifstream is still good.\n";
}
else
{
std::cout << "ifstream is not good any more.\n";
}
return 0;
}
输出:
ifstream is good. ifstream is not good any more.