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

标签 c arrays windows list compiler-errors

我有一个在 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/

相关文章:

c - 在自定义宏中使用 offsetof 宏

c - 堆栈指针应该指向顶部的值,还是指向下一个值的位置?

c - iup, IupParamBox 问题

ruby - 在 Ruby 中,如何确定字符串是否不在数组中?

javascript - 如何直接追加Json对象?

windows - .rdata 和 .idata 段有什么区别?

c - mmap 相关的段错误

arrays - 如何在Powershell中使用2D阵列?

sql - 登录失败。用户 'IIS APPPOOL\ASP.NET v4.0' 登录失败

C++ 函数调用路由解析器