C 动态创建二维数组并复制txt文件。分割误差
我一直得到这个代码的分段错误。有什么建议可以解决吗?我知道我应该在每个malloc()之后使用free(),但是我不知道如何在我的函数中使用free(),因为我正在返回创建的数组。信息技术奇怪的是,这个代码在家里有效,但在学校却不行,我必须让它工作。如有任何建议,将不胜感激C 动态创建二维数组并复制txt文件。分割误差,c,C,我一直得到这个代码的分段错误。有什么建议可以解决吗?我知道我应该在每个malloc()之后使用free(),但是我不知道如何在我的函数中使用free(),因为我正在返回创建的数组。信息技术奇怪的是,这个代码在家里有效,但在学校却不行,我必须让它工作。如有任何建议,将不胜感激 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> void chang
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void changeToLower(const char *regular, char *lowerCaseOnly)
{
size_t ctr;
size_t len = strlen(regular);
int i=0;
while(i<len)
{
lowerCaseOnly[i] = tolower((unsigned char) regular[i]);
i++;
}
lowerCaseOnly[len] = '\0';
}
char ** enterFile(int *size, char * nameofFile )
{
int l;
int ne;
int messageSize =165;
char fname[40] ;//="dictionary2.txt";
FILE *file =NULL;
int i ;
char **dArray =NULL;
int localsize =0;
if(strcmp(fname, "quit\n") == 0){
exit(-1);
}
printf("Enter the name of the dictionary file you'd to use: ");
fgets(fname,100, stdin);
strtok(fname, "\n");
strcpy(nameofFile,fname);
file = fopen(fname,"r");
if(file==NULL){ printf("Error could not find file;\n");}//In case file DNE
fscanf(file, "%d", size);
localsize=(*(size)*2)+1;
*size=localsize;
//Allocation of memory for 2D array
dArray = malloc(localsize*sizeof( char*));
for(i=0;i<localsize;i++){
dArray[i]=malloc(messageSize*sizeof(char));
}
i=0;
while(!feof(file))
{
fgets(dArray[i],messageSize, file);
strtok(dArray[i], "\n");
i++;
}
fclose(file);
printf("Dictionary File Loaded\n");
return dArray;
}
int main()
{
int size =0;
int ne, counter ;
char **dArray =NULL; //pointer to array DOUBLE ARRAY
char option ;
int x =1; //Neede DND
char nameofDictionary[40]=" ";
while(x!=5)
{
printf("|------------------------------------------------------|\n");
printf("| Enter Option 1: To open dictionary file |\n");
scanf(" %c", & option);
while((ne=getchar()) != '\n' && ne!= EOF );//====> DELETE
//printf("You entered this option:\t %c\n", option);
switch(option)
{
case '1':
dArray=enterFile(&size, nameofDictionary);
// printf("main file name now is: %s\n", nameofDictionary);
break;
case '2':
//option2(dArray, &size);
break;
case '3':
//option3(dArray, &size);
break;
case '4':
//dArray=option4(dArray,&size, nameofDictionary);
// printf("this should say futbol %s",dArray[151]);
break;
case '5':
x=5;
printf("Exit program.");
break;
default:
printf(">>>>INVALID: Enter a valid input, try again\n");
}
}
return 0;
}
#包括
#包括
#包括
#包括
void changeToLower(常量字符*常规,字符*小写)
{
尺寸/中心;
尺寸长度=标准长度(常规);
int i=0;
然而(i遗憾的是,您的代码在许多地方都缺乏错误检查。由于缺乏错误检查,有一个特定的地方您很可能会失败:
i=0;
while(!feof(file))
{
fgets(dArray[i],messageSize, file);
strtok(dArray[i], "\n");
i++;
}
这不需要检查以确保您将i
限制为dArray
的大小,因此您可以在此处轻松越界,例如,如果文件中的行数超过预期。您可能希望将循环更改为:
i=0;
while (i < localSize && fgets(dArray[i], messageSize, file))
{
strtok(dArray[i], "\n");
i++;
}
i=0;
而(i
检查代码的其余部分是否存在其他类似问题,如果缺少错误检查,可能会导致不好的事情发生。请编写防御代码!能否将此文件缩短一点?此文件之所以长,是因为字典文件很小,调试器指向哪一行?请注意,没有“分段错误”这样的术语,逐行打印后出现“分段错误”,我认为在使用malloc的最后一行之后和while循环之后,enterfile方法中会出现这种情况1)while(!feof(file))
更改为while(fgets(dArray[i],messageSize,file))
我需要释放指针(使用malloc后)有什么建议吗?这只是malloc
s-free
循环中每一行的反向操作,然后free
指针数组。在这个特定的例子中,我需要返回我用malloc创建的数组。我可以在main中释放dArray吗???。顺便说一句,非常感谢您-您可以free
在main
中释放它>,当你完成它时。我甚至在你的代码中看不到这一行。不管怎样,如果你有进一步的问题,那么你应该尝试调试它,如果你失败了,你应该发布一个新的问题和最新的代码,描述你的确切问题和你到目前为止尝试了什么。
i=0;
while (i < localSize && fgets(dArray[i], messageSize, file))
{
strtok(dArray[i], "\n");
i++;
}