gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:32:03 25 4
gpt4 key购买 nike

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

这是我的名为 data.txt 的 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

最佳答案

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

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

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

“测试”代码输出客户数据数组中的每个字段。

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

#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

关于c - 如何拆分文本文件并将它们存储在数组中以用于 C 中的模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35044944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com