使用sscanf从文本文件中读取由空格分隔的字符串和整数

时间:2019-03-02 03:30:43

标签: c input linked-list scanf stdin

我正在使用%[]

获取完整的字符串

我认为在这种情况下这是错误的。第二个条件是从文件中的名称到文件中不打印的数字,它只打印0。在我设置的计数器中,它说它仅扫描了文本一次,应该两次;对于最终代码,我需要扫描3次。

这是input.txt的示例

Ant,Adam   10 5
Mander,Sally 4 3

这是代码示例

FILE *pFilePerson;
char szInputBuffer[100];
int iScanfCnt;
Simulation newSim = newSimulation();
NodeLL linkList;
LinkedList list = newLinkedList();
Event event;
Person person;

pFilePerson = stdin;
while(fgets(szInputBuffer, sizeof szInputBuffer, pFilePerson) != NULL)
{
        iScanfCnt = sscanf(szInputBuffer,"%[] %d",
                    person.szName,
                    person.iDepartTmUnits);                 

    printf("%-7s\n", person.szName);
    printf("%d\n", person.iDepartTmUnits);

                // Check for bad input.  scanf returns the number of valid conversions
        if (iScanfCnt < 2)
        {
            printf("\tWarning: Expected Name and Depart Units, received %d successful values\n"
                , iScanfCnt);
            continue;
        }
}

这是我得到的输出

Ant,Adam   10 5

4199680
    Warning: Expected Name and Depart Units, received 1 successful values
Mander,Sally 4 3

4194314
    Warning: Expected Name and Depart Units, received 1 successful values
King,May  6 6

0 个答案:

没有答案