C 我有一个不';在Ubuntu上不起作用,但在Windows上起作用
我有一个用Windows10编写的程序,它运行得非常好。 在这种情况下(我将过度简化,因为这是程序失败的地方),它采用一个包含200个元素的简单链表(struct),并将前100个元素的数据复制到一个包含100个元素的数组中(array[100])。 这个程序在Windows上运行得很好,但它不会在Ubuntu上复制一个结构。 两个系统的GCC编译器之间是否存在可能导致这种情况的差异C 我有一个不';在Ubuntu上不起作用,但在Windows上起作用,c,arrays,windows,list,compiler-errors,C,Arrays,Windows,List,Compiler Errors,我有一个用Windows10编写的程序,它运行得非常好。 在这种情况下(我将过度简化,因为这是程序失败的地方),它采用一个包含200个元素的简单链表(struct),并将前100个元素的数据复制到一个包含100个元素的数组中(array[100])。 这个程序在Windows上运行得很好,但它不会在Ubuntu上复制一个结构。 两个系统的GCC编译器之间是否存在可能导致这种情况的差异 #include <stdio.h> #include <stdlib.h> #incl
#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);
}
#包括
#包括
#包括
#定义大小100
类型定义结构{
int-id;
字符标题[100];
char-director[100];
字符类型[100];
int喜欢;
投票人的整数;
浮动平均值;
国际年;
国际成本;
炭色[100];
浮动比率w;
}电影;
结构节点{
电影;
结构节点*下一步;
};
//简单链表
类型定义结构{
结构节点*头部;
}名单;
//排列
类型定义结构{
电影[大小];
int num;//元素数
}阵列;
无效初始化(列表*l);
作废打印列表(列表l);
void InsertNode(列表*l,电影注册);
无效印刷电影(电影电影电影);
无效打印数组(数组arr);
从列表到数组的void(列表l,数组*arr);
int main(){
列出SL;
阵列a;
初始化(&sls);
PrintList(sls);//打印200部电影,并显示消息“列表中加载的电影:::200”
从列表到阵列(sls和a);
PrintArray(a);//不打印任何电影,并显示消息“在数组中加载的电影:::0”
返回0;
}
//初始化列表
无效初始化(列表*l){
l->head=NULL;
}
作废打印列表(列表l){
结构节点*p;
p=l.水头;
while(p!=NULL)
{
打印电影(p->movie);
p=p->next;
}
}
//在列表的开头插入一个节点
void InsertNode(列表*l,电影注册){
结构节点*r;
r=(结构节点*)malloc(sizeof(结构节点));;
if(r==NULL){
printf(“无内存!\n”);
}
否则{
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_vorters=reg.number_of_vorters;
r->movie.average_rating=reg.average_rating;
r->movie.year=注册年份;
r->movie.cost=注册成本;
strcpy(r->movie.color,reg.color);
r->movie.ratingW=reg.ratingW;
r->next=l->head;
l->head=r;
}
}
/*将数据从txt文件复制到简单链接列表中。
第1行是第一部电影的id,第2行是标题。。。第10行是颜色。
第11行是第二部电影的id,依此类推。。。重复200次(200部电影=2000行)*/
void fromTXTOList(列表*l,文件*f){
int j=0;
char-cad[100];
电影注册;
f=fopen(“movies.txt”,“r”);
如果(f==NULL){
printf(“错误,无法打开文件\n”);
}
否则{
而(!feof(f)){
fgets(cad,100,f);
注册id=atoi(cad);
fgets(cad,100,f);
strcpy(注册名称,cad);
fgets(cad,100,f);
strcpy(注册主任,cad);
fgets(cad,100,f);
strcpy(注册类型,cad);
fgets(cad,100,f);
reg.likes=atoi(cad);
fgets(cad,100,f);
登记选民人数=atoi(cad);
fgets(cad,100,f);
注册平均评级=atof(cad);
fgets(cad,100,f);
注册年份=atoi(cad);
fgets(cad,100,f);
注册成本=atoi(cad);
fgets(cad,100,f);
strcpy(注册颜色,cad);
reg.ratingW=0;
插入节点(l,reg);
j++;
}
}
fclose(f);
printf(“列表中加载的电影::%d\n”,j);
}
作废打印电影(电影电影短片){
printf(“//////n”);
printf(“Id:%d\n”,mov.Id);
printf(“标题:%s”,运动标题);
printf(“董事:%s”,移动董事);
printf(“流派:%s”,电影流派);
printf(“Likes:%d\n”,mov.Likes);
printf(“投票人数:%d\n”,移动投票人数);
printf(“平均评级:%.2f\n”,移动平均评级);
printf(“年份:%d\n”,移动年份);
printf(“成本:$%d\n”,移动成本);
printf(“颜色或宽度:%s”,运动颜色);
printf(“额定值:%.2f\n”,移动额定值w);
printf(“//////n”);
}
无效打印数组(数组arr){
int i;
printf(“\n数组:\n”);
对于(i=0;inum=0;
结构节点*p;
p=l.水头;
而((p!=NULL)&&(arr->nummovie.color,“color\n”)==0{
//如果我找到一部“彩色”电影(color=“color”)
//将数据复制到数组中
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].投票人数量=p->movie.number\u投票人数量;
arr->movies[i].平均评分=p->movie.average评分;
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(“加载到数组中的电影::%d\n”,arr->num);
}
首先,在函数打印数组中
在printmoine(arr.movies[i])
行中有一个miising“;”
第二个,TxtToList中的函数中(!feof(f))
存在问题
显然,feof()只有在读取文件结尾(EOF)后才是真的,而不是在达到EOF时。见和
如果(fgets(cad,100,f)==NULL)中断,可能的修复方法是代码>
而不是fgets(cad,100,f)代码>
在txttolist的函数中
此外movies.txt文件应仅以一行空行结尾(否则,您将在最后一部电影中获得“Color”而不是“Color\n”)可能,但极有可能您有