文件读错值

时间:2017-02-20 07:14:29

标签: c file-io

我不知道为什么会这样。我有一个结构可以完美地打印到文件,但是当我尝试读出该文件时,我得到的东西甚至不接近实际存在的东西。写/读部分:

int save_data(){
    char enter = 0;
    int response;
     while(true){
            printf("1. Add new player\n2. View player data\n3. Return to main");
            printf("\nSelection: ");
            scanf("%d", &response);
                if (response == 1){
                     FILE* PlayerFile = fopen("players.txt","w");
                     int i = 0;

                     for (i = 0; i < 1; i++){
                     struct player_info aPlayer = create_player();
                     fprintf(PlayerFile, "Name: %s\nLevel: %d\nStrength Mod: %d\nDexterity Mod: %d\nConstitution Mod: %d\nIntelligence Mod: %d\nWisdom Mod: %d\nCharisma Mod: %d\n", aPlayer.name, aPlayer.Level, aPlayer.Str, aPlayer.Dex, aPlayer.Con, aPlayer.Int, aPlayer.Wis, aPlayer.Cha);
                     }
                     fclose(PlayerFile);
                     return 0;
                     }

                else if (response == 2){
                    struct player_info aPlayer;{
                        char name[30];
                        int Level, Str, Dex, Con, Int, Wis, Cha;
                        };
                    FILE* PlayerFile = fopen("players.txt","r");
                    for (int i = 0; i < 1; i++){
                        struct player_info create_player;
                        fscanf(PlayerFile, "Name: %s\nLevel: %d\nStrength Mod: %d\nDexterity Mod: %d\nConstitution Mod: %d\nIntelligence Mod: %d\nWisdom Mod: %d\nCharisma Mod: %d\n", aPlayer.name, &aPlayer.Level, &aPlayer.Str, &aPlayer.Dex, &aPlayer.Con, &aPlayer.Int, &aPlayer.Wis, &aPlayer.Cha);
                        printf("\nName: %s\nLevel: %d\nStr mod: %d\nDex mod: %d\nCon mod: %d\nInt mod: %d\nWis mod: %d\nCha mod: %d\n\n", aPlayer.name, &aPlayer.Level, &aPlayer.Str, &aPlayer.Dex, &aPlayer.Con, &aPlayer.Int, &aPlayer.Wis, &aPlayer.Cha);
                    }

                    fclose(PlayerFile);
                }
                else if (response == 3){
                false;
                break;}
}
}

文件中实际存在的输入:

Name: Hamfast Drummond
Level: 9
Strength Mod: 8
Dexterity Mod: 7
Constitution Mod: 6
Intelligence Mod: 5
Wisdom Mod: 4
Charisma Mod: 3

打印上述内容时的结果:

Name: Hamfast
Level: 6422172
Str mod: 6422176
Dex mod: 6422180
Con mod: 6422184
Int mod: 6422188
Wis mod: 6422192
Cha mod: 6422196

现在,我意识到在所有变量之前,一行缺少&,但是当我将其添加到文件中的内容时,相同的错误数字和读出是正确的,在这种情况下,但它没有打印到我放入的文件。

作为一个附带问题,如果我允许二合一,我也希望打印全名而不是第一个,但我不知道如何做到这一点

4 个答案:

答案 0 :(得分:4)

printf希望使用[self performNewFetchedDataActionsWithDataArray:dataArray];格式说明符的值,而scanf需要引用

此:

%d

必须

printf("\nName: %s\nLevel: %d\nStr mod: %d\nDex mod: %d\nCon mod: %d\nInt mod: %d\nWis mod: %d\nCha mod: %d\n\n", aPlayer.name, &aPlayer.Level, &aPlayer.Str, &aPlayer.Dex, &aPlayer.Con, &aPlayer.Int, &aPlayer.Wis, &aPlayer.Cha);

换句话说,你(非常)打印这些变量的地址,而不是它们的值。

答案 1 :(得分:3)

printf("\nName: %s\nLevel: %d\nStr mod: %d\nDex mod: %d\nCon mod: %d\nInt mod: %d\nWis mod: %d\nCha mod: %d\n\n", aPlayer.name, aPlayer.Level, aPlayer.Str, aPlayer.Dex, aPlayer.Con, aPlayer.Int, aPlayer.Wis, aPlayer.Cha);

您正在打印从文件中读取的内容

答案 2 :(得分:2)

  1. 关于阅读全名:
  2. 使用规则%[^\n]\n代替%s\n。这将读取除换行符之外的所有内容,为您提供完整的名称。

    1. printf 不应使用&&用于获取给定变量的地址。因此,只有在使用 scanf 时才使用&,因为我们希望将值存储在地址中。使用 printf 时只使用变量名称,因此需要使用值。

答案 3 :(得分:0)

发布的代码包含许多问题,如其他答案和问题评论中所述。

这是一个干净地编译并实现建议更正的代码版本:

#include <stdio.h>
#include <stdbool.h>



struct player_info
{
    char name[30];
    int Level, Str, Dex, Con, Int, Wis, Cha;
};

struct player_info create_player( void );

int save_data( void );

int save_data( void )
{
    //char enter = 0;
    int response;

     while(true)
     {
            printf("1. Add new player\n"
                   "2. View player data\n"
                   "3. Return to main");
            printf("\nSelection: ");

            if( 1 != scanf("%d", &response) )
            {
                perror( "scanf failed" );
                return 1;
            }

            if (response == 1)
            {
                 FILE* PlayerFile = fopen("players.txt","r");
                 if( NULL == PlayerFile )
                 {
                     perror( "fopen for truncate and write failed" );
                     return 1;
                 }

                 int i = 0;

                 struct player_info aPlayer = create_player();
                 fprintf(PlayerFile, "Name: %s\nLevel: %d\nStrength Mod: %d\nDexterity Mod: %d\nConstitution Mod: %d\nIntelligence Mod: %d\nWisdom Mod: %d\nCharisma Mod: %d\n", aPlayer.name, aPlayer.Level, aPlayer.Str, aPlayer.Dex, aPlayer.Con, aPlayer.Int, aPlayer.Wis, aPlayer.Cha);

                 fclose(PlayerFile);
                 return 0;
            }

            else if (response == 2)
            {
                struct player_info aPlayer;

                FILE* PlayerFile = fopen("players.txt","r");
                if( NULL == PlayerFile )
                {
                    perror( "fopen for reading failed" );
                    return 1;
                }

                fscanf(PlayerFile, "Name: %s\nLevel: %d\nStrength Mod: %d\nDexterity Mod: %d\nConstitution Mod: %d\nIntelligence Mod: %d\nWisdom Mod: %d\nCharisma Mod: %d\n",
                    aPlayer.name,
                    &aPlayer.Level,
                    &aPlayer.Str,
                    &aPlayer.Dex,
                    &aPlayer.Con,
                    &aPlayer.Int,
                    &aPlayer.Wis,
                    &aPlayer.Cha);
                printf("\nName: %s\nLevel: %d\nStr mod: %d\nDex mod: %d\nCon mod: %d\nInt mod: %d\nWis mod: %d\nCha mod: %d\n\n",
                    aPlayer.name,
                    aPlayer.Level,
                    aPlayer.Str,
                    aPlayer.Dex,
                    aPlayer.Con,
                    aPlayer.Int,
                    aPlayer.Wis,
                    aPlayer.Cha);


                fclose(PlayerFile);
            }

            else if (response == 3)
            {
                //false;
                break;
            }
    }
    return 0;
}

但是,我强烈建议不要将所有字段标签文本放在文件中,因为它只会在尝试从程序中读取文件时产生问题。

请注意,程序在任何单个操作后都会返回,因此该文件不能包含单个播放器上的信息。