나는 작업중인 코드에서 45430 단어의 사전을 읽고 각 단어 내에 포함 된 사전의 다른 모든 단어를 파일에 인쇄합니다. 나는 파일 MyDictionary txt 파일을 문자 배열 단어 [45430] [30]로 읽은 다음 단어 - 인 - 워드 txt 파일에 이것을 인쇄하는 중 일하고 있습니다. 내가 그렇게 할 때 나는 44946 단어에서 seg fault로 빠져 나간다. 그러나 같은 while 루프에서 나는 또한 콘솔에 인쇄하고 모든 단어가 제대로 출력된다. 파일에 쓰는 중 왜이 오류가 발생합니까? 그리고 왜 콘솔에 쓰는 오류가 없습니까?사전 내용을 파일에 쓸 때 세그먼트 오류가 발생했습니다.
코드 : 당신은 간접 너무 많은 수준을 가지고
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <string.h>
//char ***alloc_array(int,int);
int main(void){
FILE *fr; //declare file read file pointer
FILE *fp; //declare file printed file pointer
//char array to read in up to 30 chars
char line[31];
long numwords=45430; //number of words in dictionary
int maxlength=31; // the longest string in dictionary (30 chars)
long i; //counts up to 45430
//allocate space for 45430 words at a max length of 30 chars (1 extra char for "\0")
char ***word = calloc(numwords, sizeof(char **));
for(i = 0; i != numwords; i++) {
word[i] = calloc(maxlength, sizeof(char *));
}
//Open MyDictionary.txt and determine if there is an error or not
fr = fopen ("MyDictionary.txt", "r"); // open the file for reading
if(fr==NULL){
printf("\nError! opening input file");
exit(1); //Program exits if file pointer returns NULL.
}
//Open words-within-words.txt and determine if there is an error or not
fp = fopen ("words-within-words.txt", "w"); // open the file for reading
if(fp==NULL){
printf("\nError! opening output file");
exit(1); //Program exits if file pointer returns NULL.
}
int j=0; //counts to 30 for max length
i=0;
while(fgets(line, 40, fr) != NULL){ //get a line, up to 40 chars from fr and put first . done if NULL
for(j=0;j<30;){
word[i][j]=&line[j];
j++;
}
j=0;
printf("\n%s",word[i][j]); //print out each word of dictionary to console on its own line
/*
if((i>4 && i<8)||(i>45428)){
fprintf(fp,"\nanalyze:word[i][0]=%s\tword[i][2]=%s\ti=%li",word[i][0],word[i][2],i+1);
}
*/
fprintf(fp,"%s",word[i][j]); //print out each word of dictionary to words-in-words on its own line
i++;
}
fclose(fr); //close the files prior to exiting
fclose(fp);
return 0;
} //main
내 과제에는 실제로 단어의 형식이 필요합니다. 단어 [num_words_in_dict] [length_longest]. 나는이 아이디어가 단어 안에있는 단어를 찾는 것이 더 쉬울 것이라고 생각합니다. 일단 단어를 모두 파일로 인쇄하는 법을 배웁니다. 고마워요 – user2985363
맞습니다. 그러나'strcpy()'는 목적지보다 10 바이트 더 짧기 때문에 대상을 오버플로 할 수 있습니다. – alk