gpt4 book ai didi

c - 从多个文件读取到一个结构

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

我在分配时遇到问题,它涉及从文件读取到结构中,我对如何执行感到困惑这里是我必须使用的带有参数的函数。

// This function will read in size  struct players from filename and add these
// the players array. The function will use index to know where to start
// writing the players to in the array.
// Parameters
//
// filename – The name of the input file
// players – a pointer to the array of player structures
// index – The index of the array to start placing players into
// size – The number of players in the input file
// Return - Nothing

void read_from_file(char* filename, Player* players, int index, int size);

这是我必须用来从 3 个看起来像这样的不同文件中读取数据的函数:

Andrew Jackson 129 33 38 30 506
Jeremy Warden 25 24 3 9 493
Jared Welch 130 1 43 27 422
Brandon Splitter 138 38 40 7 587
Joe Gwilliams 150 23 30 25 498
Ali Mohammed 119 43 13 6 598
Dheeraj Johnson 124 79 59 36 506
Bill Clinton 121 65 12 26 449
Jesse James 87 58 8 5 464
John Doe 129 100 0 12 548

我必须读入 3 个文件,每个文件都有 10 个玩家,总共 30 个我需要读入结构。我知道我还没有走得很远,但是我对做什么以及如何处理这个问题感到非常困惑,非常感谢任何帮助!下面我记下了我已经做过的事情。请帮忙!!谢谢

//Brady Webb
//lab D
//HW1

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

typedef struct player
{
char Fname[25];
char Lname[25];
int Singles;
int Doubles;
int Triples;
int Homeruns;
int At_Bats;
float Slugging_Percentage;
} Player;

void read_from_file(char* filename, Player* players, int index, int size);

int main(int argc, char* argv[])
{
int size= atoi(*(argv+1));
char* file1 = *(argv+2);
char* file2 = *(argv+3);
char* file3 = *(argv+4);
if (argc<6 || argc>6)
{
printf("Incorrect command line arguments\n");
return 0;
}

return 0;
}
void read_from_file(char*filename, Player* players, int index, int size)
{
FILE *ptr;
int i=0;

if ((ptr=fopen(filename, "r")) == NULL)
{
return 0;
}

while (ptr != EOF)
{

}

}

最佳答案

读取具有规则结构的文件的最简单方法是使用具有复杂格式字符串的fscanf

fscanf("%s %s %d %d %d %d %d", Player.Fname, Player.Lname, 
&Player.Singles, &Player.Doubles,
&Player.Triples, &Player.Homeruns, &Player.At_Bats);

你应该做一个循环来读取文件的和,你可以添加以正确格式读取数据的检查,例如:

int check = fscanf("%s %s %d %d %d %d %d", Player.Fname, Player.Lname, 
&Player.Singles, &Player.Doubles,
&Player.Triples, &Player.Homeruns, &Player.At_Bats);
if( check != 7 )
{
// stop reading and report on wrong file format
}

更新:

我建议将以下代码作为可能的解决方案:

// This function will read in size  struct players from filename and add these
// the players array. The function will use index to know where to start
// writing the players to in the array.
// Parameters
//
// filename – The name of the input file
// players – a pointer to the array of player structures
// index – The index of the array to start placing players into
// size – The number of players in the input file
// Return - number of read players (positive number)
// or error code (negarive number)
int read_from_file(char * filename, Player* players, int index, int size)
{
struct player ptmp;
FILE *fptr;
// open the file
if ((fptr = fopen(filename, "r")) == NULL)
{
fprintf(stderr, "File %s cannot be oppened\n",filename);
return -1; // error code for "File cannot be oppened"
}
// reading from file
int position = index;
int cnt = 0;
while (!ferror(fptr) && cnt < size)
{
int check = fscanf(fptr, "%24s %24s %d %d %d %d %d", ptmp.Fname, ptmp.Lname,
&ptmp.Singles, &ptmp.Doubles, &ptmp.Triples, &ptmp.Homeruns, &ptmp.At_Bats);
if (feof(fptr) && check != 7)
{
break;
}
if (check != 7)
{
fclose(fptr);
fprintf(stderr,"Wrong data format in line %d of file %s\n", cnt+1, filename);
return -2; // error code for "File has wrong data format"
}
// copy data to players
players[index++] = ptmp;
cnt++;
}
// close the file
fclose(fptr);
return cnt;
}

注意改变类型的函数 read_from_file - 我在评论中描述了我关于返回值的想法。

main在我的理解中应该是这样的:

int main(int argc, char* argv[])
{
Player players[30]; // memory is allocated for particular number of data items
// check the command line arguments
if (argc < 3)
{
printf("Please run the program in the format:\n");
printf(" %s 2 firstFile.txt secondFile.txt\n", argv[0]);
printf(" where 2 is number of files given after 2 with data to be read\n");
return 0;
}
int fileNumber = 0;
if (!sscanf(argv[1], "%d", &fileNumber) || fileNumber <= 0)
{
printf("The first command line argument nust be positive number.\n");
printf("Run program without parameters to see details\n");
return 0;
}
if (fileNumber != (argc - 2))
{
printf("Command line arguments are inconsistent\n");
printf("Run program without parameters to see details\n");
return 0;
}
// file processing
int i = 0;
int total = 0;
int max = 30;
for (i = 0; i < fileNumber; i++)
{
printf("Reading from %s...\n", argv[i + 2]);
int res = read_from_file(argv[i + 2], players, total, max);
if (res > 0)
{
total += res;
max -= res;
}
}
// check data
for (i = 0; i < total; i++)
{
printf("%s %s : %d %d %d %d %d\n", players[i].Fname, players[i].Lname, players[i].Singles, players[i].Doubles, players[i].Triples, players[i].Homeruns, players[i].At_Bats);
}
return 0;
}

假设30个播放器可以读取任意数量的文件,不一定要每个文件10个。

关于c - 从多个文件读取到一个结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42129878/

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