gpt4 book ai didi

c - 努力按整数搜索文件中的结构数组

转载 作者:行者123 更新时间:2023-11-30 20:29:14 24 4
gpt4 key购买 nike

我正在执行一项主要任务,并且我真的很难从文件中的数组中提取结构。我不知道我的代码从这里到哪里去,因为我没有太多使用 C 的经验。

我正在尝试搜索文件寻找“Robot_Number”(最左边的整数),然后打印所有特定的机器人信息。我的代码编译并运行,但不会从 while 循环中打印任何内容 (Ln57-66)。 .txt 文件也没有明显的问题。

我的 .txt 文件的布局如下:

1 Bender 2054 30.500000 80 60 0.000000
2 Crusher 1979 15.000000 86 80 0.000000
3 Sprinter 2042 45.000000 41 30 0.000000
4 Vulc4n 2178 47.000000 90 50 0.000000

我的代码:

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

#define RobMax 10

typedef struct {
int Robot_Number;
char Robot_Name[20];
int Year_Manufactured;
float Top_Speed;
int Strength;
int Mass;
float Best_Score;
}robot_t;

int main(){
int targetNumber;
int RobCounter = 4;

//create an array of struct,
robot_t robot_info[RobCounter] =
{{1, "Bender", 2054, 30.5, 80, 60, 0},
{2, "Crusher", 1979, 15, 86, 80, 0},
{3, "Sprinter", 2042, 45, 41, 30, 0},
{4, "Vulc4n", 2178, 47, 90, 50, 0}};


//opening a file for reading and writing
FILE* file =fopen("Robot.txt", "r+w");
if(file ==NULL){
printf("ERROR: file failed to open!\n");
exit(-1);
}

/*//Initialising the .txt file, first time only
for (i=0; i<4; i++){
fprintf(file, "%d %s %d %f %d %d %f\n",
robot_info[i].Robot_Number,
robot_info[i].Robot_Name,
robot_info[i].Year_Manufactured,
robot_info[i].Top_Speed,
robot_info[i].Strength,
robot_info[i].Mass,
robot_info[i].Best_Score);

}*/

printf("you selected search a robot by number. (Type 0 to view all avalible robots)\n");
scanf("%d", &targetNumber);

int RNo, YM, S, M;
char RNa[20];
float TS, BS;

if (targetNumber != 0){
printf("Robot ID: %d's information:\n", targetNumber);
while( fscanf(file, "%d%19s%d%f%d%d%f", &RNo, RNa, &YM, &TS, &S, &M, &BS) ==2) {
robot_info[targetNumber].Robot_Number = RNo;
strcpy(robot_info[targetNumber].Robot_Name, RNa);
robot_info[targetNumber].Year_Manufactured = YM;
robot_info[targetNumber].Top_Speed = TS;
robot_info[targetNumber].Strength = S;
robot_info[targetNumber].Mass = M;
robot_info[targetNumber].Best_Score = BS;
printf("Num: %d, Name: %19s, Manufactured: %d, Top Speed: %f, Strength: %d, Mass: %d, Best Score: %f.", RNo, RNa, YM, TS, S, M, BS);
}
}
fclose(file);

return 0;
}

最佳答案

一些需要考虑的注意事项,如评论部分所述:

  • 当您打算从文件中读取包含机器人信息的结构体数组时,为什么要使用它们?
  • 当您实际上只是读取文件时,为什么要使用 r+w 打开文件?



因此,假设您没有有关机器人的这些信息,而您只想读取该文件。如果这是最初的意图,这里有一个片段,可能会对您有所帮助。

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

#define RobMax 10

typedef struct {
int Robot_Number;
char Robot_Name[20];
int Year_Manufactured;
float Top_Speed;
int Strength;
int Mass;
float Best_Score;
}robot_t;

int scan_file(const char* fname, robot_t* robot_info, const int N)
{
int counter = 0;

// opening a file for reading
// Note the change in file open mode as you
// do not need to write it
FILE* file = fopen(fname, "r");
if(file == NULL)
{
printf("ERROR: file failed to open!\n");
return 0;
}

int RNo, YM, S, M;
char RNa[20];
float TS, BS;


// Note the change in scan format - you need to include the correct delimiters
// in your case the white spaces. Also changed 2 -> 7 as you'd expect 7 values
// to be read and not 2
int scanres;
while((scanres = fscanf(file, "%d %19s %d %f %d %d %f", &RNo, RNa, &YM, &TS, &S, &M, &BS)) == 7 && scanres != EOF)
{
// if we reached maximum capacity
if (counter >= N)
break;

robot_info[counter].Robot_Number = RNo;
strcpy(robot_info[counter].Robot_Name, RNa);
robot_info[counter].Year_Manufactured = YM;
robot_info[counter].Top_Speed = TS;
robot_info[counter].Strength = S;
robot_info[counter].Mass = M;
robot_info[counter].Best_Score = BS;
printf("SCANNED: Num: %d, Name: %19s, Manufactured: %d, Top Speed: %f, Strength: %d, Mass: %d, Best Score: %f.\n", RNo, RNa, YM, TS, S, M, BS);

++counter;
}

fclose(file);

return counter;
}

// gets the robot's index in the robots array by ID
int get_roboti_by_id(const int ID, const robot_t* robots, const int N)
{
for (int i = 0; i < N; ++i)
{
if (robots[i].Robot_Number == ID)
{
return i;
}
}

return -1;
}

void print_info(const robot_t* info)
{
printf("Num: %d, Name: %19s, Manufactured: %d, Top Speed: %f, Strength: %d, Mass: %d, Best Score: %f.",
info->Robot_Number, info->Robot_Name, info->Year_Manufactured, info->Top_Speed, info->Strength, info->Mass, info->Best_Score);
}

int main()
{
int targetNumber;
int RobCounter;

// create an array of struct,
robot_t robot_info[RobMax];


printf("you selected search a robot by number. (Type 0 to view all avalible robots)\n");
scanf("%d", &targetNumber);
// scans the whole file populated with robots
RobCounter = scan_file("robots.txt", robot_info, RobMax);


if (targetNumber != 0)
{
int robot_index = get_roboti_by_id(targetNumber, robot_info, RobCounter);
if (robot_index >= 0)
{
print_info(&robot_info[robot_index]);
printf("\n");
}
else
{
printf("There is no such robot with index: %d\n", targetNumber);
}
}
else
{
for (int i = 0; i < RobCounter; ++i)
{
print_info(&robot_info[i]);
printf("\n");
}
}

return 0;
}



如果您发现任何错误或者这不是您所需要的,请随时纠正我。如有任何建议,我们将不胜感激!

关于c - 努力按整数搜索文件中的结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58677898/

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