简体   繁体   English

使用'/ n'从文本文件读取

[英]Reading from text file with '/n'

Okay. 好的。 So I'm reading and storing text from a text file into a char array, this is working as intended. 因此,我正在从文本文件中读取文本并将其存储到char数组中,这按预期工作。 However, the textfile contains numerous newline escape sequences. 但是,文本文件包含许多换行符转义序列。 The problem then is that when I print out the string array with the stored text, it ignores these newline sequences and simply prints them out as "\\n". 然后的问题是,当我用存储的文本打印出字符串数组时,它会忽略这些换行符序列,而只是将它们打印为“ \\ n”。

Here is my code: 这是我的代码:

char *strings[100];

void readAndStore(FILE *file) {
  int count = 0;
  char buffer[250];

  while(!feof(file)) {
    char *readLine = fgets(buffer, sizeof(buffer), file);
    if(readLine) {
      strings[count] = malloc(sizeof(buffer));
      strcpy(strings[count], buffer);
      ++count;
    }

  }
}

int main() {

  FILE *file1 = fopen("txts", "r");
  readAndStore(&*file1);
  printf("%s\n", strings[0]);
  printf("%s\n", strings[1]);
  return 0;
}

And the output becomes something like this: 输出结果如下所示:

Lots of text here \\n More text that should be on a new line, but isn't \\n And so \\n on and and on \\n \\ n这里有很多文字\\ n应该在换行符上显示更多文字,但不是\\ n等等\\ n和\\ n

Is there any way to make it read the "\\n" as actual newline escape sequences or do I just need to remove them from my text file and figure out some other way to space out my text? 有什么方法可以让它读取“ \\ n”作为实际的换行符转义序列,还是我只需要从文本文件中删除它们并找出其他方法来分隔文本?

No. Fact is that \\n is a special escape sequence for your compiler, which turns it into a single character literal, namely "LF" (line feed, return), having ASCII code 0x0A . 否。事实是\\n是编译器的特殊转义序列,它将其转换为具有ASCII代码0x0A的单个字符文字,即“ LF”(换行,返回)。 So, it's the compiler which gives a special meaning to that sequence. 因此,正是编译器赋予了该序列特殊的含义。

Instead, when reading from file, \\n is read as two distinct character, ASCII codes 0x5c,0x6e . 而是从文件读取时, \\n被读取为两个不同的字符,ASCII代码0x5c,0x6e

You will need to write a routine which replaces all occurences of \\\\n (the string composed by characters \\ and n, the double escape is necessary to tell the compiler not to interpret it as an escape sequence) with \\n (the single escape sequence, meaning new line). 您将需要编写一个例程,以\\n (单转义)替换所有\\\\n (由字符\\和n组成的字符串,必须使用双转义来告诉编译器不要将其解释为转义序列) \\n顺序,表示换行)。

If you only intend to replace '\\n' by the actual character, use a custom replacement function like 如果只打算用实际字符替换'\\ n',请使用自定义替换函数,例如

void replacenewlines(char * str)
{
   while(*str)
   {
      if (*str == '\\' && *(str+1) == 'n') //found \n in the string. Warning, \\n will be replaced also.
      {
         *str = '\n'; //this is one character to replace two characters
         memmove(str, str+1, strlen(str)); //So we need to move the rest of the string leftwards
               //Note memmove instead of memcpy/strcpy. Note that '\0' will be moved as well
      }
      ++str;
   }
}

This code is not tested, but the general idea must be clear. 该代码未经测试,但总体思路必须明确。 It is not the only way to replace the string, you may use your own or find some other solution. 这不是替换字符串的唯一方法,您可以使用自己的字符串或找到其他解决方案。

If you intend to replace all special characters, it might be better to lookup some existing implementation or sanitize the string and pass it as the format parameter to printf . 如果您打算替换所有特殊字符,则最好查找一些现有实现或清理字符串并将其作为format参数传递给printf As the very minimum you will need to duplicate all '%' signs in the string. 作为最低要求,您将需要复制字符串中的所有“%”符号。

Do not pass the string as the first argument of printf as is, that would cause all kinds of funny stuff. 不要传递字符串作为第一个参数printf的是,这会导致各种有趣的东西。

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

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