如何写入随机访问文件
How do I write to a random access file?
此代码应该将一些项目写入文件,但创建的文件具有 0 个字节并且为空。我真的不确定有什么问题。
我没有任何错误。另外,随机访问文件可以.txt还是必须.dat?
add 函数运行并添加信息没有错误,但文件中没有任何内容。
void add()
{
int hour, min, day, month, yr, snum, interNo,age;
string fname, lname, clinic, area, ex, li, type, sname, town, pay,problem, breed, gender;
char ans;
Intervention inte;
fstream InfoFile("JSPCA.dat", ios::in | ios::out |ios::app);
if (!InfoFile)
{
cout << "Error -file could not be opened.";
}
else
{
cout<<"Enter intervention numbern";
("Enter 0 to end input)n");
cin>>interNo;
system("cls");
InfoFile.seekp(sizeof(Intervention)*(interNo - 1));
/* read record from file*/
InfoFile.read(reinterpret_cast<char *> (&inte), sizeof(Intervention));
if (inte.getClient().getInterventionNo() != 0)
{
cout << "inter # already contains information.n";
}
else {
/* create record */
while (interNo != 0) {
InfoFile.seekp(sizeof(Intervention)*(interNo - 1));
InfoFile.read(reinterpret_cast<char *> (&inte), sizeof(Intervention));
if (inte.getClient().getInterventionNo() != 0)
cout << "inter # already contains information.n";
else {
// user enters information, which is copied into file
cout << "Enter name of clinic(Winchester or Caymanas)n ";
cin >> clinic;
cout << "Enter lastname, firstnamen ";
cin >> lname;
cin >> fname;
cout << "Please Address(Street#, Street Name, Town)n";
cin >> snum >> sname >> town;
cout << "Enter Contact#:(area exchange line)n";
cin >> area >> ex >> li;
cout << "Enter Animal Typen";
cin >> type;
cout << "Enter Animal Breedn";
cin >> breed;
cout << "Enter animal's gendern";
cin >> gender;
cout << "Enter Animal problemn";
cin >> problem;
cout << "Enter age of animaln";
cin >> age;
cout << "Please enter letter for payment type:n";
cout << "Full=Fn";
cout << "Contribution=Cn";
cout << "Can't Pay=CPn";
cin >> pay;
cout << "Enter date in format dd mm yyyyn";
cin >> day >> month >> yr;
cout << "Enter the time(hour minuten)";
cin >> hour >> min;
//inte.getClient().getInterventionNo() == interNo;
// set records for Client, address, TelNo, animal, date and time values
inte.setClient(Client(fname, lname, pay, interNo, clinic));
inte.setAddress(Address(snum, sname, town));
inte.setTelNo(TelNo(area, ex, li));
inte.setAnimal(Animal(type, breed, gender, problem, age));
inte.setDate(Date(day, month, yr));
inte.setTime(Time(hour, min));
InfoFile.seekp(sizeof(Intervention)*(interNo - 1));
InfoFile.write(reinterpret_cast<const char *>(&inte), sizeof(Intervention));
}
system("cls");
cout << "Enter new intervention numbern";
cout<<"Enter 0 to end input";
cin >> interNo;
}
}
}
}
至少存在三个错误
1) 当你的对象包含 std::string 时,你不能做二进制 I/O。我猜是这种情况,您还没有发布您正在使用的对象的定义。底线是,你不应该只是假设你可以在任何东西上做二进制I/O。您必须了解可以使用二进制 I/O 输出哪种对象。我猜你的不能。
2)如果你想做二进制I/O,你必须用ios::binary打开文件
3)这就是您获得空文件的原因。如果文件为空(或不存在),则
InfoFile.read(reinterpret_cast<char *> (&inte), sizeof(Intervention));
将失败。在此之后,在清除错误(您永远不会这样做)之前,任何输入或输出操作都将起作用。因此,以下写入也会失败,最终会得到一个空文件。
我认为您在二进制和随机访问 I/O 方面有一些研究要做。这并不接近正确。
相关文章:
- 防止临时对象文件访问 MSVC 中的磁盘
- 内存映射文件访问非常慢
- 从多个文件访问静态全局 WinForm UI 对象
- 在单元测试项目中包括 .c 文件,并从多个 cpp 文件访问它而不会出现链接问题
- 如何在Android中从本机编译的c ++二进制文件访问相机
- 如何:监视Windows中另一个进程的文件访问?
- 如何从NTFS中的$ MFT文件访问文件的每个块的文件指针
- 执行C 代码时快速频繁的文件访问
- 使用TDD时隐藏文件访问实现详细信息
- 从C文件访问C 类成员功能
- 从另一个CPP文件访问Form1的文本框,Visual C 2010
- 在所有CPP文件中使用一个Globe变量.如果CPP文件的一个类更改该值,我想从另一个类CPP文件访问它
- 如何从C 文件访问$(target_arch)
- 系统访问出界异常,尝试从flatbuffer的二进制文件访问"LengthofTable"
- 如何声明可通过多个头文件访问的对象
- 使用二进制文件访问违规读取位置
- 哪种类型的文件访问使用
- 在其他源文件中没有通用的头文件访问功能
- 文件访问不起作用
- Windows 8 应用商店应用程序C++文件访问