解析CString中的信息
Parsing information in a CString
当我在cstring中解析信息时,我遇到了问题。
我有一个类似的数据文件。
Hammer 1 1.00
Drill 2 2.00
Saw 3 3.00
Level 4 4.00
Pick 5 5.00
Nail 6 6.00
Mallet 7 7.00
Putty 8 8.00
Stapler 9 9.00
Desk 10 10.00
Table 11 11.00
Marker 12 12.00
数据文件可以在每行开始或每个值之间使用空格。该数据文件的格式为名称、数量和价格。在正确解析后,Name、Quantity和Price都存储在一个结构数组中。
我需要在这个问题上使用cstring,因为我现在正在从一本书中学习它们。
这是我目前为止的算法,我想我在某个地方遇到了while循环,程序就挂起了。
我的代码就是这样,我的想法就是这样。虽然来自数据文件的行readLineIndex
处的值不是空字符,但请执行内部操作。如果cstring在cstring中的任何地方都有空白行,请向我的readLineIndex
添加1,如果字符是字母数字,则将其复制到临时数组中,并移动到下一个字符并重复上述操作,直到获得空白。一旦程序遇到空白,将该值存储到正确的结构变量数组中。程序会知道它的值是多少,因为我从输入文件中只有3个变量,我只是说,如果我在计数器上检索的第一个变量是1,那么将它存储到数组[structurecoter]中。product,如果是2,将其存储到数组[structureter]中。如果是3,将其存储到数组[structureter]. unitprice中。此外,每次解析完成且读取文件不在行尾时,structurecoter仅上升1。
这就是我的函数被调用的方式。MAX_INVENTORY = 12.
while(!inFile.getline(readLine, MAX_CHARACTERS, 'n').eof() && structureCounter < MAX_INVENTORY)
{
parseInformation(readLine,inventory, readLineIndex, structureCounter);
structureCounter += 1;
}
函数算法。
void parseInformation(OneLine readLine,OneItem inventory[], int & readLineIndex, int & structureCounter)
{
int tempIndex = 0;
int valueCounter = 0;
OneLine tempArray;
while(readLine[readLineIndex] != ' ')
{
while(readLine[readLineIndex] == ' ')
{
readLineIndex += 1;
}
while(isalnum(readLine[readLineIndex]))
{
tempArray[tempIndex] = readLine[readLineIndex];
tempIndex += 1;
readLineIndex += 1;
}
if(valueCounter == 0)
{
strcpy(inventory[structureCounter].product,tempArray);
valueCounter += 1;
}
if(valueCounter == 1)
{
inventory[structureCounter].quantity = atoi(tempArray);
valueCounter += 1;
}
if (valueCounter == 2)
{
inventory[structureCounter].unitPrice = atof(tempArray);
}
}
return;
首先,在调用strcpy
之前,您没有向tempArray
添加空终止符。其次,在复制tempArray
之后,需要将tempIndex
重置为0
,以便可以将其用于下一个字段。第三,在从tempArray
复制到product
之后,将valueCounter
从0
增加到1
。因此,下一个测试if (valueCounter == 1)
将成功,它将尝试将解析后的tempArray
放入quantity
;然后将其增加到2
,下一个测试将成功,并且您将尝试将其放入unitPrice
。您应该使用else if
或switch
语句,这样您只执行其中一个;您还可以在完成所有测试后增加valueCounter
。
void parseInformation(OneLine readLine,OneItem inventory[], int & readLineIndex, int & structureCounter) {
int tempIndex = 0;
int valueCounter = 0;
OneLine tempArray;
while(readLine[readLineIndex] != ' ') {
while(readLine[readLineIndex] == ' ') {
readLineIndex += 1;
}
while(isalnum(readLine[readLineIndex])) {
tempArray[tempIndex] = readLine[readLineIndex];
tempIndex += 1;
readLineIndex += 1;
}
tempArray[tempIndex] = 0; // Add null terminator
switch (valueCounter) {
case 0:
strcpy(inventory[structureCounter].product,tempArray);
break;
case 1:
inventory[structureCounter].quantity = atoi(tempArray);
break;
case 2:
inventory[structureCounter].unitPrice = atof(tempArray);
break;
}
valueCounter++;
}
return;
}
- 正在查找文档以获得PS4平台的C++中的设备信息
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何设置一个范围来提取我想要获得的信息
- 在C++中使用Cstring反转字符串
- 系统参数信息A 与 SPI_GETMOUSE 返回 0
- 为什么 gcovr 会生成空覆盖率统计信息?
- 如何转换真实路径 CString c++
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么
- 调试符号中缺少 QObject 类信息(编辑但存在其他 Qt 类)
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- Pisarze - 来自波兰奥林匹克信息学的数据分析任务
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- C++20 个模块在 clang (Windows):在最简单的示例中键入信息错误
- C++ 如何暂停控制台输出,以便用户可以在继续之前阅读信息
- 如何从WebAssembly模块检测浏览器信息?
- Visual Studio 2017 C++,不能使用 typeid() 获取信息对象,缺少指针?;
- 如何实现具有多个平台__FILE__和__LINE__信息的 C/C++ 可变参数日志记录宏?
- 解析CString中的信息