[英]Segmentation fault (core dumped) error when running my program
so i am supposed to create a program that analyzes said text file and when i first ran it, i didn't have the text file created, but once i created it and tried to run the program i got a seg fault error and now i don't know how to fix it.所以我应该创建一个分析所述文本文件的程序,当我第一次运行它时,我没有创建文本文件,但是一旦我创建它并尝试运行该程序,我得到了一个段错误错误,现在我不知道如何解决。
#include <stdio.h>
#include <string.h>
#define FILENAME "poems.txt"
int getWords(int maxWord, char words[][maxWord], FILE*);
int countLetters(char str_letter[]);
int countLowerCase(char str_lower[]);
int countVowels(char str_vowel[]);
int main()
{
int maxWord=50;
int numWord=0;
char words[numWord][maxWord];
char str_letter[75];
char str_lower[75];
char str_vowel[75];
FILE* fp;
int numLetter;
int numLower;
int numVowel;
fp=fopen(FILENAME, "r");
if(fp==NULL) {
printf ("poems.txt could not be found!\n");
}
else {
getWords(maxWord, words, fp);
countLetters(str_letter);
countLowerCase(str_lower);
countVowels(str_vowel);
printf ("There are %d letters in your file.\n", numLetter);
printf ("There are %d lower case letters in your file.\n", numLower);
printf ("There are %d vowels in your file.\n", numVowel);
fclose(fp);
}
return 0;
}
int getWords(int maxWord, char words[][maxWord], FILE* inFILE)
{
int numWord=0;
char poem;
while(fscanf(inFILE, "%s", &poem)==1){
words[numWord][maxWord]=poem;
numWord++;
}
numWord++;
return numWord;
}
int countLetters(char str_letter[])
{
int numLetter=0;
for(int i=0; i<strlen(str_letter); i++) {
if((str_letter[i]>'a' && str_letter[i]<'z')||(str_letter[i]>'A' && str_letter[i]<'Z'));
numLetter++;
}
return numLetter;
}
int countLowerCase(char str_lower[])
{
int numLower=0;
for(int i=0; i<strlen(str_lower); i++) {
if(str_lower[i]>'a' && str_lower[i]<'z');
numLower++;
}
return numLower;
}
int countVowels(char str_vowel[])
{
int numVowel=0;
for(int i=0; i<strlen(str_vowel); i++) {
if(str_vowel[i]=='a'||str_vowel[i]=='e'||str_vowel[i]=='i'||str_vowel[i]=='o'||str_vowel[i]=='u')
{
numVowel++;
}
else if(str_vowel[i]=='A'||str_vowel[i]=='E'||str_vowel[i]=='I'||str_vowel[i]=='O'||str_vowel[i]=='U')
{
numVowel++;
}
}
return numVowel;
}
You are making so many mistakes on your code, so i need to ask, you understands how dynamic memory allocation and pointers works in C?您在代码上犯了很多错误,所以我需要问一下,您了解动态 memory 分配和指针在 C 中的工作原理吗? Is not like other languajes, you need to allocate memory before you access to them.
与其他语言不同,您需要先分配 memory 才能访问它们。
If you dont have a text file, your code works and the output is:如果您没有文本文件,则您的代码可以工作,并且 output 是:
poems.txt could not be found!
So up to here everithings seems to work fine, but it doesn't.所以到这里为止一切似乎都可以正常工作,但事实并非如此。
Lets go to the line where you declare:让 go 到您声明的行:
int maxWord=50;
int numWord=0;
char words[numWord][maxWord];
Here you are saying "SAVE ME AN ARRAY OF 0 POSITIONS AS COLUMNS AND 50 POSITIONS OF ROWS" so when you execute the following code in the function getWords, your code breaks.在这里,您说“将 0 个位置的数组保存为列和 50 个位置的行”,因此当您在 function getWords 中执行以下代码时,您的代码会中断。
while(fscanf(inFILE, "%s", &poem)==1){
words[numWord][maxWord]=poem;
numWord++;
}
Because the position:因为 position:
words[1][maxWord]
isn't allocated.没有分配。
You got two ways of allocate memory in C as far as i know, dynamic and static memory allocation.据我所知,您有两种在 C 中分配 memory 的方法,即动态分配和 static ZCD691ZB4957F09CDE818 分配。
I believe you came from learning another languaje like python or javascript where this types of things "works" but not in C.我相信您来自学习另一种语言,例如 python 或 javascript,这种类型的东西“有效”,但在 C 中无效。
If you want to solve this problems take a look to dynamic memory allocation and how C pointers works, you can make your code unstopable!如果您想解决此问题,请查看动态 memory 分配以及 C 指针的工作原理,您可以让您的代码无法停止!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.