Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 具有较大不可理解错误的分段错误?_C_String_File_Sorting_Segmentation Fault - Fatal编程技术网

C 具有较大不可理解错误的分段错误?

C 具有较大不可理解错误的分段错误?,c,string,file,sorting,segmentation-fault,C,String,File,Sorting,Segmentation Fault,该程序旨在按字母顺序对单词进行排序,可以是输入到该程序中的单词,也可以是来自文本文件的单词。它编译得很好,但是当我运行它时,我得到了大量的文本。以下是一个小样本: :*.dl=01;35:**.xcf=01;35:**.xwd=01;35:**.yuv=01;35:**.cgm=01;35:*。电动势=01;35:**.axv=01;35:**.anx=01;35:**.ogv=01;35:**.ogx=01;35:*。aac=00;36:**.au=00;36:**.flac=00;36:**

该程序旨在按字母顺序对单词进行排序,可以是输入到该程序中的单词,也可以是来自文本文件的单词。它编译得很好,但是当我运行它时,我得到了大量的文本。以下是一个小样本:
:*.dl=01;35:**.xcf=01;35:**.xwd=01;35:**.yuv=01;35:**.cgm=01;35:*。电动势=01;35:**.axv=01;35:**.anx=01;35:**.ogv=01;35:**.ogx=01;35:*。aac=00;36:**.au=00;36:**.flac=00;36:**.mid=00;36:*midi=00;36:**.mka=00;36:*。mp3=00;36:**.mpc=00;36:**.ogg=00;36:*ra=00;36:*wav=00;36:*。axa=00;36:**.oga=00;36:*。spx=00;36:**.xspf=00;36:
v=01;35:**.ogx=01;35:*。aac=00;36:**.au=00;36:**.flac=00;36:**.mid=00;36:*midi=00;36:**.mka=00;36:*。mp3=00;36:**.mpc=00;36:**.ogg=00;36:*ra=00;36:*wav=00;36:*。axa=00;36:**.oga=00;36:*。spx=00;36:**.xspf=00;36:

它看起来有点像一些文件格式之类的?
然后是单词:
分段故障(堆芯倾倒)

我正在Ubuntu上用GCC编译。
该计划是:

#include <stdio.h>
#include <string.h>

#define MO 109 // 109 is ASCII for "m".
#define FO 102 // 102 is ASCII for "f".
#define OO 101 // 101 is ASCII for "e" and denotes an error.

int main() // Main part of program.
{
        int i, j; // Counter integer assignment.
        int n = 100; // assignment of integer for the number of strings.
        char a; // For the m/f (manual or file) option.
        char str[100][100]; // Str is the main string to be sorted.
        char temp[100]; // Temp is to switch the values for bubble sorting.
        for(i = 0; i < 1; a = OO)
        {
                printf("To input text manually, press m. To sort a file, press f. \n");   
                // M/f option.
                scanf("%c", &a); // Gets m/f option.
                if(a == MO || a == FO) // Checks for valid input.
                {
                        i = 2; // Escape from loop with valid input.
                }
                if(a != MO && a != FO) // Invalid input.
                {
                        printf("Please insert a valid response. ");
                        i = 0; // Continue loop until a valid input is reached.
                }
        } 
        if(a == MO) // Manual insert option.
        {
                puts("Enter the number of strings to be sorted.");
                scanf("%d", &n); // Gets number of strings.
                for(i = 0; i <= n; i++)
                {
                        gets(str[i]); // Gets strings from user.
                }
        }
        if(a == FO) // File option.
        {
                char b[100]; // File address of text file to be sorted.
                FILE * f; // Text file.
                printf("Enter file path of file to be sorted.");
                scanf("%c", b); // Gets file path.
                f = fopen(b, "r"); // Opens file.
                fgets(*str, 100, f); // Coverts file into string str.
                fclose(f); // Closes file.
        }
        for(i = 0; i <= n; i++) // Begin bubble sort.
        {
                for(j = i + 1; j <= n; j++)
                {
                        if(strcmp(str[i], str[j]) > 0) // Checks alphabetical value.
                        {
                                 strcpy(temp, str[i]); // Switch two strings.
                                 strcpy(str[i], str[j]);
                                 strcpy(str[j], temp);
                        }
                }
        }
        printf("The sorted string:");
        for(i = 0; i <= n; i++)
        {
                puts(str[i]); // Prints final output.
        }
        return 0; // End of main.
}
#包括
#包括
#定义MO 109//109是表示“m”的ASCII码。
#定义FO 102//102是表示“f”的ASCII码。
#define OO 101//101是表示“e”的ASCII码,表示错误。
int main()//程序的主要部分。
{
int i,j;//计数器整数赋值。
int n=100;//为字符串数指定整数。
char a;//用于m/f(手动或文件)选项。
char str[100][100];//str是要排序的主字符串。
char temp[100];//temp用于切换气泡排序的值。
对于(i=0;i<1;a=OO)
{
printf(“要手动输入文本,请按m键。要对文件排序,请按f键。\n”);
//M/f选项。
scanf(“%c”,&a);//获取m/f选项。
if(a==MO | | a==FO)//检查有效输入。
{
i=2;//使用有效输入从循环中转义。
}
如果(a!=MO&&a!=FO)//输入无效。
{
printf(“请插入有效回复”);
i=0;//继续循环,直到达到有效输入。
}
} 
if(a==MO)//手动插入选项。
{
puts(“输入要排序的字符串数”);
scanf(“%d”,&n);//获取字符串数。

对于(i=0;i,正如对您的问题的一条评论所说,代码中有很多错误

#include <stdio.h>
#include <string.h>

#define MO 109 // 109 is ASCII for "m".
#define FO 102 // 102 is ASCII for "f".
#define OO 101 // 101 is ASCII for "e" and denotes an error.

int main() // Main part of program.
{
        int i, j; // Counter integer assignment.
        int n = 100; // assignment of integer for the number of strings.
        char a; // For the m/f (manual or file) option.
        char str[100][100]; // Str is the main string to be sorted.
        char temp[100]; // Temp is to switch the values for bubble sorting.
        for(i = 0; i < 1; a = OO)
        {
                printf("To input text manually, press m. To sort a file, press f. \n");   
                // M/f option.
                scanf("%c", &a); // Gets m/f option.
                if(a == MO || a == FO) // Checks for valid input.
                {
                        i = 2; // Escape from loop with valid input.
                }
                if(a != MO && a != FO) // Invalid input.
                {
                        printf("Please insert a valid response. ");
                        i = 0; // Continue loop until a valid input is reached.
                }
        } 
        if(a == MO) // Manual insert option.
        {
                puts("Enter the number of strings to be sorted.");
                scanf("%d", &n); // Gets number of strings.
                for(i = 0; i <= n; i++)
                {
                        gets(str[i]); // Gets strings from user.
                }
        }
        if(a == FO) // File option.
        {
                char b[100]; // File address of text file to be sorted.
                FILE * f; // Text file.
                printf("Enter file path of file to be sorted.");
                scanf("%c", b); // Gets file path.
                f = fopen(b, "r"); // Opens file.
                fgets(*str, 100, f); // Coverts file into string str.
                fclose(f); // Closes file.
        }
        for(i = 0; i <= n; i++) // Begin bubble sort.
        {
                for(j = i + 1; j <= n; j++)
                {
                        if(strcmp(str[i], str[j]) > 0) // Checks alphabetical value.
                        {
                                 strcpy(temp, str[i]); // Switch two strings.
                                 strcpy(str[i], str[j]);
                                 strcpy(str[j], temp);
                        }
                }
        }
        printf("The sorted string:");
        for(i = 0; i <= n; i++)
        {
                puts(str[i]); // Prints final output.
        }
        return 0; // End of main.
}
比如说

#include <stdio.h>
#include <string.h>

#define MO 109 // 109 is ASCII for "m".
#define FO 102 // 102 is ASCII for "f".
#define OO 101 // 101 is ASCII for "e" and denotes an error.

int main() // Main part of program.
{
        int i, j; // Counter integer assignment.
        int n = 100; // assignment of integer for the number of strings.
        char a; // For the m/f (manual or file) option.
        char str[100][100]; // Str is the main string to be sorted.
        char temp[100]; // Temp is to switch the values for bubble sorting.
        for(i = 0; i < 1; a = OO)
        {
                printf("To input text manually, press m. To sort a file, press f. \n");   
                // M/f option.
                scanf("%c", &a); // Gets m/f option.
                if(a == MO || a == FO) // Checks for valid input.
                {
                        i = 2; // Escape from loop with valid input.
                }
                if(a != MO && a != FO) // Invalid input.
                {
                        printf("Please insert a valid response. ");
                        i = 0; // Continue loop until a valid input is reached.
                }
        } 
        if(a == MO) // Manual insert option.
        {
                puts("Enter the number of strings to be sorted.");
                scanf("%d", &n); // Gets number of strings.
                for(i = 0; i <= n; i++)
                {
                        gets(str[i]); // Gets strings from user.
                }
        }
        if(a == FO) // File option.
        {
                char b[100]; // File address of text file to be sorted.
                FILE * f; // Text file.
                printf("Enter file path of file to be sorted.");
                scanf("%c", b); // Gets file path.
                f = fopen(b, "r"); // Opens file.
                fgets(*str, 100, f); // Coverts file into string str.
                fclose(f); // Closes file.
        }
        for(i = 0; i <= n; i++) // Begin bubble sort.
        {
                for(j = i + 1; j <= n; j++)
                {
                        if(strcmp(str[i], str[j]) > 0) // Checks alphabetical value.
                        {
                                 strcpy(temp, str[i]); // Switch two strings.
                                 strcpy(str[i], str[j]);
                                 strcpy(str[j], temp);
                        }
                }
        }
        printf("The sorted string:");
        for(i = 0; i <= n; i++)
        {
                puts(str[i]); // Prints final output.
        }
        return 0; // End of main.
}
for(i = 0; i < 1; a = OO)
{
  // ...
}

str[100]超出了数组的限制。一个包含100个元素的数组将使用0到99之间的标记。str[100]将访问“101”元素,该元素超出了范围,因此可能导致seg故障。

我对您的算法做了一个小的修改,它对我有效:

#include <stdio.h>
#include <string.h>

#define MO 109 // 109 is ASCII for "m".
#define FO 102 // 102 is ASCII for "f".
#define OO 101 // 101 is ASCII for "e" and denotes an error.

int main() // Main part of program.
{
        int i, j; // Counter integer assignment.
        int n = 100; // assignment of integer for the number of strings.
        char a; // For the m/f (manual or file) option.
        char str[100][100]; // Str is the main string to be sorted.
        char temp[100]; // Temp is to switch the values for bubble sorting.
        for(i = 0; i < 1; a = OO)
        {
                printf("To input text manually, press m. To sort a file, press f. \n");   
                // M/f option.
                scanf("%c", &a); // Gets m/f option.
                if(a == MO || a == FO) // Checks for valid input.
                {
                        i = 2; // Escape from loop with valid input.
                }
                if(a != MO && a != FO) // Invalid input.
                {
                        printf("Please insert a valid response. ");
                        i = 0; // Continue loop until a valid input is reached.
                }
        } 
        if(a == MO) // Manual insert option.
        {
                puts("Enter the number of strings to be sorted.");
                scanf("%d", &n); // Gets number of strings.
                for(i = 0; i <= n; i++)
                {
                        gets(str[i]); // Gets strings from user.
                }
        }
        if(a == FO) // File option.
        {
                char b[100]; // File address of text file to be sorted.
                FILE * f; // Text file.
                printf("Enter file path of file to be sorted.");
                scanf("%c", b); // Gets file path.
                f = fopen(b, "r"); // Opens file.
                fgets(*str, 100, f); // Coverts file into string str.
                fclose(f); // Closes file.
        }
        for(i = 0; i <= n; i++) // Begin bubble sort.
        {
                for(j = i + 1; j <= n; j++)
                {
                        if(strcmp(str[i], str[j]) > 0) // Checks alphabetical value.
                        {
                                 strcpy(temp, str[i]); // Switch two strings.
                                 strcpy(str[i], str[j]);
                                 strcpy(str[j], temp);
                        }
                }
        }
        printf("The sorted string:");
        for(i = 0; i <= n; i++)
        {
                puts(str[i]); // Prints final output.
        }
        return 0; // End of main.
}
#include <stdio.h>
#include <string.h>

#define MO 109 // 109 is ASCII for "m".
#define FO 102 // 102 is ASCII for "f".
#define OO 101 // 101 is ASCII for "e" and denotes an error.

int main() // Main part of program.
{
    int i, j; // Counter integer assignment.
    int n = 100; // assignment of integer for the number of strings.
    char a; // For the m/f (manual or file) option.
    char str[100][100]; // Str is the main string to be sorted.
    char temp[100]; // Temp is to switch the values for bubble sorting.
    a = OO;
    i=0;
    while(i < 1)
    {
        printf("To input text manually, press m. To sort a file, press f. \n");
        // M/f option.
        scanf("%c", &a); // Gets m/f option.
        if(a == MO || a == FO) // Checks for valid input.
        {
            i = 2; // Escape from loop with valid input.
        }
        if(a != MO && a != FO) // Invalid input.
        {
            printf("Please insert a valid response. ");
            i = 0; // Continue loop until a valid input is reached.
        }
    }
    if(a == MO) // Manual insert option.
    {
        puts("Enter the number of strings to be sorted.");
        scanf("%d", &n); // Gets number of strings.
        for(i = 0; i <= n; i++)
        {
            gets(str[i]); // Gets strings from user.
        }
    }
    if(a == FO) // File option.
    {
        char b[100]; // File address of text file to be sorted.
        FILE * f; // Text file.
        printf("Enter file path of file to be sorted.");
        scanf("%c", b); // Gets file path.
        f = fopen(b, "r"); // Opens file.
        fgets(*str, 100, f); // Coverts file into string str.
        fclose(f); // Closes file.
    }
    for(i = 0; i < n; i++) // Begin bubble sort.
    {
        for(j = i + 1; j <= n; j++)
        {
            if(strcmp(str[i], str[j]) > 0) // Checks alphabetical value.
            {
                strcpy(temp, str[i]); // Switch two strings.
                strcpy(str[i], str[j]);
                strcpy(str[j], temp);
            }
        }
    }
    printf("The sorted string:");
    for(i = 0; i < n; i++)
    {
        puts(str[i]); // Prints final output.
    }
    return 0; // End of main.
}
#包括
#包括
#定义MO 109//109是表示“m”的ASCII码。
#定义FO 102//102是表示“f”的ASCII码。
#define OO 101//101是表示“e”的ASCII码,表示错误。
int main()//程序的主要部分。
{
int i,j;//计数器整数赋值。
int n=100;//为字符串数指定整数。
char a;//用于m/f(手动或文件)选项。
char str[100][100];//str是要排序的主字符串。
char temp[100];//temp用于切换气泡排序的值。
a=OO;
i=0;
而(i<1)
{
printf(“要手动输入文本,请按m键。要对文件排序,请按f键。\n”);
//M/f选项。
scanf(“%c”,&a);//获取m/f选项。
if(a==MO | | a==FO)//检查有效输入。
{
i=2;//使用有效输入从循环中转义。
}
如果(a!=MO&&a!=FO)//输入无效。
{
printf(“请插入有效回复”);
i=0;//继续循环,直到达到有效输入。
}
}
if(a==MO)//手动插入选项。
{
puts(“输入要排序的字符串数”);
scanf(“%d”,&n);//获取字符串数。

对于(i=0;i),该代码有很多错误。你能告诉我它有什么错误吗?我不太擅长这类事情。@user2973526对于初学者,使用
'm'
'f'
'e'
而不是那些#defines。Hint1:如果你将数组定义为[100],那么它的元素就是[0],[99]无论出于什么原因,都不要使用gets。请改用FGET。