尝试将数字从.txt写入二进制文件

时间:2019-04-06 05:55:29

标签: c binary fwrite

我当前正在阅读以下文本文件:

New York,4:20,3:03
Kansas City,12:03,3:00
North Bay,16:00,0:20
Kapuskasing,10:00,4:02
Thunder Bay,0:32,0:31

我将城市名fprintf移到了一个新的.txt文件中,该文件可以正常工作,但是我试图花费时间将其打印到二进制文件中,并卡住我的位置有问题。任何帮助将不胜感激。我需要将“ New York”的时间以04、20的形式存储在2字节的值中,并且在解析时有具体的问题。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable: 4996)

// a function to remove the trailing carraige return
void clearTrailingCarraigeReturn(char* buffer);

/* == FUNCTION PROTOTYPES == */

/* == CONSTANTS == */

// MAIN
typedef struct
{
    char cityName[20];
    short flightTime;
    short layoverTime;
} Flight;

Flight parseFlight(char* line) {

    char delimiter[2] = ",";
    Flight flight;
    char* token = strtok(line, delimiter);
    int i = 0;
    while (token != NULL)
    {
        if (i == 0)
        {
            strcpy(flight.cityName, token);
        }
        if (i == 1)
        {
            flight.flightTime = atoi(token);
        }
        if (i == 2)
        {
            flight.layoverTime = atoi(token);
        }
        token = strtok(NULL, delimiter);
        i++;
    }

    return flight;
}

int main(int argc, char* argv[])
{
    FILE *fpIn, *fpOut, *fbOut;
    char line[80];

    Flight flight;

    fpIn = fopen(argv[1], "r");
    fpOut = fopen("theCities.txt", "w+");
    fbOut = fopen("theTimes.dat", "wb+");
    while (fgets(line, 1024, fpIn) > 0)
    {

        clearTrailingCarraigeReturn(line);
        printf("  >>> read record [%s]\n", line);

        flight = parseFlight(line);
        fprintf(fpOut, "%s\n", flight.cityName);
        fwrite(&flight.flightTime, sizeof(short), 1, fbOut);
        fwrite(&flight.layoverTime, sizeof(short), 1, fbOut);

    }





fclose(fpIn);
fclose(fpOut);
fclose(fbOut);

}





// This function locates any carraige return that exists in a record
// and removes it ...
void clearTrailingCarraigeReturn(char* buffer)
{
    char* whereCR = strchr(buffer, '\n');
    if (whereCR != NULL)
    {
        *whereCR = '\0';
    }
}

1 个答案:

答案 0 :(得分:0)

也许是这样的:

  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>

  typedef struct Time_s
     {
     unsigned char hours;
     unsigned char minutes;
     } Time_t;

  typedef struct Flight_s
     {
     char  cityName[20];
     Time_t flightTime;
     Time_t layoverTime;
     } Flight_t;

  // a function to remove the trailing carraige return
  void clearTrailingCarraigeReturn(char *buffer)
     {
     int more;

     do {
        size_t index;

        index = strlen(buffer);
        if(!index)
           break;

        --index;
        switch(buffer[index])
           {
           case '\n':
           case '\r':
              buffer[index] = '\0';
              more = 1;
              break;

           default:
              more = 0;
              break;
           }
        } while(more);

     return;
     }

  int ParseTime(char *timeStr, Time_t *time)
     {
     int rCode=0;
     char *next;

     time->hours = (unsigned char)strtoul(timeStr, &next, 10);
     if(':' == *next)
        {
        ++next;
        time->minutes = (unsigned char)strtoul(next, NULL, 10);
        }

     return(rCode);
     }

  Flight_t parseFlight(char* line)
     {
     char      delimiter[2] = ",";
     Flight_t  flight;
     char     *token = strtok(line, delimiter);
     int       i = 0;

     while(token)
        {
        switch(i)
           {
           case 0:
              strcpy(flight.cityName, token);
              break;

           case 1:
              ParseTime(token, &flight.flightTime);
              break;

           case 2:
              ParseTime(token, &flight.layoverTime);
              break;
           }

        token = strtok(NULL, delimiter);
        i++;
        }

     return(flight);
     }

  int main(int argc, char* argv[])
     {
     int rCode=0;
     FILE *fpIn=NULL, *fpOut=NULL, *fbOut=NULL;
     char line[80+1];
     Flight_t flight;

     if(argc < 2)
        {
        fprintf(stderr, "ERROR: argc < 2\n");
        goto CLEANUP;
        }

     fpIn = fopen(argv[1], "r");
     if(!fpIn)
        {
        fprintf(stderr, "ERROR: fopen(\"%s\",\"r\")\n", argv[1]);
        goto CLEANUP;
        }

     fpOut = fopen("theCities.txt", "w+");
     if(!fpOut)
        {
        fprintf(stderr, "ERROR: fopen(\"theCities.txt\",\"w+\")\n");
        goto CLEANUP;
        }

     fbOut = fopen("theTimes.dat", "wb+");
     if(!fbOut)
        {
        fprintf(stderr, "ERROR: fopen(\"theTimes.dat\",\"wb+\")\n");
        goto CLEANUP;
        }

     while(fgets(line, sizeof(line), fpIn) > 0)
        {
        clearTrailingCarraigeReturn(line);
        flight = parseFlight(line);

        printf("%s,%02hhu:%02hhu,%02hhu:%02hhu\n",
           flight.cityName,
           flight.flightTime.hours, flight.flightTime.minutes,
           flight.layoverTime.hours, flight.layoverTime.minutes
           );

        fprintf(fpOut, "%s\n", flight.cityName);
        fwrite(&flight.flightTime, sizeof(Time_t), 1, fbOut);
        fwrite(&flight.layoverTime, sizeof(Time_t), 1, fbOut);
        }

  CLEANUP:

     if(fpIn)
        fclose(fpIn);

     if(fpOut)
        fclose(fpOut);

     if(fbOut)
        fclose(fbOut);

     return(rCode);
     }