解析CString中的信息

Parsing information in a CString

本文关键字:信息 CString 解析      更新时间:2023-10-16

当我在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之后,将valueCounter0增加到1。因此,下一个测试if (valueCounter == 1)将成功,它将尝试将解析后的tempArray放入quantity;然后将其增加到2,下一个测试将成功,并且您将尝试将其放入unitPrice。您应该使用else ifswitch语句,这样您只执行其中一个;您还可以在完成所有测试后增加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;
}