【发布时间】:2019-12-26 14:21:19
【问题描述】:
int main(int argc, char *argv[])
{
MYAPP MyApp;
MyApp = (struct MyApp*)malloc(sizeof(struct MyApp));
MyApp->maxMovieYear = 0;
MyApp->maxMovieNum = 0;
MyApp->myYearTree = CreateTreeYear();
MyApp->myTree = CreateTree();
MyApp = read_data(argv,MyApp);
int exit=FALSE;
int command;
char title[256];
int year;
int imdbID;
char posterlink[512];
printf(">>>Welcome to Movie Analysis at IMDB<<< \n");
while (!exit)
{
printf("\n-Menu-\n\
1. Display the full index of movies\n\
2. Display the movies where their title contains a specific keyword\n\
3. Display the year with maximum number of movies\n\
4. Exit\n\
Option:");
if(MyApp == NULL){
exit = TRUE;
printf("\nCannot locate the file!");
break;
}
YearWithMostMovies(MyApp->myYearTree ,MyApp);
scanf("%d", &command);
fflush(stdin);
char searchSong[512];
switch (command)
{
case 1:
printf("Movie Index\n-------------\n");
display_index(MyApp->myTree);
break;
case 2:
printf("Please enter the movie you want to see: ");
scanf("%s",searchSong);
display_movies_keyword(MyApp->myTree,searchSong);
break;
case 3:
most_popular_year_movies(MyApp);
break;
case 4:
exit = TRUE;
break;
default:
printf("command not recognized\n");
break;
}
}
printf("\n");
return 0;
}
MYAPP read_data(char *argv[],MYAPP MyApp){
FILE *outfile = fopen(argv[1],"r");
AVLTree newNode = NULL;
if(outfile == NULL)
{
printf("File does not exist...\n");
return NULL;
}
int delimiterCount = 0;
char inputData[2001];
while((fgets(inputData,2001,outfile)) != NULL)
{
int len = strlen(inputData);
if (len >= 0 && inputData[len-1] == '\n')
inputData[--len] = '\0';
AVLTree temp = NULL;
//initialize the newNode
temp = (struct tree*)malloc(sizeof(struct tree));
temp->height = 0;
temp->left = NULL;
temp->right = NULL;
char* token = strtok(inputData, ",");
strcpy(temp->title,token);
while (token != NULL)
{
token = strtok(NULL, ",");//reading the rest of the tokens with strtok
delimiterCount++;
switch (delimiterCount) // this switch orders the tokens by their places
{
case 1:
strcpy(temp->year,token);
break;
case 2:
strcpy(temp->imdbID,token);
break;
case 3:
strcpy(temp->posterlink,token);
break;
}
}
newNode = temp;
MyApp->myTree = InsertElement(newNode,MyApp->myTree);
int x = atoi(newNode->year);
MyApp->myYearTree = InsertElementYear(x,MyApp->myYearTree);
delimiterCount = 0;
}
fclose(outfile);
return MyApp;
}
我的结构定义是:
typedef struct tree * AVLTree;
typedef struct year *AVLYear;[enter image description here][1]
struct tree{
char title[512];
char posterlink[1024];
char imdbID[512];
char year[512];
struct tree* left;
struct tree* right;
int height;
};
struct MyApp{
AVLTree myTree;
AVLYear myYearTree;
int maxMovieYear;
int maxMovieNum;
};
typedef struct MyApp* MYAPP;
typedef struct year *AVLYear;
struct year
{
int year;
int num;
AVLYear left;
AVLYear right;
int height;
};
当我试图获取文件名作为参数时,fopen 会返回这个
**<msvcrt!_iob+96>**
什么可能导致这个问题?
我的目标是从文件中获取数据并将其放入 2 个不同的 avltree 中,我将树保存在另一个名为 MYAPP 的结构中,以便在函数之间轻松传递。会是问题吗?
我看不到这个错误的原因。
编辑:我正在使用我自己的头文件来进行结构定义和函数。还包括:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "AVLTree.h"
#include "AVLYear.h"
谢谢。
【问题讨论】:
-
也许可以尝试
perror("filename");(将"filename"替换为您正在使用的名称)让运行时库帮助您诊断问题? -
fopen出错时返回 0 或指向打开的文件结构的指针。 那个打开的文件结构对你没有意义。所以不管它是什么,它就是。 -
我尝试添加
perror(argv[1]);说没有错误。还有什么问题。在我使用 MYAPP 之前它正在工作 -
看来你看到的是你的调试器对指针的解释。这意味着它指向某个符号“+96 字节”。
-
if (len >= 0 && inputData[len-1] == '\n')-->>if (len >=0 && inputData[len-1] == '\n')(首选:inputData[strcspn(inputData, "\r\n")] = 0;
标签: c function pointers segmentation-fault fopen