无法打开 fstream C++文件,即使它与 .cpp 位于同一位置
Failure to open fstream C++ file even though it's in same location as .cpp
我目前正在做一些功课,但我无法让这段简单的代码(我已经提取了它(工作。我只需要它来打开文件,以便我可以读取和写入它。 该文件 (Sedes.txt( 与当前工作目录中的.cpp和.exe位于同一位置。即使使用 C:\ 或 C:\ 或 C://或 C:/添加路径也不起作用。 我正在使用带有编译器代码生成选项的 DEV C++ -std ISO C++11
我还确认将此链接与解决方案代码一起使用以证实目录。它输出在同一文件夹中。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
fstream aSedes("Sedes.txt");
int main(){
string txtWrite = "";
string txtRead = "";
aSedes.open("Sedes.txt", ios_base::in | ios_base::out);
if(aSedes.fail()){
cout << "!---ERROR: Opening file ln 446---!";
} else {
cout << "Sedes.txt opened successfully." << endl;
cout << "Text in file: " << endl;
while(aSedes.good()){
getline(aSedes, txtRead);
cout << txtRead << "n";
}
}
aSedes.close();
return 0;
}
老实说,我完全迷路了。我尝试在任何地方将其切换无济于事。
您打开文件两次,一次使用构造函数,一次使用open
fstream aSedes("Sedes.txt"); // opened here
int main(){
string txtWrite = "";
string txtRead = "";
aSedes.open("Sedes.txt", ios_base::in | ios_base::out); // and here again
if(aSedes.fail()){
试试这个
int main(){
string txtWrite = "";
string txtRead = "";
fstream aSedes("Sedes.txt", ios_base::in | ios_base::out);
if(!aSedes.is_open()){
您可能更喜欢is_open
来检查文件是否已打开。您可能应该为流使用局部变量。但是如果你想要一个全局变量,那么这也应该有效
fstream aSedes;
int main(){
string txtWrite = "";
string txtRead = "";
aSedes.open("Sedes.txt", ios_base::in | ios_base::out);
if(!aSedes.is_open()){
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- Linux的Cpp上的计时器
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 命名空间中具有.h和.cpp文件的类
- 将值指定给向量(2D)的向量中的某个位置
- 内置函数可查看CPP中的成员变量
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- Cpp-Tuple使用带有变量的get
- 使用Unreal C++获取VR耳机的世界位置/方向
- 写入位置0x0000000C时发生访问冲突
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 无法打开 fstream C++文件,即使它与 .cpp 位于同一位置
- CPP 运算符重载操作数位置 [] 与 ==
- JNI如何从CPP获取JAR位置
- 将过滤器中的所有.cpp和.h移动到另一个项目并更正其文件夹位置
- 用于配置cpp源文件位置的环境变量
- CPP Win32移动鼠标,Wont移动到我指定的位置
- 使用gcc或cpp搜索定义的位置
- 在类作用域内定义方法时,除非在原始 cpp 文件中的其他位置调用方法,否则项目不会编译