gpt4 book ai didi

c - 我有一个程序无法在 Ubuntu 上运行,但可以在 Windows 上运行

转载 作者:行者123 更新时间:2023-11-30 16:09:42 25 4
gpt4 key购买 nike

我有一个在 Windows 10 中编码的程序,它运行得很好。在这种情况下(我将过度简化,因为这是程序失败的地方),它需要一个包含 200 个元素的简单链表(结构体),并将前 100 个元素的数据复制到一个包含 100 个元素的数组中(数组[100])。该程序在 Windows 上完美运行,但在 Ubuntu 上它不会复制单个结构。两个系统的 GCC 编译器之间是否有任何差异会导致此问题?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100

typedef struct{
int id;
char title[100];
char director[100];
char genre[100];
int likes;
int number_of_voters;
float average_rating;
int year;
int cost;
char color[100];
float ratingW;
}Movie;

struct Node{
Movie movie;
struct Node *next;
};

//Simply linked list
typedef struct{
struct Node *head;
}List;

//Array
typedef struct{
Movie movies[SIZE];
int num; //number of elements
}Array;

void Initialize(List *l);
void PrintList(List l);
void InsertNode(List *l, Movie reg);
void PrintMovie(Movie mov);
void PrintArray(Array arr);
void FromListToArray(List l, Array *arr);

int main(){
List sls;
Array a;
Initialize(&sls);
PrintList(sls); //Prints 200 movies, and shows the message "Movies loaded in the list ::: 200"
FromListToArray(sls, &a);
PrintArray(a); //Doesn't print any movie, and shows the message "Movies loaded in the array ::: 0"
return 0;
}

//Initializes the list
void Initialize(List *l){
l->head = NULL;
}

void PrintList(List l){
struct Node *p;
p = l.head;
while(p != NULL)
{
PrintMovie(p->movie);
p = p->next;
}
}

//Inserts a node at the beginning of the list
void InsertNode(List *l, Movie reg){
struct Node *r;
r = (struct Node *) malloc (sizeof(struct Node));;
if (r == NULL){
printf("No memory!\n");
}
else{
r->movie.id = reg.id;
strcpy(r->movie.title, reg.title);
strcpy(r->movie.director, reg.director);
strcpy(r->movie.genre, reg.genre);
r->movie.likes = reg.likes;
r->movie.number_of_voters = reg.number_of_voters;
r->movie.average_rating = reg.average_rating;
r->movie.year = reg.year;
r->movie.cost = reg.cost;
strcpy(r->movie.color, reg.color);
r->movie.ratingW = reg.ratingW;
r->next = l->head;
l->head = r;
}
}

/*Copies the data from a txt file into a simply linked list.
Line 1 is the id of the first movie, line 2 the title... line 10 is the color.
Line 11 is the id of the second movie, and so on... This repeated 200 times (200 movies = 2000 lines)*/
void FromTxtToList(List *l, FILE *f){
int j = 0;
char cad[100];
Movie reg;
f = fopen("movies.txt", "r");
if (f == NULL){
printf("Error, could not open the file\n");
}
else{
while(!feof(f)){
fgets(cad, 100, f);
reg.id = atoi(cad);
fgets(cad, 100, f);
strcpy(reg.title, cad);
fgets(cad, 100, f);
strcpy(reg.director, cad);
fgets(cad, 100, f);
strcpy(reg.genre, cad);
fgets(cad, 100, f);
reg.likes = atoi(cad);
fgets(cad, 100, f);
reg.number_of_voters = atoi(cad);
fgets(cad, 100, f);
reg.average_rating = atof(cad);
fgets(cad, 100, f);
reg.year = atoi(cad);
fgets(cad, 100, f);
reg.cost = atoi(cad);
fgets(cad, 100, f);
strcpy(reg.color, cad);
reg.ratingW = 0;
InsertNode(l, reg);
j++;
}
}
fclose(f);
printf("Movies loaded in the list ::: %d\n", j);
}

void PrintMovie(Movie mov){
printf("///////////////////////////////////\n");
printf("Id: %d\n", mov.id);
printf("Title: %s", mov.title);
printf("Director: %s", mov.director);
printf("Genre: %s", mov.genre);
printf("Likes: %d\n", mov.likes);
printf("Number of voters: %d\n", mov.number_of_voters);
printf("Average rating: %.2f\n", mov.average_rating);
printf("Year: %d\n", mov.year);
printf("Cost: $%d\n", mov.cost);
printf("Color or BW: %s", mov.color);
printf("Rating: %.2f\n", mov.ratingW);
printf("///////////////////////////////////\n");
}

void PrintArray(Array arr){
int i;
printf("\nArray: \n");
for(i=0; i < arr.num; i++){
PrintMovie(arr.movies[i])
}
}

void FromListToArray(List l, Array *arr){
int i = 0;
arr->num = 0;
struct Node *p;
p = l.head;
while ((p != NULL) && (arr->num < SIZE)){
if (strcmp(p->movie.color, "Color\n")==0){
//If I find a "colored" movie (color = "Color")
//Copy the data into the array
arr->movies[i].id = p->movie.id;
strcpy(arr->movies[i].title, p->movie.title);
strcpy(arr->movies[i].director, p->movie.director);
strcpy(arr->movies[i].genre, p->movie.genre);
arr->movies[i].likes = p->movie.likes;
arr->movies[i].number_of_voters = p->movie.number_of_voters;
arr->movies[i].average_rating = p->movie.average_rating;
arr->movies[i].year = p->movie.year;
arr->movies[i].cost = p->movie.cost;
strcpy(arr->movies[i].color, p->movie.color);
arr->movies[i].ratingW = p->movie.ratingW;
arr->num++;
i++;
}
p = p->next;
}
printf("Movies loaded in the array ::: %d\n", arr->num);
}

最佳答案

首先,在函数 PrintArray

有一个错误的“;”在 PrintMovie(arr.movi​​es[i]) 行中。

第二,函数 FromTxtToList 中的 while(!feof(f)) 存在问题。

显然,只有在读取文件末尾 (EOF) 后,feof() 才为 TRUE,而不是在到达 EOF 时。请参阅herehere .

可能的修复方法是 if(fgets(cad, 100, f)==NULL) break;

而不是fgets(cad, 100, f);

在函数FromTxtToList中。

此外movies.txt 文件应该仅以一个空行结尾(否则最后一部电影将得到“Color”而不是“Color\n”)。

关于c - 我有一个程序无法在 Ubuntu 上运行,但可以在 Windows 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59058639/

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