sscanf()未写入变量

问题描述 投票:0回答:1

我正在从csv文件中读取数据,以便将其复制到数组中。数据格式为int1,String1,String2,String3,String4,int2我使用sscanf分隔这些参数。

此函数将文件指针和动态分配的数组作为参数,并将数据从文件复制到数组。我正在使用6个中间变量来保存读取的数据以检查其有效性。

//temporary variables for checks
        int  month = 0;
        char origin[4];
        char destination[4];
        char airline[4];
        char type[10]; //will only be used to parse the file
        int  passengerCount = 0;


/******************************************************************************************************/
    while(!feof(fileIn))
    {
        fgets(buffer, 1000, fileIn);
        if(buffer == NULL)
            continue;

        printf("\n%s", buffer); //Using this to check correct read

                sscanf(buffer,"%d,%[^,],%[^,],%[^,],%[^,],%d", &month, origin, destination, airline, type, &passengerCount);

        if (strlen(airline) > 2) //skips value if airline code is over 2 chars long
        {
            skipCount++;
            continue;
        }

        printf("Checks: %d,%s,%s,%s,%s,%d\n", month, origin, destination, airline, type, passengerCount); //using this to check correct read from buffer
/*******************************************************************************************************/

[运行时,这段代码应读取文件,检查airline是否长于2个字符,并跳过那些。由于某些原因,sscanf()会正确写入所有变量,但airline始终为空。.csv文件中的前三行:

6,JFK,LHR,VS,Passengers,85790
6,JFK,CDG,AF,Passengers,85324
6,LGA,YYZ,AC,Passengers,82389

这是输出:

Buffer: 6,JFK,LHR,VS,Passengers,85790
Checks: 6,JFK,LHR,,Passengers,85790

Buffer: 6,JFK,CDG,AF,Passengers,85324
Checks: 6,JFK,CDG,,Passengers,85324

Buffer: 6,LGA,YYZ,AC,Passengers,82389
Checks: 6,LGA,YYZ,,Passengers,82389
c string scanf
1个回答
0
投票

我看到的问题是:在执行[[sscanf时,您具有以下内容:

sscanf(buffer,"Buffer: %d,%[^,],%[^,],%[^,],%[^,],%d", &month, origin, destination, airline, type, &passengerCount);
这里是示例解决方案:

#include <cstdio> int main(){ //temporary variables for checks int month = 0; char origin[4]; char destination[4]; char airline[4]; char type[10]; //will only be used to parse the file int passengerCount = 0; FILE *fileIn = fopen("sample.txt","r"); char buffer[100]; int skipCount = 0; while(!feof(fileIn)) { if(fgets(buffer, 100, fileIn) == NULL) continue; printf("\n%s\n", buffer); //Using this to check correct read sscanf(buffer,"%d,%[^,],%[^,],%[^,],%[^,],%d", &month, origin, destination, airline, type, &passengerCount); if (strlen(airline) > 2) //skips value if airline code is not 2 chars long { skipCount++; continue; } printf("Checks: %d,%s,%s,%s,%s,%d\n", month, origin, destination, airline, type, passengerCount); //using this to check correct read from buffer } }

我的sample.txt文件如下:

6,JFK,LHR,VS,Passengers,85790 6,JFK,CDG,AF,Passengers,85324 6,LGA,YYZ,AC,Passengers,82389

执行此命令时,我得到:

6,JFK,LHR,VS,Passengers,85790 Checks: 6,JFK,LHR,VS,Passengers,85790 6,JFK,CDG,AF,Passengers,85324 Checks: 6,JFK,CDG,AF,Passengers,85324 6,LGA,YYZ,AC,Passengers,82389 Checks: 6,LGA,YYZ,AC,Passengers,82389

[另外,我认为您应该在解析这样的字符串时使用strtok。它要干净得多。 
© www.soinside.com 2019 - 2024. All rights reserved.