代码不适用于"Modifying a data in a Binary file"
Code not working for "Modifying a data in a Binary file"
,所以这是我的代码"从二进制文件中修改记录",它应该起作用...
void Update()
{
fstream file;
STUDENT st, temp; // STUDENT is a class which I made
int RollNo;
clrscr();
cout << "Enter the Roll Number: ";
cin >> RollNo; // to get the roll number which is to be modified. Also there is a data member inside the class STUDENT which is also known as RollNo
cout << "n Enter the update info...nn";
st.Input(); // This is a function inside the class STUDENT to get the values for the data members
file.open("Student.dat", ios::binary | ios::ate);
file.seekg(0);
while(file)
{
file.read((char*)&temp, sizeof(temp)); // the data which is being read is stored inside temp
if(temp.retRollNo() == RollNo) // since RollNo is a private data member inside the class STUDENT so I've created an accessor function which returns RollNo
{
file.seekp(file.tellg() - sizeof(temp));
file.write((char*)&st, sizeof(st));
}
} // please note that this loop modifies all the instances which match the criteria and not just one since this while loop runs until end of file
file.close();
}
但是此代码的问题是它不会修改任何记录...为什么?
您应该在if(temp.retRollNo() == RollNo)
的内部添加一个调试语句,例如cout << "Made it heren";
阅读此链接后,我认为您确实应该将 std::fstream::in | std::fstream::out
作为file.open()
的第二个参数。
相关文章:
- netcat command in c++
- Difference in displaying cv2 Mat
- C++ MFC Libraries in Travis CI
- 如何在OpenSSL中从configuration.h.in获取configuration.h
- 创建具有 new in 函数和"this is nullptr"异常的对象
- IN, OUT, INOUT Parameters
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 有人安装"IITB Simplecpp in mac"吗?
- 从 C 样式字符串中删除子字符串 "in place" 在C++代码中
- 如何修复"error: ‘_1’ was not declared in this scope"?
- Softmax Implementation in C++
- 将 out/in out 参数与 if/switch 的 init 语句一起使用
- IF-nesting in c++
- Gurobi GRBModel and GRBmodel in C++
- Tensorflow Hub in C++
- 如何修复张量流中的"Non-OK-status: Not found: Op type not registered 'NoOp' in binary running"
- 计算"copying a binary file in c++ on linux"消耗时间的最佳方法是什么?
- 代码不适用于"Modifying a data in a Binary file"
- 分段故障即将"Insertion in Binary Search tree." #
- 错误"token is not a valid binary operator in a preprocessor subexpression"