程序在执行strcpy()函数后崩溃(Program crashes after perfoming strcpy() function)

我需要一些帮助,我必须为学校做一个分配,其中包括在标题,作者和出版日期之后对一些书籍进行分类。 所有信息都以txt文件中的字符串形式给出,它们之间使用分隔符。 问题是我无法正确读取数据,我尝试执行strcpy()后程序崩溃了。 你能帮我解决这个问题并告诉我,我做错了什么?

#include <stdio.h> #include <stdlib.h> #include <string.h> struct book { char author[100],title[100]; int year; }; int main() { struct book b[25]; int i,n; char intro[25][150],*p; const char delim[2] = "#"; FILE *fp; fp=fopen("text.txt", "r"); fscanf(fp,"%d",&n); for(i=0;i<=n;i++) { fgets(intro[i], sizeof (intro[i]), fp); p=strtok(intro[i], delim); strcpy(b[i].title,p); p=strtok(NULL, delim); strcpy(b[i].author,p); /// The program works until it reaches this point - after performing this strcpy() it crashes if(p!=NULL) { p=strtok(NULL,delim); b[i].year=atoi(p); } } return 0; }

输入的一个例子可能是这样的:

5 Lord Of The Rings#JRR Tolkien#2003 Emotional Intelligence#Daniel Goleman#1977 Harry Potter#JK Rowling#1997 The Foundation#Isaac Asimov#1952 Dune#Frank Herbert#1965

I need some help with an assignement I have to do for school which consists in sorting some books after the title,author and the publication date of it. All the infos are given as a string in a txt file using a delimiter between them. The problem is that I don't manage to properly read the data, my program crashes after I try to perform a strcpy(). Can you guys help me with this and tell me what have I done wrong?

#include <stdio.h> #include <stdlib.h> #include <string.h> struct book { char author[100],title[100]; int year; }; int main() { struct book b[25]; int i,n; char intro[25][150],*p; const char delim[2] = "#"; FILE *fp; fp=fopen("text.txt", "r"); fscanf(fp,"%d",&n); for(i=0;i<=n;i++) { fgets(intro[i], sizeof (intro[i]), fp); p=strtok(intro[i], delim); strcpy(b[i].title,p); p=strtok(NULL, delim); strcpy(b[i].author,p); /// The program works until it reaches this point - after performing this strcpy() it crashes if(p!=NULL) { p=strtok(NULL,delim); b[i].year=atoi(p); } } return 0; }

An example of input could be this:

5 Lord Of The Rings#JRR Tolkien#2003 Emotional Intelligence#Daniel Goleman#1977 Harry Potter#JK Rowling#1997 The Foundation#Isaac Asimov#1952 Dune#Frank Herbert#1965

最满意答案

问题是在初始fscanf()调用之后文件中保留了换行符。

这个

fscanf(fp,"%d",&n);

读取5 ,后续fgets()只读取\n 。 所以这不是你想要的。 使用fgets()读取n并使用sscanf()或strto*将其转换为整数。 例如,您可以执行以下操作,而不是fscanf()调用:

char str[256]; fgets(str, sizeof str, fp); sscanf(str, "%d", &n);

从文件中读取n 。

您还应该检查strtok()返回NULL。 如果你这样做,你会很容易想出这个问题。

另外,你需要从0到n-1 。 因此for循环中的条件是错误的。 它应该是

for(i=0; i<n; i++)

The problem is with the newline left in the file after the initial fscanf() call.

This

fscanf(fp,"%d",&n);

reads the 5 and the subsequent fgets() reads just the \n. So this is not what you want. Read n using fgets() and convert it into integer using sscanf() or strto*. For example, instead of the fscanf() call, you can do:

char str[256]; fgets(str, sizeof str, fp); sscanf(str, "%d", &n);

to read the n from file.

You should also check if strtok() returns NULL. If you did, you would have figured out the issue easily.

Also, your need to go from 0 to n-1. So the condition in the for loop is wrong. It should be

for(i=0; i<n; i++)

更多推荐