我有下面的结构
struct JugadorStruct_t {
int fichas, manos_ganadas, manos_perdidas;
char* nombre;
int* fichas_partidas;
};我想用txt文件中的一些值初始化这个结构,所以我执行以下操作:
int initJugador(JugadorPtr_t jugador) {
FILE* fp =
fopen("/home/norhther/CLionProjects/blackjack/jugador.txt","r");
if (fp == NULL) {
printf("El archivo del jugador no existe\n");
fclose(fp);
return 1;
}
else {
char* line = NULL;
size_t len = 0;
getline(&line, &len, fp);
jugador->nombre = strdup(line);
getline(&line, &len, fp);
jugador->fichas = atoi(line);
getline(&line, &len, fp);
jugador->manos_ganadas = atoi(line);
getline(&line, &len, fp);
jugador->manos_perdidas = atoi(line);
while (getline(&line, &len, fp) != -1) {
printf("%s", line);
}
}
fclose(fp);
return 0;
}(前提是文件至少有4行)
我不知道文件中还剩下多少行,但在剩余的每一行中都有一个数字。我希望分配内存并将每个值添加到fichas_partidas。
有没有一个很好的方法来实现这一点?
发布于 2018-08-17 09:03:20
下面是代码的改编版本,它可以满足您的需求。注意,文件名是在main()程序中指定的(没有绝对路径),并传递给初始化函数。还要注意Is it a good idea to typedef pointers?中的讨论-简短的答案是“不”。
该代码在结构中添加了两个字段,以记录可以存储在fichas_perdidas数组中的最大条目数和实际存储的条目数。分配代码利用了main()中的零初始化,以及当传递空指针时,realloc()的行为类似于malloc()。分配代码将错误报告给标准错误;名称stderr表示这是用于错误消息。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct JugadorStruct_t
{
int fichas, manos_ganadas, manos_perdidas;
char *nombre;
int *fichas_partidas;
int max_fichas;
int num_fichas;
};
typedef struct JugadorStruct_t *JugadorPtr_t; // Don't do this!
static
int initJugador(JugadorPtr_t jugador, const char *file)
{
FILE *fp = fopen(file, "r");
if (fp == NULL)
{
printf("El archivo del jugador no existe\n");
fclose(fp);
return 1;
}
char *line = NULL;
size_t len = 0;
getline(&line, &len, fp);
jugador->nombre = strdup(line);
getline(&line, &len, fp);
jugador->fichas = atoi(line);
getline(&line, &len, fp);
jugador->manos_ganadas = atoi(line);
getline(&line, &len, fp);
jugador->manos_perdidas = atoi(line);
while (getline(&line, &len, fp) != -1)
{
int x = strtol(line, 0, 0); /* Extremely sloppy */
if (jugador->num_fichas >= jugador->max_fichas)
{
size_t new_number = 2 * jugador->max_fichas + 2;
size_t new_buflen = new_number * sizeof(*jugador);
void *new_buffer = realloc(jugador->fichas_partidas, new_buflen);
if (new_buffer == NULL)
{
fprintf(stderr, "Out of memory (requesting %zu bytes)\n",
new_buflen);
free(jugador->nombre);
free(jugador->fichas_partidas);
fclose(fp);
return -1;
}
jugador->fichas_partidas = new_buffer;
jugador->max_fichas = new_number;
}
jugador->fichas_partidas[jugador->num_fichas++] = x;
}
fclose(fp);
return 0;
}
static void printJugador(const char *tag, struct JugadorStruct_t *jp)
{
printf("%s (%p):\n", tag, (void *)jp);
printf("Nombre: [%s]\n", jp->nombre);
printf("Fichas: %d\n", jp->fichas);
printf("Manos Ganadas: %d\n", jp->manos_ganadas);
printf("Manos Perdidas: %d\n", jp->manos_perdidas);
printf("Num Fichas: %d\n", jp->num_fichas);
printf("Max Fichas: %d\n", jp->max_fichas);
for (int i = 0; i < jp->num_fichas; i++)
printf("%2d: %d\n", i + 1, jp->fichas_partidas[i]);
}
int main(void)
{
struct JugadorStruct_t j = { 0 };
initJugador(&j, "jugador.txt");
printJugador("After reading", &j);
return 0;
}示例数据文件:
Line for nombre
32
27
19
12345
23456
34567
45678
56789
67890
99999999程序输出:
After reading (0x7ffee84ee400):
Nombre: [Line for nombre
]
Fichas: 32
Manos Ganadas: 27
Manos Perdidas: 19
Num Fichas: 7
Max Fichas: 14
1: 12345
2: 23456
3: 34567
4: 45678
5: 56789
6: 67890
7: 99999999https://stackoverflow.com/questions/51812437
复制相似问题