gpt4 book ai didi

c - 如何摆脱段错误 : 11

转载 作者:太空宇宙 更新时间:2023-11-04 08:21:35 24 4
gpt4 key购买 nike

我知道可能还有一大堆其他问题,但在解决段错误之前我无法真正排除故障。我很确定这与我对 mallocs 的使用有关,但我不确定它的确切来源。我对编码还是个新手,因此也欢迎任何其他建议。我使用的测试文件只有一行,上面写着“The quick brown fox jumps over the lazy old dog”,但它应该能够用于任何事情。所有其他信息都应在代码注释中。

//This program is supposed to act as line justification formater.
//When running the program, the user will specify what justification
//type they want (either left, right, or center) and the column width.
//The program will output the text of the file broken up over several
//and justified appropriately.



#include <stdio.h>
#include "scanner.h"
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]){

FILE *fp = fopen("argv[2]","r");

int checkArguments(char[],int); //function declarations
char getString (FILE *);
int justify(char,int,int,char **);


char *justif=argv[4]; //these two variables hold the width and length
char *widthC=argv[3]; //that the user puts in when they run the function
int width=atoi(widthC);
int wordsSum=0; //this variable holds the total number of wordis
int i;

checkArguments(justif,width);

char **words = malloc(sizeof(char *)*1); //this string will hold each word as a token

for (i=0;;i++){
if (feof(fp)) break;
else {
char **temp;
wordsSum++;

temp=realloc(words,(i+1)*sizeof(char*));
if(temp!=NULL){ //this reallocates the memory for words
words=temp; //and if the reallocation was succsesful
char tempString=getString(fp); //gives words a new token
words[i]=&tempString;
i++;
}
else {
free(words);
printf("Malloc error :c\n");
return 0;
}
}


}

justify(*justif,width,wordsSum,words);

fclose(fp);

return 0;

}



/*This program does two simple checks to make sure the user put in valid widths and lengths */

int checkArguments(char a[], int width){
if (!(strcmp(a,"right")==0 || strcmp(a,"left")==0 || strcmp(a,"center")==0)){
printf("You did not enter a possible justification, please try again.");
exit(0);
}
else if(width<=0){
printf("You did not enter a possible width, please try again.");
exit(0);
}
else return 0;
}


/*This program reads the file and stores characters of one word inside a malloc string
It then does a malloc realocation check and, if sucsessful, returns the word*/

char getString (FILE *fp){

char c;
int i;
char *s=malloc(sizeof(int));
char *temp;

for (i=0;;i++) {
c=fgetc(fp);
if (c==' ' || c=='\n')
break;
s[i]=c;
temp=realloc(s,(i+1)*sizeof(int));
if(temp!=NULL){
s=temp;
}
else {
free(s);
printf("Malloc error :c\n");
return 0;
}
}
return *s;
}

/*This function will eventually have another if else statement at the beggining, and
print different amounts of spaces in the beggining of the line depending on what
justification the user wants. For now it assumes left justified.*/

int justify(char justif, int width, int wordsSum, char ** words){

int i=0,j=0,k=0;
while(i<wordsSum) //while there are more words to print
{
while(j<=width) //while not at the maximum width for lines
{
if(j+strlen(words[i])<=width){ //find if adding another word would put
j+=strlen(words[i]); //the line width over the maximum width
i++; //if no, add another word
}
else{ //if yes, print the current line
for (k=0;k<=j;k++)
printf ("%s",words[k]);
printf("\n");
j=0; //reset the line length to zero
}
}
}
return 0;
}

最佳答案

FILE *fp = fopen("argv[2]","r");   

应该是-

FILE *fp = fopen(argv[2],"r");   
if(fp==NULL)
printf("error in opening file");

您再次将 words 指向 temp 因此您失去了对之前使用 malloc 分配给它的内存的引用。注意之前分配的其他内容内存不会被释放。

在函数 char getString (FILE *fp) 中,您分配的 char *s 内存等于 sizeof(int)。是故意的吗?

关于c - 如何摆脱段错误 : 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33175340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com