gpt4 book ai didi

c++ - 如何修复 ifstream 语法错误,以及 C++ 中可能丢失数据的转换错误?

转载 作者:行者123 更新时间:2023-11-28 04:21:40 25 4
gpt4 key购买 nike

我的代码有来自 Source.cpp 文件的代码。我在这个实验室的中间,我创建了一个跟踪相机商店的库存。它几乎完成了,但我遇到错误 C4244(使用 = on like 24 可能丢失数据)和错误 C3867(ios::base_fail/ios::basic_ifstream)。我会在出现错误的地方旁边写上“这里”,因为我无法告诉你是哪一行。这是有问题的代码:

#include<iostream>
#include<string>
#include<fstream>
#include "LinkedList.h"
using namespace std;

int main()
{
LinkedList ll; // Creating object of linked list

struct info record;

char dataToBeRead[100];

ifstream fp;

fp.open("fileInventory.txt");

if (fp.fail) !!!HERE!!!
{
cout << "The file cannot be opened\n";
}
else


{
while (fp.open, dataToBeRead[100] != NULL) !!!HERE!!!
{
char *token = strtok(dataToBeRead, " ");
strcpy(record.camera, token);
token = strtok(NULL, " ");
record.price = atof(token);
token = strtok(NULL, " ");
record.quantity1 = atoi(token); !!!HERE!!! (possible loss of data?)
token = strtok(NULL, " ");
record.quantity2 = atoi(token);
token = strtok(NULL, " ");
ll.addEnd(record);
ll.addReverse(record);

}
int ch = 0;
char camera[50];
float price;
int quantity1;
int quantity2;
do //Must open the menu
{
cout << "1. Display the inventory in alphabetic order:\n";
cout << "2. Display the inventory in reverse alphabetic order:\n";
cout << "3. Add an item to the inventory:\n";
cout << "4. Delete an item from inventory:\n";
cout << "5. Change any info for an item:\n";
cout << "6. Exit:\n";
cin >> ch;
switch (ch)
{
case 1:
ll.showList1();
break;
case 2:
ll.showList2();
break;
case 3:
cout << "Enter camera:\n";
cin >> camera;
cout << "Enter price:\n";
cin >> price;
cout << "Enter quantity1:\n";
cin >> quantity1;
cout << "Enter quantity2:\n";
cin >> quantity2;
strcpy(record.camera, camera);
record.price = price;
record.quantity1 = quantity1;
record.quantity2 = quantity2;
ll.addEnd(record);
ll.addReverse(record);
ll.showList1();
ll.showList2();
break;
case 4:
cout << "Enter camera name to be deleted:\n";
cin >> camera;
ll.removeValue1(camera);
ll.removeValue2(camera);
ll.showList1();
ll.showList2();
break;
case 5:
cout << "Enter the camera name for which you want to edit details:\n";
cin >> camera;
cout << "Enter price:\n";
cin >> price;
cout << "Enter quantity1:\n";
cin >> quantity1;
cout << "Enter quantity2:\n";
cin >> quantity2;
ll.editList(camera, price, quantity1, quantity2);
break;
case 6:
break;

}
} while (ch != 6); // Menu will run until the ch is not 6


ofstream myfile;
myfile.open("newlist.txt");

if (myfile.fail) !!!HERE!!!
{
exit(1);
}

}

return 1;
}

这显然是 fstream 的问题,但我不知道我做错了什么。而且我不确定为什么在那条线上可能会丢失数据。预先感谢您的帮助!

最佳答案

不用说了,这一行:

while (fp.open, dataToBeRead[100] != NULL)

不正确。

此外,要测试文件是否成功打开,您可以简单地这样做:

if (!fp)
{
// file failed to open
}

但总的来说,最简单的方法是完全不引入 char 数组,而是使用 std::string:

  std::string dataToBeRead;
while (std::getline(fp, dataToBeRead))
{
//...
}

鉴于 dataToBeRead 现在是 std::string,标记化的逻辑需要更改。这只需使用 std::istringstream 即可完成。并使用 operator >>

std::istringstream strm(dataToBeRead);
strm >> record.camera >> record.price >> record.quantity1 >> record.quantity2;
ll.addEnd(record);
ll.addReverse(record);

因此整个循环是这样的:

  #include <sstream>
//...
std::string dataToBeRead;
while (std::getline(fp, dataToBeRead))
{
std::istringstream strm(dataToBeRead);
strm >> record.camera >> record.price >> record.quantity1 >> record.quantity2;
ll.addEnd(record);
ll.addReverse(record);
}

这比使用 C 风格的 strtok 处理要容易和安全得多。


您也可以使用 operator >> 直接读取输入,从而跳过整个字符串创建过程:

  while (fp >> record.camera >> record.price >> record.quantity1 >> record.quantity2)
{
ll.addEnd(record);
ll.addReverse(record);
}

如果数据的解析很简单(在您的情况下,使用空格分隔),您通常希望使用此方法。如果比这更复杂,则可以使用 istringstream

关于c++ - 如何修复 ifstream 语法错误,以及 C++ 中可能丢失数据的转换错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55319756/

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