尝试从文件读取下一个 line/character 时出现分段错误
Segmentation Fault while trying to read next line/character from file
我有一个学校项目,我需要用 C 读取 .ppm 文件并将其存储在一个结构中以供以后的任务使用。获取第一行并将其分配给结构变量后,如果我尝试再次浏览该文件,则会出现错误。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int r, g, b;
} pixels;
typedef struct {
int width, height, maxColors;
char *format; //PPM file format
char *comments;
pixels *pixels;
} PPM;
PPM *getPPM(FILE * f) {
// Check if the file is valid. If not then exit the program
if(f == NULL) {
perror("Cannot open file: ");
exit(1);
}
PPM *pic = malloc(sizeof(PPM));
// If memory allocation fails, exit the program.
if(!pic) {
perror("Unable to allocate memory for structure");
exit(1);
}
// Store the first line of file into the structure
char *fileFormat;
if(fgets(fileFormat, sizeof(fileFormat), f) != NULL) {
// If it is a valid .ppm format, then store it in the structure
if(fileFormat[0] == 'P')
pic->format = fileFormat;
} else {
perror("Unable to read line from the input file");
exit(1);
}
//Errors start here
int c = getc(f);
while(c != EOF)
c = getc(f);
/*
char *comments;
if(fgets(comments, sizeof(comments), f) != NULL) {
printf("%s\n", comments);
} else {
perror("Unable to read line from the input file");
exit(1);
}
*/
fclose(f);
return pic;
}
int main(int argc, char **argv) {
PPM *p = getPPM(fopen(argv[1], "r"));
printf(" PPM format = %s",p->format);
return 0;
}
我试过从文件中获取单个字符。我已经尝试使用 fgets 读取整行,就像我在上一步(对于 fileFormat)中所做的那样,但每次它都会给出段错误。我试过查看其他示例,但无法找出问题所在。我已经用了几个小时了,所以任何帮助将不胜感激!
会不会是内存分配方式的问题?或者当我尝试读取新行时是否需要提供某种指向文件的指针?我试图在手册页中找到答案,但我什么也想不出来。
P.S。 while(c != EOF) { c = getc(f); } 下一个评论步骤只是为了看看它是否有效。我想将 .ppm 文件中的所有信息放入 PPM 结构中。
您正在读入一个未初始化的指针,所以这会崩溃。你需要一个缓冲区:
char *fileFormat = malloc(SIZE_OF_FILE_FORMAT);
另外 sizeof(fileFormat)
returns 指针的大小 在这种情况下这不是您想要的。您需要指针指向的缓冲区的大小。
我有一个学校项目,我需要用 C 读取 .ppm 文件并将其存储在一个结构中以供以后的任务使用。获取第一行并将其分配给结构变量后,如果我尝试再次浏览该文件,则会出现错误。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int r, g, b;
} pixels;
typedef struct {
int width, height, maxColors;
char *format; //PPM file format
char *comments;
pixels *pixels;
} PPM;
PPM *getPPM(FILE * f) {
// Check if the file is valid. If not then exit the program
if(f == NULL) {
perror("Cannot open file: ");
exit(1);
}
PPM *pic = malloc(sizeof(PPM));
// If memory allocation fails, exit the program.
if(!pic) {
perror("Unable to allocate memory for structure");
exit(1);
}
// Store the first line of file into the structure
char *fileFormat;
if(fgets(fileFormat, sizeof(fileFormat), f) != NULL) {
// If it is a valid .ppm format, then store it in the structure
if(fileFormat[0] == 'P')
pic->format = fileFormat;
} else {
perror("Unable to read line from the input file");
exit(1);
}
//Errors start here
int c = getc(f);
while(c != EOF)
c = getc(f);
/*
char *comments;
if(fgets(comments, sizeof(comments), f) != NULL) {
printf("%s\n", comments);
} else {
perror("Unable to read line from the input file");
exit(1);
}
*/
fclose(f);
return pic;
}
int main(int argc, char **argv) {
PPM *p = getPPM(fopen(argv[1], "r"));
printf(" PPM format = %s",p->format);
return 0;
}
我试过从文件中获取单个字符。我已经尝试使用 fgets 读取整行,就像我在上一步(对于 fileFormat)中所做的那样,但每次它都会给出段错误。我试过查看其他示例,但无法找出问题所在。我已经用了几个小时了,所以任何帮助将不胜感激!
会不会是内存分配方式的问题?或者当我尝试读取新行时是否需要提供某种指向文件的指针?我试图在手册页中找到答案,但我什么也想不出来。
P.S。 while(c != EOF) { c = getc(f); } 下一个评论步骤只是为了看看它是否有效。我想将 .ppm 文件中的所有信息放入 PPM 结构中。
您正在读入一个未初始化的指针,所以这会崩溃。你需要一个缓冲区:
char *fileFormat = malloc(SIZE_OF_FILE_FORMAT);
另外 sizeof(fileFormat)
returns 指针的大小 在这种情况下这不是您想要的。您需要指针指向的缓冲区的大小。