简体   繁体   English

读取C txt文件时出现问题

[英]Problems reading C txt file

I'm making a C program and I need it to save some integers and strings when the user closes the program. 我正在制作一个C程序,当用户关闭该程序时,我需要它来保存一些整数和字符串。 I managed to do it successfully. 我成功地做到了。 The txt file is created and everything is in correctly written, the problem is, when I try to read it on the compiler it's not working properly. txt文件已创建并且所有内容均已正确编写,问题是,当我尝试在编译器上读取它时,它无法正常工作。 My code is: To Write: 我的代码是:编写:

saveFile = fopen("saveFileP.txt", "w");
  for (int i=0; i<numberofPeople; i++) {
    fputs(people[i].firstName, saveFile);
    fputs(" ", saveFile);
    fputs(people[i].lastName, saveFile);
    fputs(" ", saveFile);
    fprintf(saveFile, "%d ", people[i].cash);
    fprintf(saveFile, "%d ", people[i].position);
    fprintf(saveFile, "%d ", people[i].inGame);
}

To Read: 读书:

saveFile = fopen("saveGameP.txt", "r");
for (int i=0; i<numberOfPeople; i++) {
    fgets(people[i].firstName, 20, saveFile);
    fgets(people[i].lastName, 20, saveFile);
    fscanf(saveFile, "%d", &people[i].cash);
    fscanf(saveFile, "%d", &people[i].position);
    fscanf(saveFile, "%d", &people[i].inGame);
}

And then I ask it to print the values onto the screen and it writes wrong values, but on the txt file the values are correct. 然后我要求它将值打印到屏幕上,并写入错误的值,但是在txt文件中,这些值是正确的。 What am I doing wrong? 我究竟做错了什么? Thank you. 谢谢。

PS: I am using Xcode. PS:我正在使用Xcode。

Try 尝试

for (int i=0; i<numberOfPeople; i++) {
    fscanf(saveFile,"%s %s",people[i].firstName,people[i].lastName);

    fscanf(saveFile, "%d", &people[i].cash);
    fscanf(saveFile, "%d", &people[i].position);
    fscanf(saveFile, "%d", &people[i].inGame);
}

I've used fscanf() because fgets() reads a whole line until a \\n character is encountered(or until a maximum of 20 characters is read) and not until it finds a space. 我使用fscanf()是因为fgets()读取整行,直到遇到\\n字符(或直到读取最多20个字符)为止,直到找到空格为止。 Since,both firstName and lastName are seperated by a space,you can use fscanf() . 由于firstNamelastName都用空格分隔,因此可以使用fscanf()

fgets() reads till newline character and it includes \\n fgets()读取直到换行符为止,并且包括\\n

In this case isn't both firstname and lastname are on the same line seperated by a space? 在这种情况下,姓氏和姓氏是否不在同一行中,并用空格分隔?

In your code you are reading the first line and fetching just first 20 chars and the rest is left unread. 在您的代码中,您正在读取第一行,并且仅读取前20个字符,其余未读取。

Then you are moving to the next line without actually reading the later part. 然后,您将移至下一行而不实际阅读下一部分。

You can fix this by doing 您可以通过执行此操作来解决此问题

  1. Read the whole line using fgets() 使用fgets()阅读整行
  2. Use strtok() and break the line to tokens using space as delimiter 使用strtok()并使用空格作为定界符将行换成令牌
  3. Then copy tokens to your arrays 然后将令牌复制到您的数组

I suggest getline ( though not specified in POSIX), Use that to read a full line ( to verify that use printf to print the line) 我建议使用getline(尽管未在POSIX中指定),使用它来读取整行(以验证使用printf来打印该行)

Then if your values are separated by spaces, for example. 然后,例如,如果您的值用空格分隔。 Use strtok function to tokenize the line that you just read. 使用strtok函数标记刚刚阅读的行。

For example: 例如:

strtok("123: A String", " ");  //returns a pointer to the tokenized string
strtok(NULL, " ");
strtok(NULL, " ");
strtok(NULL, " ");

will break the string as: "123:" "A" "String" 将字符串拆分为:“ 123:”“ A”“ String”

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

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