简体   繁体   English

如何使用C从.txt文件的特定点读取整数?

[英]How to read integers from a specific point of a .txt file using C?

I need to implement a game using the C language where a functionality is to able the player to save the game to play later. 我需要使用C语言实现游戏,该游戏的功能是使玩家能够保存游戏以便以后玩。 A requirement is to save the data of the game on a .txt file in the next way: 1) the number of players in the first row; 要求是通过以下方式将游戏数据保存在.txt文件中:1)第一行的玩家人数; 2) the cards on the table in the second row, identified by player 0 and the existent cards. 2)第二行表中的纸牌,由玩家0和现有的纸牌标识。 3) the next information for each player: number of player, if it is a real player or bot (1 if it's bot, 0 if it's real), the name of the player, the points and the cards in the hand. 3)每个玩家的下一个信息:玩家人数(如果是真正的玩家或机器人)(如果是机器人则为1,如果是机器人则为0),玩家名称,点数和手中的牌。 See the example below of the format of how I need to store the data on the .txt file: 请参阅下面的示例格式,该示例说明了我需要如何在.txt文件上存储数据:

2\n
0:T;23,43,45\n
1:0;John;23:12,32,44,43\n
2:1;BOT1;34:43,54,53,45\n
EOF

Now, I don't have any idea of how to store, for example the cards of player 1 (named John) in a structure player[0].cards[7], when the player is loading the game after this .txt file. 现在,我不知道如何在播放器在此.txt文件之后加载游戏时将其存储在结构播放器[0] .cards [7]中,例如,播放器1(名为John)的纸牌。 。 How can i save the name of player 1 (John) into player[0].name and then save the name of the second player (BOT1) into player[1].name. 如何将播放器1(John)的名称保存到播放器[0] .name中,然后将第二个播放器(BOT1)的名称保存到播放器[1] .name中。

I've been trying to solve this for days, but I'm not being successful. 我已经尝试了好几天了,但是没有成功。 Can someone help me to understand how to store a specific number/string from a specific position on a .txt file back into my code? 有人可以帮助我了解如何将.txt文件上特定位置的特定数字/字符串存储回我的代码中吗? How can I make my code read the numbers, for example 12,32,44,43 and store it the proper array. 如何使我的代码读取数字,例如12,32,44,43并将其存储在适当的数组中。 How can I make my code navigate the file with all the lines, the ":", ";" 如何使代码使用所有行(“:”,“;”)浏览文件 and ",". 和“,”。

I'm not asking for the solution. 我不是在寻求解决方案。 I'm just want someone to enlight my mind so that I can find the proper way to do this. 我只是希望有人能启发我的头脑,以便我找到执行此操作的正确方法。 Any help is welcome. 欢迎任何帮助。 Thank you. 谢谢。

Assume that you use fgets to read the file line by line; 假设您使用fgets逐行读取文件; Then you can parse each line and each part of such a line separately. 然后,您可以分别解析每一行和该行的每个部分。 As far as the list of numbers is concerned, you could use strrchr to find the position of the last colon, and then use strtok to split the comma separated values one by one. 就数字列表而言,您可以使用strrchr查找最后一个冒号的位置,然后使用strtok将逗号分隔的值一一拆分。 Hope it helps. 希望能帮助到你。

int main() {

    char aSingleLine[] = "1:0;John;23:12,32,44,43\n";
    char* lastColon = strrchr(aSingleLine,':');
    if (lastColon) {
        char* numbers = lastColon+1; // one after the last colon;
        int i=0;
        char *numStr = strtok(numbers,",");
        while(numStr != NULL) {
            int numVal;
            if (sscanf(numStr,"%d",&numVal)==1) {
                printf("%dth value: %d\n", i, numVal);
            }
            else {
                printf("invalid number: %s",numStr);
            }
            i++;
            numStr = strtok(NULL,",");
        }
    }
}

Output: 输出:

0th value: 12
1th value: 32
2th value: 44
3th value: 43

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM