分段错误有很大的难以理解的错误?

时间:2013-11-14 23:21:20

标签: c string file sorting segmentation-fault

此程序用于按字母顺序对单词进行排序,可以是插入其中的单词,也可以是文本文件。它编译得很好,但是当我运行它时,我得到了大量的文本。这是一个小样本: :*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=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:
它看起来像某些文件格式或什么? 接下来是:
Segmentation fault (core dumped)
我在UCCntu上编译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.
}

Google搜索告诉我,分段错误通常意味着我指的是内存中不存在的位置。但我找不到任何关于如何解决它的建议,甚至没有具体问题。 如果有人可以帮助我,我会非常感激。感谢。

2 个答案:

答案 0 :(得分:0)

正如你的问题的评论之一所说,代码有很多问题......

例如

for(i = 0; i < 1; a = OO)
{
  // ...
}

在该循环结束时,总是a == OO,因为你在for语句的最后部分告诉它等于OO。所以你在'循环'中设置a值的所有工作都被浪费了。

但回到问题的关键点,关于seg-fault:你说的是你的程序不拥有的内存引起的。在你的情况下,可能是因为:

   int n = 100; // assignment of integer for the number of strings.
   // ...
   char str[100][100]; // Str is the main string to be sorted.
   // ...

   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.

str [100]超出了数组的限制。具有100个元素的数组将使用0到99之间的标记.str [100]将访问'101st'元素,该元素超出范围,因此可能导致seg错误。

答案 1 :(得分:0)

我对你的算法进行了一些小修改,它对我有用:

#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.
}