gpt4 book ai didi

c++ - 尝试将记录中的数据读入程序时难以捉摸的错误(C++)

转载 作者:行者123 更新时间:2023-11-27 23:40:48 25 4
gpt4 key购买 nike

我正在尝试编写一个程序来读取数据并将数据写入数据文件。出于某种原因,我无法弄清楚,它无法从文件中读取字符,只有名称和数字名称。我需要字符,以便显示项目列表。

文件中的数据是一条记录,struct是this,获取char时出错。

struct Data
{
char Genre;
string Productname;
int Numberofproducts;
int Numberofproductsleft;
int Numberofproductssold;
bool Morethantwo;
bool Noticeseen;
float price;
};

//Lists genre to choose from
string listofgenres[] = {"1) Book", "2) Movie", "3) Other", "4) Delete Entry" };

这是我命名为 input 的 void 函数中的代码,它将数据写入文件。这是一本书,还有其他三个选项,这就是为什么他们有一个字符来标识数据是什么类型。

Data put;
int choice;
string genrechosen;
ofstream F;

int count = 0;

cout<<"What genre is the item? (Type the option or enter 1,2, or 3): "<<endl;
while(count < logs)
{
cout<<listofgenres[count]<<endl;
count++;
}

cin >> genrechosen;

genrechosen = validate(genrechosen, 'I');

if(genrechosen == "1" || genrechosen == "Books" || genrechosen == "books" || genrechosen == "Book" || genrechosen == "book" || genrechosen == "B" || genrechosen == "b")
{
F.open(filename, ios::out|ios::app|ios::binary);
put.Genre = '~';

cout<<"What is the name of the book: "<<endl;
cin >> put.Productname;

cout<<"How many "<<put.Productname<<" do you have: "<<endl;
cin >> put.Numberofproducts;
neg(put.Numberofproducts);

cout<<"How much are you selling "<<put.Productname<<" for? (If you don't know , just put 0. Don't put a $): "<<endl;
cin >> put.price;
neg(put.price);

put.Numberofproductssold = 0;
put.Numberofproductsleft = put.Numberofproducts;

if(put.Numberofproducts >= 2)
put.Morethantwo = true;
else
put.Morethantwo = false;

put.Noticeseen = false;

F.write( (const char *)&put , sizeof(put));
F.close();
}

我已经使用了 reinterpret_cast 但它失败了,这就是 F.write 是那样的原因。

尝试从文件中读取数据时出现错误(在我调用 Modify 的 void 函数中),我也尝试过使用常规文本文件,但仍然遇到同样的问题。

     Data change;
string answer;
char genrechosen;
int sold;
int continuee;
int continued;
int passedrecords = 0;
vector <int> position;

f1.open(filename, ios::in|ios::out|ios::ate|ios::binary);

int count = 1;

cout<<"What genre is the item you are looking for? "<<endl;
for(int val = 0; val <= logs; val++)
cout<<listofgenres[val]<<endl;

cin >> answer;

//Validates input
if(answer != "4" || answer != "Delete Entry" || answer != "delete entry" || answer != "Delete" || answer != "delete" || answer != "D" || answer != "d" )
answer = validate(answer, 'I'); // Uses I for the char variable because they both use same list, so there is no point in creating new char for it.

//Allows for list of only that genre to be shown
else if(answer == "1" || answer == "Books" || answer == "books" || answer == "Book" || answer == "book" || answer == "B" || answer == "b")
genrechosen = '~';

else if(answer == "2" || answer == "Movies" || answer == "movies" || answer == "Movie" || answer == "movie" || answer == "M" || answer == "m")
genrechosen = '!';

else if(answer == "3" || answer == "Other" || answer == "other" || answer == "O" || answer == "o")
genrechosen = '@';
else
genrechosen = '_';

//Displays List of items in that genre
if(genrechosen == bs)
cout<<"Which book's data will you be modifying? (Type in the number):"<<endl;
if(genrechosen == ms)
cout<<"Which movie's data will you be modifying? (Type in the number):"<<endl;
if(genrechosen == orr)
cout<<"What item's data will you be modifying? (Type in the number):"<<endl;
if(genrechosen == de)
{
deleterec();
return;
}

//Read records until eof
while( f1.read( (char *)&change, sizeof(change)) )
{
if(change.Genre == genrechosen)
{
cout<<count<<") "<<change.Productname<<endl;
count++;
position.push_back(passedrecords);
}

(Personal comment)/* We need to know exactly what record in the file matches that genre, that way when the users chooses a number of the list, we can go and find that exact record. We don't want only 2 records to show up and then conclude the user wants the second record when they actually want the 37th record.*/

passedrecords++;

}

Tl;博士;

问题是当有多个记录时,while 循环只迭代一次。也改变。流派从来没有实际值,因此不会显示任何记录。我无法读取名称或字符数据,只能读取 double /整数。我正在阅读的 C++ 书说对不同的数据类型使用二进制文件,这是错误的吗?谢谢你,抱歉这么久了。顺便说一句,我正在使用代码块编译器。

编辑 1:我一次编写一个结构后遇到此错误:

f1.read( (char *)&change.Genre, sizeof(change.Genre));
uint32_t size = change.Productname.length();
f1.read( (char *)&size, sizeof(size));
f1.read( change.Productname.data() , size); //Problematic Line
f1.read( (const char *)&change.Numberofproducts, sizeof(change.Numberofproducts));
f1.read( ( const char *)&change.Numberofproductsleft, sizeof(change.Numberofproductsleft));
f1.read( ( const char *)&change.Numberofproductssold, sizeof(change.Numberofproductssold));
f1.read( ( const char *)&change.Morethantwo, sizeof(change.Morethantwo));
f1.read( ( const char *)&change.Noticeseen, sizeof(change.Noticeseen));
f1.read( ( const char *)&change.price, sizeof(change.price));

给我错误从 const char 到 char 的无效转换

最佳答案

字符串,就像任何可能包含指针的对象一样,不能用 fwrite 写入以供以后安全重构。快速而肮脏的修复(通常不推荐)是替换:

string Productname;

 char Productname[100];

或者,更好的是,一次写入一个字段。字符串字段可以写成size+characters,像这样:

 F.write( (const char *)&put.Genre, sizeof(put.Genre)); 
uint32_t size = put.Productname.length();
F.write( (const char *)&size, sizeof(size));
F.write(put.Productname.data(), size);
F.write( (const char *)&put.Numberofproducts, sizeof(put.Numberofproducts));
//... and so on

阅读也应如此。

注意:向文件写入和读取对象的过程通常称为序列化和反序列化。例如boost serialization .

注意2:为了文件格式的可移植性,最好写成int32_t, uint32_t, int64_t , ...,而不是普通的 intlong。这是因为 long(和类似的)在不同的系统上有不同的字节数。

如果您打算使用 PC 以外的任何设备,您还应该注意不同的系统可能有不同的字节顺序(大端或小端)。这种顺序差异使得序列化甚至 uint32_t 都无法跨此类系统移植。为了可移植性,最好将uint32_t对象按位操作分解成字节,并将这些字节序列化。

对于有符号类型,事情甚至更复杂,因为在某些专用系统上可能的最小 int32_t 值可能不同。幸运的是,这些并不常见。

关于c++ - 尝试将记录中的数据读入程序时难以捉摸的错误(C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55084414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com