fstream错误无法在C 中创建或打开文件
fstream error cannot create or open file in c++
每当要输入选项1以创建文件以存储我的数据时错误会生成。我无法创建文件,也无法读取该文件
*//cannot create the file*
#include <fstream>
class file
{
private:
char data[100];
public:
void Write();
void Read();
};
*//this is the write function* help me
void file::Write() {
*//fin object is created*
fstream fin;
*//cant open the file*
fin.open("rahul.txt", ios::in);
if (fin.is_open()) { help me
cout << "Enter data" << endl;
cin.ignore();
cin.getline(data, 100);
fin.close();
} else
cout << "error" << endl; help me
}
*//this is the read function*
void file::Read() {
*//fout object is created*
fstream fout;
*//cant read the file* help me
fout.open("rahul.txt", ios::out);
if (fout.is_open()) {
while (!fout.eof()) {
fout.getline(data, 100);
cout << data; help me
}
fout.close();
} else
cout << "error" << endl;
}
int main() {
int choice;
file f;
while (1) {
cout << "Menu" << endl;
*//cant enter the file when pressed choice 1*
cout << "1.Enter 2.Display 3.exit" << endl;
cout << "Enter your choice" << endl;
cin>>choice;
switch (choice) {
case 1:
f.Write();
break;
case 2:
f.Read();
break; help me
case 3:
return 0;
}
}
return 0;
}
*`//cant enter the file`*
请有人可以帮助我解决这个问题。
fin.open("rahul.txt", ios::in);
中使用的打开模式要求该文件已经存在。并将其放置在"当前目录"中,无论是什么,因为您没有指定路径。
模式in
还只允许您从文件中读取。
fout.open("rahul.txt", ios::out);
中使用的打开模式将创建一个空文件供书写(用同名名称替换任何以前的文件)。试图从该文件中读取将失败,因为两个原因 - 该文件始终为空,并且仅写作。
相关文章:
- 如何从Windows控制台调用.exe(C++)以在不同的目录(或任何目录)中创建文件夹?
- 避免使用 boost::进程间::消息队列创建文件
- 在C++中创建文件夹选取器对话框的最简单方法是什么?
- 如何在软件代码中使用ofstream创建文件
- 在特定 Unicode 路径中创建文件
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- 即使使用 FILE_FLAG_DELETE_ON_CLOSE 属性创建文件,文件也会保留在磁盘上
- C++ 为每个结构{人}条目创建文件
- "Access is Denied" U盘上的创建文件()
- 在c++编程中的文件夹中创建文件
- 创建文件夹结构
- 在C++中创建文件夹
- 创建文件函数是否可以打开仅在WinObj实用程序中的全局目录下列出的设备的句柄?
- 使用 C++ 或 SDL 在 Android 中创建文件
- 如何检测哪些进程更改,重命名或创建文件?
- 如何在 Linux 中通过 C/C++ 以其他用户身份创建文件?
- 挂钩创建文件抛出异常:读取访问冲突
- UWP 创建文件2 ERROR_ACCESS_DENIED "NUL"
- 如何指定创建文件函数获取未缓存的结果?
- Open() 用于创建文件和打开现有文件.有人能分辨出其中的区别