在C中将文本文件中的各种数据存储到数组中(并打印出来)

时间:2016-06-21 20:18:19

标签: c arrays printf scanf

好的,感谢迄今为止帮助过的所有人,我已经设法解决了我原来的问题。现在,除dSeatPrice变量外,我的输出看起来很完美。我的输出目前正在为票价打印全零,即使它们的值明显不同。现在我正在使用eclipse和cygwin来处理这个程序,该程序已更新并显示如下。 getFlights()printFlights()是完成主要工作的地方:

FILE *pFileCust;               // stream Input for Customer Reservation data
FILE *pFileFlight;              // stream input for Flight Definitions


int main(int argc, char *argv[])
{
    char *pszCustomerFileName = NULL;
    char *pszFlightFileName = NULL;

    Flight flightM[MAX_FLIGHTS];
    int flightMentries;
    flightMentries = 0; //may need to delete

    // Process the command switches
    processCommandSwitches(argc, argv,  &pszCustomerFileName, &pszFlightFileName);

    // open the Customer Reservation stream data file
    if (pszCustomerFileName == NULL)
        exitError(ERR_MISSING_SWITCH, "-c");

    pFileCust = fopen(pszCustomerFileName, "r");
    if (pFileCust == NULL)
        exitError(ERR_CUSTOMER_RES_FILENAME, pszCustomerFileName);

    // open the Flight stream data file
    if (pszFlightFileName == NULL)
        exitError(ERR_MISSING_SWITCH, "-f");

    pFileFlight = fopen(pszFlightFileName, "r");
    if (pFileFlight == NULL)
        exitError(ERR_FLIGHT_FILENAME, pszFlightFileName);

    // process the Reservations
    printFlights(flightM, flightMentries);
    getFlights(flightM);
    processReservations(flightM, flightMentries);

    fclose(pFileCust);
    printf("\n");    // included so that you can put a breakpoint on this line

    fclose(pFileFlight);
    printf("\n");    // included so that you can put a breakpoint on this line

    return 0;
}

int getFlights(Flight flightM[]){
    int i;
    i = 0;
    int scanCnt;
    char szInputBuffer[100];
    while (fgets(szInputBuffer, 100, pFileFlight) != NULL){
        scanCnt = sscanf(szInputBuffer, "%10s %3s %3s %10s %4d %[^\n]f\n", flightM[i].szFlightId,
                flightM[i].szFrom,
                flightM[i].szDest,
                flightM[i].szDepartTm,
                &flightM[i].iAvailSeatCnt,
                &flightM[i].dSeatPrice);
        printf("   %10s   %3s  %3s  %5s  %1d    %1f\n",flightM[i].szFlightId, flightM[i].szFrom, flightM[i].szDest, flightM[i].szDepartTm, flightM[i].iAvailSeatCnt, flightM[i].dSeatPrice);
        i++;
    }
    printf("Scanned %d\n", scanCnt);
    return 0;
}

void printFlights(Flight flightM[], int iFlightCnt){
    printf("Original Flight Information\n");
    printf("   Flight Id    From Dest Depart Avail Unit Price\n");
}

此外,这里是我试图读取的信息的typdef struct(在h文件中定义):

typedef struct
{
    char szFlightId[11];                    // Flight Identifier
    char szFrom[4];                         // Origin of the flight
    char szDest[4];                         // Destination of the flight
    char szDepartTm[6];                     // Departure Time (24 hour clock) HH:MM
    int  iAvailSeatCnt;                     // Number of available seats
    double dSeatPrice;                      // Price per seat (i.e., unit price)
} Flight;

最后,这是文本文件中的第一行输入。共有8行:

H100.15005 SAT HOU 08:00 4 65.00

0 个答案:

没有答案