Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 需要帮助调试简单的更新二进制文件功能吗 void updatebfile(字符文件名[MAX]) { fstream-writeradb; char cont='y'; char filenameb[MAX]; int i=1; int记录; 学生证; strcpy(filenameb,filename); strcat(文件名为“.dat”); open(filenameb,ios::in | ios::out | ios::binary); cout_C++_Debugging_Binaryfiles - Fatal编程技术网

C++ 需要帮助调试简单的更新二进制文件功能吗 void updatebfile(字符文件名[MAX]) { fstream-writeradb; char cont='y'; char filenameb[MAX]; int i=1; int记录; 学生证; strcpy(filenameb,filename); strcat(文件名为“.dat”); open(filenameb,ios::in | ios::out | ios::binary); cout

C++ 需要帮助调试简单的更新二进制文件功能吗 void updatebfile(字符文件名[MAX]) { fstream-writeradb; char cont='y'; char filenameb[MAX]; int i=1; int记录; 学生证; strcpy(filenameb,filename); strcat(文件名为“.dat”); open(filenameb,ios::in | ios::out | ios::binary); cout,c++,debugging,binaryfiles,C++,Debugging,Binaryfiles,您总是尝试读取一个条目,并且仅在成功时使用结果(这是完全正确的)。如果没有成功,例如因为遇到EOF,streamstate将设置为“失败”。这会导致读取循环终止,但也会导致该文件流上的任何后续操作失败,直到显式重置streamstate。因此,需要在该循环之后调用writeradb.clear() 还有一些注意事项: 传递char filename[MAX]不会将数组传递给函数!相反,它与char*filename相同,即修改该参数将使其在调用函数中可见。使用std::string,使用其c_

您总是尝试读取一个条目,并且仅在成功时使用结果(这是完全正确的)。如果没有成功,例如因为遇到EOF,streamstate将设置为“失败”。这会导致读取循环终止,但也会导致该文件流上的任何后续操作失败,直到显式重置streamstate。因此,需要在该循环之后调用
writeradb.clear()

还有一些注意事项:

  • 传递
    char filename[MAX]
    不会将数组传递给函数!相反,它与
    char*filename
    相同,即修改该参数将使其在调用函数中可见。使用
    std::string
    ,使用其
    c_str()
    成员函数获取
    fstream
    的指针
  • 您没有读取最后一个值,但读取失败!您的代码应该已经检测到了这一点。此外,它似乎已经读取了最后一个值,因为您正在重用临时结构。通常,最好将变量范围的大小保持在尽可能小的范围内。在函数开头声明所有变量的习惯到旧时代(上个世纪),这是C代码所必需的,但不是C++代码。在这种情况下,我甚至可以想象将整个程序分成两个或三个函数。
  • 将结构转储到文件不能在不同的计算机之间移植,有时甚至不能在同一台计算机上的不同编译器之间移植。因此,有多个序列化库可以正确地执行此操作

非常感谢,我花了大约2个小时来调试它
void updatebfile(char filename[MAX])
{

fstream writereadb;

char cont='y';
char filenameb [MAX];
int i=1;
int record;

student s;

strcpy(filenameb,filename);
strcat(filenameb,".dat");

writereadb.open(filenameb,ios::in | ios::out | ios::binary  );


cout<<"------------------------------"
    <<endl;

cout<<"Begin updating of binary file "
    <<filenameb
    <<endl
    <<endl;

cout<<"Information for student file"
    <<endl
    <<endl;


while ( writereadb.read (reinterpret_cast <char *>(&s), sizeof (s) ) )
{

    cout<<i
        <<'\t'
        <<s.identity
        <<" "
        <<s.name
        <<endl;

    i++;


}

do
{


cout<<endl
    <<"Update record: ";
cin>>record;

cout<<endl
    <<"Student id: ";



writereadb.seekg ((record - 1) * sizeof(s), ios::beg);//problem is here
writereadb.read (reinterpret_cast <char *>(&s), sizeof (s));//always reading last value


cout<<s.identity
    <<endl;





cout<<"Update the name: ";
cin>>s.name;



writereadb.seekp((record-1)*sizeof(student),ios::beg);  
writereadb.write (reinterpret_cast <const char *>(&s), sizeof (s));

cout<<"Any more update (y/n) :";
cin>>cont;

}while (cont=='y');

writereadb.close();







}