文件操纵代码以意外的方式结束
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100000
#define N 40
int get_choice(void);
int check_word(char *word);
void insert_text(FILE ** p_fp);
char *add_word(char *word);
void add_word_in_dictionary(FILE ** p_fp);
void save_file(FILE **fp);
int count_characters(FILE **fp);
int count_spaces(FILE **fp);
void txt_to_array(FILE **fp, char x[MAX][N]);
void sort_array_alphabetically(char matrix[MAX][N]);
int count_words_same(char matrix[MAX][N]);
void create_istogram(char matrix[MAX][N]);
void print_symbols(int times,int numch);
void create_file(char matrix[MAX][N]);
int count_words(void);
int main()
{
FILE *fp;
FILE *fp2; /*fp for Alice....txt and fp2 for englishWords.txt */
int choice = 0;
char matrix[MAX][N] = {" "};
while(1)
{
choice = get_choice();
if(choice == 0)
{
insert_text(&fp);
}
if(choice == 1)
{
add_word_in_dictionary(&fp2);
}
if(choice == 2)
{
printf("\n You have entered correction mode but in this version, nothing happens \n");
}
if(choice == 3)
{
save_file(&fp);
printf("\n The file has been saved \n");
}
if(choice == 4)
{
printf("The number of character in the file : %d ",count_characters(&fp));
printf("The number of spaces in the file : %d ",count_spaces(&fp));
txt_to_array(&fp,matrix);
printf("The number of different words in the file : %d ",count_words()- count_words_same(matrix));
create_istogram(matrix);
create_file(matrix);
printf("\n A file has been created with the statistics of the text \n");
}
if(choice == 5)
{
break;
}
}
printf("\n The program has ended \n");
return 0;
}
int get_choice(void) {
int choice = 0;
printf("\n Select a choice from the below \n");
printf("\n Select 0 to add text \n");
printf("\n Select 1 to add new words in the dictionary \n");
printf("\n Select 2 to enter enter correction mode \n");
printf("\n Select 3 to save the text \n");
printf("\n Select 4 to see the statistics about your text \n");
printf("\n Select 5 to exit the program\n");
scanf("\n%d", &choice);
return choice;
}
int check_word(char *word)
{
FILE *readfile;
char word1[40];
readfile = fopen("englishWords.txt","r");
if(!readfile)
{
printf("\n There was an error opening the file \n");
return -1;
}
while(fscanf(readfile,"%s",word1) != EOF)
{
if(strcmp(word,word1) == 0)
return 1;
else
{
return 0;
}
}
fclose(readfile);
return 0;
}
void insert_text(FILE ** p_fp)
{
char word[100];
*p_fp = fopen("AlicesAdventuresInWonderland.txt", "a+");
fprintf(*p_fp, "%s\n", add_word(word));
return;
}
char *add_word(char *word)
{
printf("\n Please enter the word \n");
scanf(" %100[^\n]", word);
return word;
}
void add_word_in_dictionary(FILE ** p_fp)
{
char word[100];
*p_fp = fopen("englishWords.txt","a+");
fprintf(*p_fp, "%99s\n",add_word(word));
return;
}
void save_file(FILE **fp)
{
fclose(*fp);
return;
}
int count_characters(FILE **fp)
{
char ch = ' ';
int count_ch = 0;
*fp = fopen("AlicesAdventuresInWonderland.txt", "r");
if(*fp == NULL)
{
printf("\n There was an error opening the file \n");
return 0;
}
while(ch != EOF)
{
if(ch != ' ' && ch != '\n')
{
count_ch++;
}
ch=fgetc(*fp);
}
fclose(*fp);
return count_ch;
}
int count_spaces(FILE **fp)
{
int count_sp = 0;
char c;
*fp = fopen("AlicesAdventuresInWonderland.txt","r");
while ((c = fgetc(*fp)) != EOF)
{
if (c == ' ')
count_sp++;
}
return count_sp;
}
void txt_to_array(FILE **fp,char x[MAX][N])
{
int i = 0;
char str[MAX] = {" "};
*fp = fopen("AlicesAdventuresInWonderland.txt","r");
while(!feof(*fp))
{
while(fgets(str, sizeof str, *fp))
{
strcpy(x[i],str);
i++;
}
}
sort_array_alphabetically(x);
return;
}
void sort_array_alphabetically(char matrix[MAX][N])
{
int i = 0;
int j= 0;
char s[40] = {" "};
for(i=0;i<MAX;i++)
{
for(j=i+1;j<MAX;j++)
{
if(strcmp(matrix[i],matrix[j])>0)
{
strcpy(s,matrix[i]);
strcpy(matrix[i],matrix[j]);
strcpy(matrix[j],s);
}
}
}
return;
}
int count_words_same(char matrix[MAX][N])
{
int i;
int count = 0;
for(i = 0; i < MAX; i++)
{
if(strcmp(matrix[i],matrix[i+1]) == 0)
{
count++;
}
}
return count;
}
int count_words(void)
{
int words = 0;
char ch;
FILE *fp;
fp = fopen("AlicesAdventuresINWonderland.txt","r");
while((ch=fgetc(fp))!=EOF)
{
putchar(ch);
if((ch==' ')||(ch=='\n'))
{
words++;
}
}
fclose(fp);
return words;
}
void create_istogram(char matrix[MAX][N])
{
int a =0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int g = 0;
int h = 0;
int i = 0;
int j = 0;
int k = 0;
int m = 0;
for(m = 0 ; m < MAX; m++)
{
if((strlen(matrix[m])) == 1)
{
a++;
}
if((strlen(matrix[m])) == 2)
{
b++;
}
if((strlen(matrix[m])) == 3)
{
c++;
}
if((strlen(matrix[m])) == 4)
{
d++;
}
if((strlen(matrix[m])) == 5)
{
e++;
}
if((strlen(matrix[m])) == 6)
{
f++;
}
if((strlen(matrix[m])) == 7)
{
g++;
}
if((strlen(matrix[m])) == 8)
{
h++;
}
if((strlen(matrix[m])) == 9)
{
i++;
}
if((strlen(matrix[m])) == 10)
{
j++;
}
if((strlen(matrix[m])) == 11)
{
k++;
}
if((strlen(matrix[m])) > 11)
{
k++;
}
}
print_symbols(a-1,1);
print_symbols(b,2);
print_symbols(c,3);
print_symbols(d,4);
print_symbols(e,5);
print_symbols(f,6);
print_symbols(g,7);
print_symbols(h,8);
print_symbols(i,9);
print_symbols(j,10);
print_symbols(k,11);
printf("Total number of strings with more than 11 characters : %d\n",k);
return;
}
void print_symbols(int times,int numch)
{
int i;
printf(" %d: ",numch);
for(i = 0 ; i < times; i ++)
{
printf("*");
}
printf(" (%d) ",times);
printf("\n");
return;
}
void create_file(char matrix[MAX][N])
{
int total_ch;
int total_words;
int total_ch_wo_sp;
int total_dif_words;
FILE **fp;
FILE **p_fp;
*fp = fopen("AlicesAdventuresInWonderland.txt","r");
total_ch = count_characters(fp);
total_words = count_words();
total_ch_wo_sp = total_ch - count_spaces(fp);
total_dif_words = total_words - count_words_same(matrix);
*p_fp = fopen("data.txt","wt");
fprintf(*p_fp, "Total number of characters : %d\n",total_ch);
fprintf(*p_fp,"Total number of words : %d\n",total_words);
fprintf(*p_fp,"Total number of characters without spaces : %d\n",total_ch_wo_sp);
fprintf(*p_fp,"Total number of different words : %d\n",total_dif_words);
return;
}
问题:当我运行代码时,它出乎意料的结束,我无法认为为什么
- 会
- 。
- 发生 关于.txt文件(总空间的数量,总字符的数量,总单词数和总单词数)
- 将文本添加到.txt文件中,
- 将文本的统计信息保存在.txt文件
- 中多少个单词具有1个字符,多少个2等...
我猜我的代码中有一些致命错误,导致该程序以出乎意料的方式结束,但我找不到它们。.我也不知道使用调试器以及您可以通过代码来判断我是新手...
我更具体地发现的错误是当我单击编译并运行即使我不输入某些内容时(一个数字,指示哪些功能/函数将执行哪些函数/函数)程序刚刚结束(在终端I中返回 a_number ..更重要的是该程序已经结束 - 这表明程序的结束未打印...
The code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100000
#define N 40
int get_choice(void);
int check_word(char *word);
void insert_text(FILE ** p_fp);
char *add_word(char *word);
void add_word_in_dictionary(FILE ** p_fp);
void save_file(FILE **fp);
int count_characters(FILE **fp);
int count_spaces(FILE **fp);
void txt_to_array(FILE **fp, char x[MAX][N]);
void sort_array_alphabetically(char matrix[MAX][N]);
int count_words_same(char matrix[MAX][N]);
void create_istogram(char matrix[MAX][N]);
void print_symbols(int times,int numch);
void create_file(char matrix[MAX][N]);
int count_words(void);
int main()
{
FILE *fp;
FILE *fp2; /*fp for Alice....txt and fp2 for englishWords.txt */
int choice = 0;
char matrix[MAX][N] = {" "};
while(1)
{
choice = get_choice();
if(choice == 0)
{
insert_text(&fp);
}
if(choice == 1)
{
add_word_in_dictionary(&fp2);
}
if(choice == 2)
{
printf("\n You have entered correction mode but in this version, nothing happens \n");
}
if(choice == 3)
{
save_file(&fp);
printf("\n The file has been saved \n");
}
if(choice == 4)
{
printf("The number of character in the file : %d ",count_characters(&fp));
printf("The number of spaces in the file : %d ",count_spaces(&fp));
txt_to_array(&fp,matrix);
printf("The number of different words in the file : %d ",count_words()- count_words_same(matrix));
create_istogram(matrix);
create_file(matrix);
printf("\n A file has been created with the statistics of the text \n");
}
if(choice == 5)
{
break;
}
}
printf("\n The program has ended \n");
return 0;
}
int get_choice(void) {
int choice = 0;
printf("\n Select a choice from the below \n");
printf("\n Select 0 to add text \n");
printf("\n Select 1 to add new words in the dictionary \n");
printf("\n Select 2 to enter enter correction mode \n");
printf("\n Select 3 to save the text \n");
printf("\n Select 4 to see the statistics about your text \n");
printf("\n Select 5 to exit the program\n");
scanf("\n%d", &choice);
return choice;
}
int check_word(char *word)
{
FILE *readfile;
char word1[40];
readfile = fopen("englishWords.txt","r");
if(!readfile)
{
printf("\n There was an error opening the file \n");
return -1;
}
while(fscanf(readfile,"%s",word1) != EOF)
{
if(strcmp(word,word1) == 0)
return 1;
else
{
return 0;
}
}
fclose(readfile);
return 0;
}
void insert_text(FILE ** p_fp)
{
char word[100];
*p_fp = fopen("AlicesAdventuresInWonderland.txt", "a+");
fprintf(*p_fp, "%s\n", add_word(word));
return;
}
char *add_word(char *word)
{
printf("\n Please enter the word \n");
scanf(" %100[^\n]", word);
return word;
}
void add_word_in_dictionary(FILE ** p_fp)
{
char word[100];
*p_fp = fopen("englishWords.txt","a+");
fprintf(*p_fp, "%99s\n",add_word(word));
return;
}
void save_file(FILE **fp)
{
fclose(*fp);
return;
}
int count_characters(FILE **fp)
{
char ch = ' ';
int count_ch = 0;
*fp = fopen("AlicesAdventuresInWonderland.txt", "r");
if(*fp == NULL)
{
printf("\n There was an error opening the file \n");
return 0;
}
while(ch != EOF)
{
if(ch != ' ' && ch != '\n')
{
count_ch++;
}
ch=fgetc(*fp);
}
fclose(*fp);
return count_ch;
}
int count_spaces(FILE **fp)
{
int count_sp = 0;
char c;
*fp = fopen("AlicesAdventuresInWonderland.txt","r");
while ((c = fgetc(*fp)) != EOF)
{
if (c == ' ')
count_sp++;
}
return count_sp;
}
void txt_to_array(FILE **fp,char x[MAX][N])
{
int i = 0;
char str[MAX] = {" "};
*fp = fopen("AlicesAdventuresInWonderland.txt","r");
while(!feof(*fp))
{
while(fgets(str, sizeof str, *fp))
{
strcpy(x[i],str);
i++;
}
}
sort_array_alphabetically(x);
return;
}
void sort_array_alphabetically(char matrix[MAX][N])
{
int i = 0;
int j= 0;
char s[40] = {" "};
for(i=0;i<MAX;i++)
{
for(j=i+1;j<MAX;j++)
{
if(strcmp(matrix[i],matrix[j])>0)
{
strcpy(s,matrix[i]);
strcpy(matrix[i],matrix[j]);
strcpy(matrix[j],s);
}
}
}
return;
}
int count_words_same(char matrix[MAX][N])
{
int i;
int count = 0;
for(i = 0; i < MAX; i++)
{
if(strcmp(matrix[i],matrix[i+1]) == 0)
{
count++;
}
}
return count;
}
int count_words(void)
{
int words = 0;
char ch;
FILE *fp;
fp = fopen("AlicesAdventuresINWonderland.txt","r");
while((ch=fgetc(fp))!=EOF)
{
putchar(ch);
if((ch==' ')||(ch=='\n'))
{
words++;
}
}
fclose(fp);
return words;
}
void create_istogram(char matrix[MAX][N])
{
int a =0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int g = 0;
int h = 0;
int i = 0;
int j = 0;
int k = 0;
int m = 0;
for(m = 0 ; m < MAX; m++)
{
if((strlen(matrix[m])) == 1)
{
a++;
}
if((strlen(matrix[m])) == 2)
{
b++;
}
if((strlen(matrix[m])) == 3)
{
c++;
}
if((strlen(matrix[m])) == 4)
{
d++;
}
if((strlen(matrix[m])) == 5)
{
e++;
}
if((strlen(matrix[m])) == 6)
{
f++;
}
if((strlen(matrix[m])) == 7)
{
g++;
}
if((strlen(matrix[m])) == 8)
{
h++;
}
if((strlen(matrix[m])) == 9)
{
i++;
}
if((strlen(matrix[m])) == 10)
{
j++;
}
if((strlen(matrix[m])) == 11)
{
k++;
}
if((strlen(matrix[m])) > 11)
{
k++;
}
}
print_symbols(a-1,1);
print_symbols(b,2);
print_symbols(c,3);
print_symbols(d,4);
print_symbols(e,5);
print_symbols(f,6);
print_symbols(g,7);
print_symbols(h,8);
print_symbols(i,9);
print_symbols(j,10);
print_symbols(k,11);
printf("Total number of strings with more than 11 characters : %d\n",k);
return;
}
void print_symbols(int times,int numch)
{
int i;
printf(" %d: ",numch);
for(i = 0 ; i < times; i ++)
{
printf("*");
}
printf(" (%d) ",times);
printf("\n");
return;
}
void create_file(char matrix[MAX][N])
{
int total_ch;
int total_words;
int total_ch_wo_sp;
int total_dif_words;
FILE **fp;
FILE **p_fp;
*fp = fopen("AlicesAdventuresInWonderland.txt","r");
total_ch = count_characters(fp);
total_words = count_words();
total_ch_wo_sp = total_ch - count_spaces(fp);
total_dif_words = total_words - count_words_same(matrix);
*p_fp = fopen("data.txt","wt");
fprintf(*p_fp, "Total number of characters : %d\n",total_ch);
fprintf(*p_fp,"Total number of words : %d\n",total_words);
fprintf(*p_fp,"Total number of characters without spaces : %d\n",total_ch_wo_sp);
fprintf(*p_fp,"Total number of different words : %d\n",total_dif_words);
return;
}
The problem: When I run the code it ends unexpectedly and I cannot think why this is happenning..The program above is supposed to do the following
- Read a .txt file
- Have a 'menu' with some basic choices
- Compute some statistics concerning the .txt file (number of total spaces,number of total characters,number of total words and the number of total words)
- Add text to the .txt file
- Save the statistics of the text in a .txt file
- Print an istogram which contains how many words have 1 character,how many 2,etc...
I guess there are some fatal errors in my code that is causing the program to end in an unexpected way but I am unable to find them..Also I am not aware of using the debugger and as you can judge by the code I am a newbie...
The error I get more specifically is when I click compile and run even though I do not enter something (a number which indicates which function/functions will execute) the program just ends (in the terminal i get process returned a_number..What's more The program has ended-which indicates the end of the program is not printed...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将指出以下功能的问题:
fp
和p_fp
是变量,指向类型file> file
的指针。当您第一次使用它们*fp = fopen()
时,您将取消一个非初始化的指针。在上面的示例中,p是非初始化的,很可能P具有0(非法地址)或任何随机数(再次是非法地址)。
为什么需要指针的指针?如果您希望在其他功能传递时修改指针,但事实并非如此,甚至不是在
count_characters
中,这将很有用。I am going to point out an issue with the following function:
fp
andp_fp
are variables that point to a pointer of typeFILE
. When you first use them doing*fp = fopen()
you are dereferencing an uninitialized pointer.In the above example p is uninitialised, and it's very likely p has 0 (illegal address) or whatever random number (again, an illegal address).
Why do you need a pointer of pointers? This is useful if you want your pointer to be modified whenever it is passed around by other functions, but that's not the case, not even in
count_characters
.0xc00000fd
在Windows中指示A 堆栈溢出。使用矩阵
,您正在尝试分配40
*100000
bytes(4MB),带有自动存储持续时间(aka,aka,在堆栈上”)。默认情况下,Windows的最大堆栈大小为1MB。
使用动态内存(
malloc
),或通过将其移动到main
外,使其 global :更改此操作将使您超越第一个障碍,但是您的代码还有其他问题,包括但不限于:
在
sort_array_alphabet上
,外循环,for(i = 0; i&lt; max; max; i ++)
,应该BEfor(i = 0; i&lt; max-1; i ++)
,以便内部循环无法访问界外索引。在
create_file
中,这些双重指针是未经初始化的,包含不确定的指针值。
*FP
和*P_FP
有效地指向随机内存。之后,您将这些功能传递给尝试重新打开相同文件的函数,并使用相同的不确定指针值。
您需要一个指针
文件 *fp
,并确定要在何处打开关闭文件。这是传递指针的地址的粗略示例,该指针指向要初始化的另一个函数:
0xC00000FD
indicates a stack overflow in Windows. Withmatrix
, you are attempting to allocate40
*100000
bytes (4MB) with automatic storage duration (a.k.a., "on the stack").By default, Windows has a maximum stack size of 1MB.
Use dynamic memory (
malloc
), or makematrix
a global by moving it outside ofmain
:Changing this will get you past the first hurdle, but there are other issues with your code, including but not limited to:
In
sort_array_alphabetically
, the outer loop,for(i=0;i<MAX;i++)
, should befor(i = 0; i < MAX - 1; i++)
, so that the inner loop does not access an out-of-bounds index.In
create_file
, these double pointersare uninitialized, containing an indeterminate pointer value.
*fp
and*p_fp
effectively point to random memory.Afterwards you pass these to functions that attempt to reopen the same files, using the same indeterminate pointer value.
You need a single pointer
FILE *fp
, and to decide where you are going to open close the files.Here is a cursory example of passing the address of a pointer to another function to be initialized: