简体   繁体   English

C-将文件中的文本/句子读入字符串数组

[英]C - Read text/sentences from file into an array of strings

I am a beginner programmer , studying in an uni. 我是初学者程序员,在大学学习。 at the moment. 在这一刻。 My homework: write a text-based adventere game (after less than 2 months of programming studies :/ ). 我的作业:编写一个基于文本的adventere游戏(经过不到2个月的编程研究:/)。

The teacher said that the text which the game is using, should be stored in a file. 老师说,游戏使用的文字应存储在文件中。 At the beginning, the program should read in "all the text" from that file, and then, the program should use the text from strings (I guess). 首先,程序应从该文件中读取“所有文本”,然后,程序应使用字符串中的文本(我想)。

My first thought was that I will read in 1 sentence into 1 string but that just does not want to work out, because the sentences have spaces between the words ofcourse, but when I try to read it in, when the program sees a space, it stores the word in another string instead of the same one. 我首先想到的是,我会将1个句子读入1个字符串中,但是那只是不想解决,因为这些句子在课程单词之间有空格,但是当我尝试读入它时,程序看到一个空格,它将单词存储在另一个字符串中,而不是相同的字符串中。

So, because of this, for example: I have 4 lines in my txt file: Line 1: Which way should you go? 因此,因此,例如:我的txt文件中有4行:第1行:您应该走哪条路? Line 2: Forward Line 3: Left Line 4: Right 第2行:前进行3:左行4:右行

I thought that when I read the text in to 4 strings, then LINE 1 will be in the first string, LINE 2 in the second , etc. But what happened? 我以为,当我读入4个字符串的文本时,第一个字符串将位于LINE 1,第二个字符串将位于LINE 2,依此类推。 'Which' was in the first string, "way" was in the second string, etc. “ which”在第一个字符串中,“ way”在第二个字符串中,依此类推。

Sorry for the bad explanation, but I hope you guys understand what my problem is. 对不起,我的解释不好,但我希望你们明白我的问题所在。 How can I read the Line 1 into the first string ONLY? 如何仅将第1行读入第一个字符串? I thought the program will not count space as "okay, I should put the word after this space into a new string" .. 我以为程序不会将空格计算为“好,我应该在这个空格后面加上一个新字符串”。

This is my code which is causing this "error": 这是导致此“错误”的代码:

char decisions[4][200];
FILE *fp;
fp=fopen("text.txt", "r");
fscanf(fp, "%s %s %s %s", &decisions[0], &decisions[1], &decisions[2], &decisions[3]);
fclose(fp);

printf("%s\n %s\n %s\n %s\n", decisions[0], decisions[1], decisions[2], decisions[3]);

decisions[0] should have the question read in from the first line of my txt file, and the other 3 should have the 3 chosing options. 决定[0]应该从我的txt文件的第一行中读入问题,其他3个应该具有3个选择选项。 Thats what I would like to happen :/ 那就是我想发生的事情:/

To read line by line, you may use fgets . 要逐行阅读,可以使用fgets If you know that MAX bytes of each string is 200 and you have 4 lines, pass it to fgets into this loop: 如果您知道每个字符串的最大字节为200,并且有4行,请将其传递给fgets进入此循环:

int i;
for (i=0; i<3; i++)
  {
    fgets(decisions[i], 200, fp);
  }

fgets read n bytes into buffer until found new line or EOF. fgets将n个字节读入缓冲区,直到找到新行或EOF。 So, be sure that each line will have no more than 200 chars each. 因此,请确保每行最多包含200个字符。

Although you shouldn't, you can do it with fscanf . 尽管您不应该这样做,但是可以使用fscanf

Content of text.txt text.txt内容

"Whussup?" "All of the things that have not been screwn tightly to the ground" "Buh, who cares!" "You don't wanna know, mate!"
"Why?" "Because!" "Ask Joe!" "I'm a doctor, not an engineer!"

The first line can be parsed with: 第一行可以解析为:

#include <stdio.h>
#include <stdlib.h>
// ALL CHECKS OMMITTED!
int main()
{
  FILE *fp;
  char decisions[4][200];

  fp = fopen("text.txt", "r");
  fscanf(fp, "\"%199[^\"]\" \"%199[^\"]\" \"%199[^\"]\" \"%199[^\"]\"",
         decisions[0], decisions[1], decisions[2], decisions[3]);
  fclose(fp);
  printf("LINE1: %s\nLINE2: %s\nLINE3: %s\nLINE4: %s\n", decisions[0],
         decisions[1], decisions[2], decisions[3]);
  exit(EXIT_SUCCESS);
}

The trick is the \\"%199[^\\"]\\" , it takes up to 199 characters that are not a " and are between double quotes followed by a space. 诀窍是\\"%199[^\\"]\\" ,它最多包含199个不是"且位于双引号后跟一个空格的字符。 But the format of text.txt must be strict or it doesn't work! 但是text.txt的格式必须严格,否则无法使用!

But really, try to use fgets or similar, reduces the amount of headaches by a large amount! 但是实际上,尝试使用fgets或类似工具可以大大减少头痛!

明确使用换行符作为格式字符串中的分隔符

fscanf(fp, "%s[\n]%s[\n]%s[\n]%s", &decisions[0], &decisions[1], &decisions[2], &decisions[3]);

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

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