0

このプログラムは、単語をアルファベット順にソートすることを目的としています。このプログラムに代入された単語か、テキスト ファイルからの単語のいずれかです。コンパイルは問題なく実行できますが、実行すると大量のテキストが表示されます。これはその小さなサンプルです: :*.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)
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.
}

Google 検索によると、通常、セグメンテーション違反は、存在しないメモリ内の場所を参照していることを意味します。しかし、それを修正する方法や、具体的には何が問題なのかについてのアドバイスは見つかりませんでした。誰かがこれで私を助けることができれば、私は本当に感謝しています. ありがとう。

4

2 に答える 2

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.
}
于 2013-11-15T04:48:35.047 に答える