如何拆分文本文件并将其存储在数组中以用于C中的模板?

时间:2016-01-27 18:11:57

标签: c arrays

我是C的新手,我在拆分特定文本文件并将令牌存储在数组中时遇到问题。

这是我的txt文件,名为data.txt-,由" |"分开:

Public|Jane|Q|Ms.|600|Maple Street|Your Town|Iowa|12345
Penner|Fred|R|Mr.|123|that Street|Winnipeg|MB|R3T 2N2
Gardner|Mark|E|Mr.|76|The Avenue|Toronto|ON|M5W 1E6
Acton|Hilda|P|Mrs.|66|What Blvd|Winnipeg|MB|R3T 2N2

我的代码是:

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

#define INPUT_LENGTH 128
#define FIELD_LENGTH 30
#define NUM_FIELDS   9

int main( int argc, char *argv[] )
{
  FILE *template = NULL;
  FILE *data = NULL;

  char input[INPUT_LENGTH];
  char customerData[NUM_FIELDS][FIELD_LENGTH];
  int  element = 0;
  char *next;
  char ch;

  template = fopen( "template.txt", "r" );
  if ( template != NULL )
  {
    // read in the customers until we're done
    data = fopen( "data.txt", "r" );
    if ( data != NULL )
    {

      //next = strtok(data, "|");
      while(fgets(input, INPUT_LENGTH,data) != EOF){
        next = strtok(input, "|"); //splitting the data stored in input by |
        while(next != NULL){
          strcpy(customerData[element],next);
          //customerData[element++] = next;
          next =strtok(NULL, "|");
          //element++;
        }

        //testing
        for(element=0; element<INPUT_LENGTH; element++){
          printf("%s\n", customerData[element]);
        }
        //printf("%s\n", input );

      }



      fclose( data );
    }

    fclose( template );
  }

  return EXIT_SUCCESS;
}

这样做会给我一个错误array type 'char [30]' is not assignable和一堆垃圾输出。我需要帮助才能知道我在分裂部分做错了什么。

代码中还有其他东西,因为我的程序的主要目的是拆分这些文件,存储在数组中并使用每个位置作为我的模板: 单独的template.txt文件:

Welcome back, $1!
We hope that you and all the members
of the $0 family are constantly
reminding your neighbours there
on $5 to shop with us.
As usual, we will ship your order to
   $3 $1 $2. $0
   $4 $5
   $6, $7 $8

对于第一个标记,输出应该是:

Welcome back, Jane!
We hope that you and all the members
of the Public family are constantly
reminding your neighbors there
on Maple Street to shop with us.
As usual, we will ship your order to
    Ms. Jane Q. Public
    600 Maple Street
    Your Town, Iowa 12345

1 个答案:

答案 0 :(得分:0)

以下代码删除对template.txt文件的引用,因为它未在代码中使用。

为客户数据定​​义了适当的结构

从输入中提取每个字段并将其复制到客户数据数组

'test'代码输出客户数据阵列中的每个字段。

实施错误检查并采取适当的措施。

#include <stdio.h>
#include <string.h> // strtok()
#include <stdlib.h> // exit(), EXIT_FAILURE

#define INPUT_LENGTH   (128)
#define FIELD_LENGTH   (30)
#define NUM_FIELDS     (9)
#define MAX_CUSTOMERS  (20)

struct customer
{
    char lastName[ FIELD_LENGTH ];
    char firstName[ FIELD_LENGTH ];
    char initial[ FIELD_LENGTH ];
    char salutation[ FIELD_LENGTH ];
    char streetNumber[ FIELD_LENGTH ];
    char streetName[ FIELD_LENGTH ];
    char cityName[ FIELD_LENGTH ];
    char stateName[FIELD_LENGTH ];
    char zipCode[ FIELD_LENGTH ];
};

int main( void )
{
    struct customer customerData[ MAX_CUSTOMERS ];

    FILE *fp = NULL;

    char input[INPUT_LENGTH];

    int  element = 0;
    char *token = NULL;

    // read in the customers until we're done
    if( NULL == (fp = fopen( "data.txt", "r" ) ) )
    { // fopen failed
        perror( "fopen to read data.txt failed");
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    while( element < MAX_CUSTOMERS && fgets(input, INPUT_LENGTH, fp))
    {
        if( NULL != (token = strtok(input, "|") ) ) //splitting the data stored in input by |
            strcpy( customerData[element].lastName, token );
        else
        { // invalid file format
            fprintf( stderr, "line %d, contains invalid format\n", element);
            fclose( fp );
            exit( EXIT_FAILURE );
        }

        if( NULL != (token = strtok( NULL, "|") ) )
            strcpy( customerData[element].firstName, token );
        else
        { // invalid file format
            fprintf( stderr, "line %d, contains invalid format\n", element);
            fclose( fp );
            exit( EXIT_FAILURE );
        }

        if( NULL != (token = strtok( NULL, "|" ) ) )
            strcpy( customerData[element].initial, token );
        else
        { // invalid file format
            fprintf( stderr, "line %d, contains invalid format\n", element);
            fclose( fp );
            exit( EXIT_FAILURE );
        }

        if( NULL != (token = strtok( NULL, "|" ) ) )
            strcpy( customerData[element].salutation, token );
        else
        { // invalid file format
            fprintf( stderr, "line %d, contains invalid format\n", element);
            fclose( fp );
            exit( EXIT_FAILURE );
        }

        if( NULL != (token = strtok( NULL, "|" ) ) )
            strcpy( customerData[element].streetNumber, token );
        else
        { // invalid file format
            fprintf( stderr, "line %d, contains invalid format\n", element);
            fclose( fp );
            exit( EXIT_FAILURE );
        }

        if( NULL != (token = strtok( NULL, "|" ) ) )
            strcpy( customerData[element].streetName, token );
        else
        { // invalid file format
            fprintf( stderr, "line %d, contains invalid format\n", element);
            fclose( fp );
            exit( EXIT_FAILURE );
        }

        if( NULL != (token = strtok( NULL, "|" ) ) )
            strcpy( customerData[element].cityName, token );
        else
        { // invalid file format
            fprintf( stderr, "line %d, contains invalid format\n", element);
            fclose( fp );
            exit( EXIT_FAILURE );
        }

        if( NULL != (token = strtok( NULL, "|" ) ) )
            strcpy( customerData[element].stateName, token );
        else
        { // invalid file format
            fprintf( stderr, "line %d, contains invalid format\n", element);
            fclose( fp );
            exit( EXIT_FAILURE );
        }

        if( NULL != (token = strtok( NULL, "|" ) ) )
            strcpy( customerData[element].zipCode, token );
        else
        { // invalid file format
            fprintf( stderr, "line %d, contains invalid format\n", element);
            fclose( fp );
            exit( EXIT_FAILURE );
        }

        element++;
    } // end while

    //testing
#if 0
    // code with bug
    for(int i = 0; i < element; i++)
    {
        printf("lastName:    %s\n", customerData[element].lastName);
        printf("firstName:   %s\n", customerData[element].firstName);
        printf("initial:     %s\n", customerData[element].initial);
        printf("salutation:  %s\n", customerData[element].salutation);
        printf("streeNumber: %s\n", customerData[element].streetNumber);
        printf("streetName:  %s\n", customerData[element].streetName);
        printf("cityName:    %s\n", customerData[element].cityName);
        printf("stateName:   %s\n", customerData[element].stateName);
        printf("zipCode:     %s\n", customerData[element].zipCode);
    }
#else
    // corrected code
    for(int i = 0; i < element; i++)
    {
        printf("lastName:    %s\n", customerData[i].lastName);
        printf("firstName:   %s\n", customerData[i].firstName);
        printf("initial:     %s\n", customerData[i].initial);
        printf("salutation:  %s\n", customerData[i].salutation);
        printf("streeNumber: %s\n", customerData[i].streetNumber);
        printf("streetName:  %s\n", customerData[i].streetName);
        printf("cityName:    %s\n", customerData[i].cityName);
        printf("stateName:   %s\n", customerData[i].stateName);
        printf("zipCode:     %s\n", customerData[i].zipCode);
    }
#endif


    fclose( fp );

    return EXIT_SUCCESS;
} // end function: main