C fscanf在csv文件中读取“,,”的问题

时间:2018-02-08 18:10:35

标签: csv scanf

基本上,它会在控制台中输出所有内容,除非它在逗号之间出现空白区域。直到最后都没有空的逗号,所以直到那时所有内容都打印得很好,但是它会打一个空的逗号,下一个要打印的数据应该是“N”或“Y”,但它会打印一个“s”。有办法解决这个问题吗?

#include <stdio.h>//
#include <stdlib.h>//
#include <string.h>//
#include "airPdata.h"//

/*read a line from file or until EOF*/
void readLine(char buf[], FILE *fipo);
/*parse buffer to struct*/
int printData(airPdata* airport, FILE *fipo);
/*main function*/
int main(int argc, char *argv[])//Parameters in int for command line use
{
    FILE *fipo;//File pointer
    fipo = fopen(argv[1], "r");//Opens fipo file pointer with read permission

    if (fipo == NULL)
    {
        fprintf(stderr, "etl ERROR: File %s not found.", argv[1]);//Prints error message STDERR indicating which specific file is missing
        return (1);//Exits safely
    }//Ends if statement

    char buf[500]; /*buffer for lines from file*/
    int valid; /*check if line is valid*/
    airPdata* airport;//Declaring airPdata

    airport = (airPdata*)malloc(sizeof(airPdata)); /* allocated the memory needed */
    airport->siteNumber = (char*)malloc(500 * sizeof(char));
    airport->LocID = (char*)malloc(500 * sizeof(char));
    airport->fieldName = (char*)malloc(500 * sizeof(char));
    airport->city = (char*)malloc(500 * sizeof(char));
    airport->state = (char*)malloc(500 * sizeof(char));
    airport->useless1 = (char*)malloc(500 * sizeof(char));
    airport->useless2 = (char*)malloc(500 * sizeof(char));
    airport->useless3 = (char*)malloc(500 * sizeof(char));
    airport->latitude = (char*)malloc(500 * sizeof(char));
    airport->longitude = (char*)malloc(500 * sizeof(char));
    airport->useless4 = (char*)malloc(500 * sizeof(char));
    airport->useless5 = (char*)malloc(500 * sizeof(char));
    airport->useless6 = (char*)malloc(500 * sizeof(char));
    airport->useless7 = (char*)malloc(500 * sizeof(char));

    printf("%-13s%-11s%-45s%-45s%-7s%-16s%-16s%-5s\n", "FAA Site#", "Short Name", "Airport Name", "City", "ST", "Latitude", "Longitude", "Tower");
    printf("%-13s%-11s%-45s%-45s%-7s%-16s%-16s%-5s\n", "=========", "==========", "============", "====", "==", "========", "=========", "=====");
    printData(airport, fipo);
    fclose(fipo);//Closes the fipo file

    free(airport->siteNumber);//Returns previously allocated memory
    free(airport->LocID);//Returns previously allocated memory
    free(airport->fieldName);//Returns previously allocated memory
    free(airport->city);//Returns previously allocated memory
    free(airport->state);//Returns previously allocated memory
    free(airport->useless1);//Returns previously allocated memory
    free(airport->useless2);//Returns previously allocated memory
    free(airport->useless3);//Returns previously allocated memory
    free(airport->latitude);//Returns previously allocated memory
    free(airport->longitude);//Returns previously allocated memory
    free(airport->useless4);//Returns previously allocated memory
    free(airport->useless5);//Returns previously allocated memory
    free(airport->useless6);//Returns previously allocated memory
    free(airport->useless7);//Returns previously allocated memory
    free(airport);//Returns previously allocated memory
    return 0;//Exits
}//Ends main function

int printData(airPdata* airport, FILE *fipo)
{
    char line[128];
    int i;
    for (i = 0; i < 128; i++)
    {
        i = 0;
        while ( fgets ( line, sizeof line, fipo ) != NULL )
        {
            fscanf(fipo, "%[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %c", airport->siteNumber, airport->LocID, airport->fieldName, airport->city, airport->state, airport->useless1, airport->useless2, airport->useless3, airport->latitude, airport->longitude, airport->useless4, airport->useless5, airport->useless6, airport->useless7, &airport->controlTower);
            printf("%-13s%-11s%-45s%-45s%-7s%-16s%-16s%-5c\n", airport->siteNumber, airport->LocID, airport->fieldName, airport->city, airport->state, airport->latitude, airport->longitude, airport->controlTower);//Displays output
            i ++;
        }
        return 1;
    }
}

0 个答案:

没有答案