【问题标题】:C- Error reading and printing input from a text fileC- 从文本文件读取和打印输入时出错
【发布时间】:2016-06-13 20:15:11
【问题描述】:

我在以正确格式打印信息时遇到问题。我的程序应该读取有关航班预订的信息。该信息应作为行程打印出来,如下所示:

    ******************* Flight Reservation Request  **********************
petem@xyz.net Pete Moss (M 1986/01/01)
123 Boggy Lane
New Orleans, LA 70112
   Flight       Seats      
   H100.15005      2      
   H222.15005      2
   H200.15010      2
   H333.15010      2       
******************* Flight Reservation Request  **********************
pcorn@abc.net Pop Corn (M 1957/02/02)
456 Kernel
San Antonio, TX 78210
   Flight       Seats      
   H222.15005      1
   HXXX.XXXXX      1
   H333.15010      1    

这里是大部分重要的代码。我只在processReservations()工作。

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cs1713p1.h"
FILE *pFileCust;               // stream Input for Customer Reservation data

void processCommandSwitches(int argc, char *argv[], char **ppszCustomerFileName);
void processReservations();


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

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

    // 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);

    // process the Reservations
    processReservations();

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

/****** Where my errors are coming from *****/

void processReservations()
{

Customer customer;                        // student structure for holding student data
FlightRequest flight;
char szInputBuffer[100];  // input buffer for fgets
int iScanfCnt;                          // scanf returns the number of successful inputs


  // read data input lines of text until EOF.  fgets returns NULL at EOF
    while (fgets(szInputBuffer, 100, pFileCust) != NULL)
    {
        // if the line is just a line feed, skip it.
        if (szInputBuffer[0] == '\n')
            continue;

        iScanfCnt = sscanf(szInputBuffer, "%1c %10s %50s %30[^\n] %50[^,] %2s %2s %5s %10s %d\n"
            , &customer.cGender
            , customer.szBirthDt
            , customer.szEmailAddr
            , customer.szFullName
            , customer.szStreetAddress
            , customer.szCity
            , customer.szStateCd
            , customer.szZipCd
            , flight.szFlightId
            , &flight.iRequestSeats);

        // Check for bad input.  scanf returns the number of valid conversions
        if (iScanfCnt < 10)
        {
            printf("invalid input when reading data, only %d valid values. \n"
                , iScanfCnt);
            printf("\tdata is %s\n", szInputBuffer);
            //return ERR_MISSING_SWITCH;
        }

        printf("******************* Flight Reservation Request  **********************\n");
        printf("%s %s", customer.szEmailAddr, customer.szFullName);
        printf("(%c %s)", customer.cGender, customer.szBirthDt);
        printf("%s, %s %s\n", customer.szStreetAddress, customer.szCity, customer.szZipCd);
        printf("   Flights  Seats\n");
        printf("   %s       %d\n", flight.szFlightId, flight.iRequestSeats);
    }
    fclose(pFileCust);
}

这是我的输出对于第一个客户的一部分的样子:

invalid input when reading data, only 4 valid values. 
    data is M 1986/01/01 petem@xyz.net Pete Moss

******************* Flight Reservation Request  **********************
petem@xyz.net Pete Moss
(M 1986/01/01), a a
   Flights  Seats
invalid input when reading data, only 4 valid values. 
    data is 123 Boggy Lane,New Orleans,LA,70112

******************* Flight Reservation Request  **********************
Boggy Lane,New Orleans,LA,70112
(1 23), a a
   Flights  Seats

显然,它又长又乱,但我对 C 很陌生,必须弄清楚出了什么问题。一个问题是“Flight Reservation Request”行打印太多次。似乎每次从输入文件中读取数据变量时,while 循环都会从头开始重复,我不知道如何解决这个问题。我在另一个网站上发现了类似的问题,一位用户提出了建议

使用一个临时变量/结构来测试我在输入文件的哪一行,所以每次我只是测试我在文件中的位置时,我不会覆盖我的最终客户结构。另一个想法是根据我可以从输入文件中做出的假设来构建我的代码。例如,如果我成功读取了新客户的第一行,那么我知道接下来的行将遵循其他格式,因此我不必检查每个后续行是否是新客户的第一行。

我不明白这些建议的解决方案是什么意思或如何修复此代码,但我一直在努力解决这个问题,但没有任何进展。 我的所有变量声明中还包含一个 .h 文件,但是每个变量名称前的小写字母表示变量的数据类型(sz=string、c=char、i=int)。有什么问题?

【问题讨论】:

  • 我不明白你怎么能期望通过一个 scanf() 呼叫来读取可变数量的(航班号、座位数)对。
  • 城市 (%2s) 对应的字段描述符看起来不对。此外,您的意图似乎是跳过地址中的逗号,但您没有这样做。如果您的格式与文字不匹配,则它们将包含在其中一个字段中,否则将导致匹配失败。
  • 您一次读取一行输入,但试图从一行中扫描几行数据。
  • 所以我应该为每一行写一个scanf 声明吗?

标签: c string printf


【解决方案1】:

程序报错

读取数据时输入无效,只有4个有效值

这是因为输入缓冲区

char szInputBuffer[100];

太小了。

中字段宽度限制的总和
iScanfCnt = sscanf(szInputBuffer, "%1c %10s %50s %30[^\n] %50[^,] %2s %2s %5s %10s %d\n", ...);

更大。所以很有可能

while (fgets(szInputBuffer, 100, pFileCust) != NULL)

会将每行输入分成几个较小的字符串,您不会检测到这些字符串(缺少尾随newline)。但是 sscanf 确实失败了。

不要吝啬。使用宽大的缓冲区,例如

char szInputBuffer[4096];

【讨论】:

  • 好吧,我刚刚将其更改为 4096,但仍然出现相同的错误...我仍在尝试让它按照我需要的方式打印出来...谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多