简体   繁体   English

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

[英]How do I split a text file and store them in arrays to use for a template in C?

I'm a newbie at C and I'm having problems splitting a particular text file and storing the tokens in an array. 我是C的新手,在拆分特定文本文件并将令牌存储在数组中时遇到问题。

This is my txt file called data.txt- to be splitted by "|": 这是我的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

My code is : 我的代码是:

#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;
}

doing it like this give me an error array type 'char [30]' is not assignable and a bunch of garbage output. 这样做会给我一个错误array type 'char [30]' is not assignable并且会产生大量垃圾输出。 I need help in knowing what I'm doing wrong in the splitting part. 我需要帮助才能知道我在拆分部分中做错了什么。

There are other stuff in the code because the main purpose of my program is to split these files, store the in an array and use each position for my template: separate template.txt file: 代码中还有其他内容,因为我的程序的主要目的是拆分这些文件,将它们存储在数组中,然后将模板的每个位置使用:单独的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

for the first token, the output should be like: 对于第一个令牌,输出应类似于:

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

the following code drops reference to the template.txt file as it is not used in the code. 以下代码删除了对template.txt文件的引用,因为该代码中未使用它。

An appropriate struct is defined for the customer data 为客户数据定​​义了适当的结构

each field is extracted from the input and copied into the array of customer data 从输入中提取每个字段并将其复制到客户数据数组中

the 'test' code outputs each field from the customer data array. “测试”代码输出客户数据数组中的每个字段。

error checks are implemented and appropriate action taken. 实施错误检查并采取适当的措施。

#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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C-如何在文本文件中分割一行并存储其值? - C - How do I split a line in a text file and store their values? 如何从文本文件中读取整数和字符并将它们存储在单独的数组中? - How do I read integers and characters from a text file and store them in separate arrays? 如何从文本文件中读取值并将其存储在2个不同的数组中? - How do you read values from a text file and store them in 2 different arrays? 如何读取文本文件的某些部分并将它们存储在要比较的字符 arrays 中? - How can i read certain parts of of a text file and store them in character arrays to be compared? 如何使用分隔符分割我从文件中读取的行并将它们存储在 C 的每个 int 变量中 - How to split line I read from file with delimiters and store them in each int variable in C 如何将文本文件存储到 C 中的数组中 - How do I store a text file into an array in C 如何从文件中读取数字并将其分配给多个数组? - C - How do I read numbers from a file and assign them to multiple arrays? - C 如何将文件中的数据存储到不同的数组中? C语言 - How do I store data from a file into different arrays? C language 如何将数据存储在 char 数组中并在 C 中转换为 int - How do I store data in char arrays and convert to int in C 如何在C中将标记化字符串存储到数组中 - How do I store tokenized strings into arrays in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM