为什么这段代码不能继续?

时间:2013-09-16 01:13:11

标签: c save cycle scanf

我想知道为什么这段代码不是从file.txt中存储的单词开始的。

代码编译但无法找到错误

我想知道为什么函数“scanf”无法捕获字符并将它们传递给变量

感谢您的回复

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int e = 0;
    int f = 0;
    int g = 0;
    int h = 0;

    char var[] = { '1', 'A', '2', 'B', '3', 'C', '4', 'D', '5', 'E', '6' };
    char caracteres[9];
    char caracteres_txt[9];


    FILE *fp, *sesion;
    sesion = fopen("Save_Sesion.txt", "r");

    if (sesion == NULL)
    {
        printf("\nNo sesion saved.\n");
    }
    else
    {
        printf("\nlast word was:\n\n");
        fgets(caracteres, 9, sesion);
        printf("%s\n", caracteres);
        fscanf(sesion, "%s", caracteres_txt);
    }


    if (caracteres_txt[1] == var[0])
    {
        a = 0;
    }
    else
    {
        if (caracteres_txt[1] == var[1])
        {
            a = 1;
        }
        else
        {
            if (caracteres_txt[1] == var[2])
            {
                a = 2;
            }
            else
            {
                if (caracteres_txt[1] == var[3])
                {
                    a = 3;
                }
                else
                {
                    if (caracteres_txt[1] == var[4])
                    {
                        a = 4;
                    }
                    else
                    {
                        if (caracteres_txt[1] == var[5])
                        {
                            a = 5;
                        }
                        else
                        {
                            if (caracteres_txt[1] == var[6])
                            {
                                a = 6;
                            }
                            else
                            {
                                if (caracteres_txt[1] == var[7])
                                {
                                    a = 7;
                                }
                                else
                                {
                                    if (caracteres_txt[1] == var[8])
                                    {
                                        a = 8;
                                    }
                                    else
                                    {
                                        if (caracteres_txt[1] == var[9])
                                        {
                                            a = 9;
                                        }
                                        else
                                        {
                                            if (caracteres_txt[1] == var[10])
                                            {
                                                a = 10;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }


    if (caracteres_txt[2] == var[0])
    {
        b = 0;
    }
    else
    {
        if (caracteres_txt[2] == var[1])
        {
            b = 1;
        }
        else
        {
            if (caracteres_txt[2] == var[2])
            {
                b = 2;
            }
            else
            {
                if (caracteres_txt[2] == var[3])
                {
                    b = 3;
                }
                else
                {
                    if (caracteres_txt[2] == var[4])
                    {
                        b = 4;
                    }
                    else
                    {
                        if (caracteres_txt[2] == var[5])
                        {
                            b = 5;
                        }
                        else
                        {
                            if (caracteres_txt[2] == var[6])
                            {
                                b = 6;
                            }
                            else
                            {
                                if (caracteres_txt[2] == var[7])
                                {
                                    b = 7;
                                }
                                else
                                {
                                    if (caracteres_txt[2] == var[8])
                                    {
                                        b = 8;
                                    }
                                    else
                                    {
                                        if (caracteres_txt[2] == var[9])
                                        {
                                            b = 9;
                                        }
                                        else
                                        {
                                            if (caracteres_txt[2] == var[10])
                                            {
                                                b = 10;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    if (caracteres_txt[3] == var[0])
    {
        c = 0;
    }
    else
    {
        if (caracteres_txt[3] == var[1])
        {
            c = 1;
        }
        else
        {
            if (caracteres_txt[3] == var[2])
            {
                c = 2;
            }
            else
            {
                if (caracteres_txt[3] == var[3])
                {
                    c = 3;
                }
                else
                {
                    if (caracteres_txt[3] == var[4])
                    {
                        c = 4;
                    }
                    else
                    {
                        if (caracteres_txt[3] == var[5])
                        {
                            c = 5;
                        }
                        else
                        {
                            if (caracteres_txt[3] == var[6])
                            {
                                c = 6;
                            }
                            else
                            {
                                if (caracteres_txt[3] == var[7])
                                {
                                    c = 7;
                                }
                                else
                                {
                                    if (caracteres_txt[3] == var[8])
                                    {
                                        c = 8;
                                    }
                                    else
                                    {
                                        if (caracteres_txt[3] == var[9])
                                        {
                                            c = 9;
                                        }
                                        else
                                        {
                                            if (caracteres_txt[3] == var[10])
                                            {
                                                c = 10;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    if (caracteres_txt[4] == var[0])
    {
        d = 0;
    }
    else
    {
        if (caracteres_txt[4] == var[1])
        {
            d = 1;
        }
        else
        {
            if (caracteres_txt[4] == var[2])
            {
                d = 2;
            }
            else
            {
                if (caracteres_txt[4] == var[3])
                {
                    d = 3;
                }
                else
                {
                    if (caracteres_txt[4] == var[4])
                    {
                        d = 4;
                    }
                    else
                    {
                        if (caracteres_txt[4] == var[5])
                        {
                            d = 5;
                        }
                        else
                        {
                            if (caracteres_txt[4] == var[6])
                            {
                                d = 6;
                            }
                            else
                            {
                                if (caracteres_txt[4] == var[7])
                                {
                                    d = 7;
                                }
                                else
                                {
                                    if (caracteres_txt[4] == var[8])
                                    {
                                        d = 8;
                                    }
                                    else
                                    {
                                        if (caracteres_txt[4] == var[9])
                                        {
                                            d = 9;
                                        }
                                        else
                                        {
                                            if (caracteres_txt[4] == var[10])
                                            {
                                                d = 10;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }


    printf("\nPress Enter to continue\n");
    getchar();


    while (a <= 10)
    {
        while (b <= 10)
        {
            while (c <= 10)
            {
                while (d <= 10)
                {
                    while (e <= 10)
                    {
                        while (f <= 10)
                        {
                            while (g <= 10)
                            {
                                while (h <= 10)
                                {
                                    printf("%c%c%c%c%c%c%c%c\n", var[a],
                                           var[b], var[c], var[d], var[e],
                                           var[f], var[g], var[h]);
                                    ++h;
                                }
                                h = 0;
                                ++g;
                            }
                            g = 0;
                            ++f;
                        }
                        f = 0;
                        ++e;
                    }
                    e = 0;
                    ++d;
                    fp = fopen("Save_Sesion.txt", "w");
                    fprintf(fp, "%c%c%c%c%c%c%c%c\n", var[a], var[b], var[c],
                            var[d], var[e], var[f], var[g], var[h]);
                    fclose(fp);
                }
                d = 0;
                ++c;
            }
            c = 0;
            ++b;
        }
        b = 0;
        ++a;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我自己回答

解决方案不是一起使用fgets和fscan ....而不是if-else结构。