gpt4 book ai didi

c - 将管道分隔文件*读入结构数组 C

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

这个程序假设将管道分隔的文件读入结构数组。

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

#define LEN_LINE 160
#define LEN_NAME 40
#define MAX_LINES 60
#define LEAGUE_NAME 5
#define PARK_NAME 35
#define TEAM_ADDRESS 40
#define TEAM_CITY 30
#define TEAM_STATE 5
#define ZIP_CODE 10
#define PHONE_NUMBER 30
#define WEB_ADDRESS 25
#define LEN_TEAM 60

typedef struct
{
char leagueName[LEAGUE_NAME + 1];
char teamName[LEN_NAME + 1];
char parkName[PARK_NAME + 1];
char teamAddress[TEAM_ADDRESS + 1];
char teamCity[TEAM_CITY + 1];
char teamState[TEAM_STATE + 1];
char zipCode[ZIP_CODE + 1];
char phoneNumber[PHONE_NUMBER + 1];
char webAddress[WEB_ADDRESS + 1];


} team_t;

void displayTeams( team_t teams[], int count);

int main( void )
{
team_t teams[LEN_TEAM] = { 0 };
FILE *filePtr;
int index, count;
char line[LEN_LINE + 1] = {0};
char *startPtr, *endPtr;

filePtr = fopen( "MLBteams.txt", "r" );
if ( filePtr == NULL )
{
printf("Error in opening file\n");
}
else
{
index = 0;
while ( index < LEN_TEAM && fgets( line, sizeof( line ), filePtr ) )
{
startPtr = line;
endPtr = strchr(startPtr, '|');
strncpy( teams[index].leagueName, startPtr, endPtr - startPtr );

startPtr = endPtr + 1;
endPtr = strchr(startPtr, '|');
strncpy( teams[index].teamName, startPtr, endPtr - startPtr );

startPtr = endPtr + 1;
endPtr = strchr(startPtr, '|');
strncpy( teams[index].parkName, startPtr, endPtr - startPtr );

startPtr = endPtr + 1;
endPtr = strchr(startPtr, '|');
strncpy( teams[index].teamAddress, startPtr, endPtr - startPtr );

startPtr = endPtr + 1;
endPtr = strchr(startPtr, '|');
strncpy( teams[index].teamCity, startPtr, endPtr - startPtr );

startPtr = endPtr + 1;
endPtr = strchr(startPtr, '|');
strncpy( teams[index].teamState, startPtr, endPtr - startPtr );

startPtr = endPtr + 1;
endPtr = strchr(startPtr, '|');
strncpy( teams[index].zipCode, startPtr, endPtr - startPtr );

startPtr = endPtr + 1;
endPtr = strchr(startPtr, '|');
strncpy( teams[index].phoneNumber, startPtr, endPtr - startPtr );

startPtr = endPtr + 1;
endPtr = strchr(startPtr, '|');
strncpy( teams[index].webAddress, startPtr, endPtr - startPtr );



index++;
}

fclose( filePtr );
count = index;

displayTeams(teams, count);
}

return 0;
}

void displayTeams( team_t teams[], int count)
{
int index;

for ( index = 0; index <= count - 1; index = index + 1)
{
printf("LEAGUE: %s\nTEAM: %s\nPARKNAME: %s\nADDRESS: %s\nCITY: %s\nSTATE: %s\nZIPCODE: %i\nPHONE#: %s\nWEBADDRESS: %s\n\n",
teams[index].leagueName, teams[index].teamName, teams[index].parkName,
teams[index].teamAddress, teams[index].teamCity, teams[index].teamState,
teams[index].zipCode, teams[index].phoneNumber, teams[index].webAddress);
}
}

我正在使用它来尝试读取此文件。

A|Baltimore Orioles|Oriole Park|333 West Camden Street|Baltimore|MD|21201|(410) 685-9800|orioles.com
A|Boston Red Sox|Fenway Park|4 Yawkey Way|Boston|MA|02215|(617) 267-9440|redsox.com
N|St. Louis Cardinals|Busch Stadium|700 Clark Street|St. Louis|MO|63102|(314) 345-9600|cardinals.com
N|Washington Nationals|Nationals Park|1500 South Capitol Street, SE|Washington|DC|20003-1507|(202) 675-6287|nationals.com

这是几行(总共 30 个团队)。好吧,当我运行该程序时,它立即崩溃并且没有给我任何错误消息。如果有人能向我解释为什么代码不起作用,我将不胜感激。我的猜测是我错误地使用了 strncpy 函数。

最佳答案

在给定的代码中,保留原始字符串似乎并不重要。我建议修改它以使用 strtok(),它通过在其中插入 NUL 来修改原始字符串。具体来说,它将 NUL 字符放在第一个定界符所在的位置,这样您就可以使用 strcpy 复制 token 。然后带 NULL 的 strtok 将返回到原始字符串并为您找到下一个标记。在网上阅读所有关于 strtok() 的信息。

while (index < LEN && fgets(line, sizeof(line), filePtr))
{
char* token = strtok(line, "|");
strcpy(teams[index].leagueName, token);
token = strtok(NULL, "|");
strcpy(teams[index].parkName, token);
// and so forth
}

现在我还没有解决而您应该解决的问题是提供的文本对于该字段来说可能太长,您的原始代码和我建议的代码都没有解决这个问题。也许

token = strtok(line, "|");
if (strlen(token) > LEAGUE_NAME) token[LEAGUE_NAME]=0;
strcpy(teams[index].leagueName, token);

还有你的 strchr() 结果,在我上面的原型(prototype)代码中,strtok() 结果,没有被检查。如果输入行比预期的要宽,您的 limited by count fgets() 调用可能会截断该行并省略分隔符。 strchr 或 strtok 结果将为 NULL,这将停止您的程序。

关于c - 将管道分隔文件*读入结构数组 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45513944/

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