简体   繁体   English

从C中的文本文件中读取\\ n作为真正的换行符

[英]Reading \n as really Feed Line character from text file in C

I'm trying to read text file with C. Text file is a simple language file which works in embeded device and EACH LINE of file has a ENUM on code side. 我试图读取C.文本文件中的文本文件是一种简单的语言文件,它工作在嵌入式设备和文件的每一行对代码侧的ENUM。 Here is a simple part of my file : 这是我文件的一个简单部分:

SAMPLE FROM TEXT FILE : 来自文本文件的示例:

 OPERATION SUCCESS!
 OPERATION FAILED!\nRETRY COUNT : %d

ENUM : 枚举:

   typedef enum
   {
    ...
           MESSAGE_VALID_OP,
           MESSAGE_INVALID_OP_WITH_RETRY_COUNT
    ...
   }

Load Strings : 加载字符串:

typedef struct
{
    char *str;
} Message;

int iTotalMessageCount = 1012;

void vLoadLanguageStrings()
{
    FILE *xStringList;
    char * tmp_line_message[256];
    size_t len = 0;
    ssize_t read;
    int message_index = 0;

    xStringList = fopen("/home/change/strings.bin", "r");

    if (xStringList == NULL)
        exit(EXIT_FAILURE);

    mMessages = (Message *) malloc(iTotalMessageCount * sizeof(Message));

    while ((read = fgets(tmp_line_message, 256, xStringList)) != -1 && message_index < iTotalMessageCount)
        {
            mMessages[message_index].str = (char *) malloc(strlen(tmp_line_message));
            memcpy(mMessages[message_index].str, tmp_line_message, strlen(tmp_line_message) -1);
            message_index++;
    }

    fclose(xStringList);
}

As you se in the Sample from text file i have to use \\n Feed Line character on some of my lines. 正如您在文本文件示例中所使用的那样,我必须在某些行上使用\\ n Feed Line字符。 After all, i read file successfuly. 毕竟,我成功读取了文件。 But if i try to call my text which has feed line \\n, feed line character just printed on device screen as \\ & n characters. 但是,如果我尝试调用带有换行符\\ n的文本,则换行符仅以\\&n字符形式显示在设备屏幕上。

I already try with getline(...) method. 我已经尝试使用getline(...)方法。 How can i handle \\n character without raising the complexity and read file line by line. 如何处理\\ n字符而不增加复杂性并逐行读取文件。

As you se in the Sample from text file i have to use \\n Feed Line character on some of my lines. 正如您在文本文件示例中所使用的那样,我必须在某些行上使用\\ n Feed Line字符。

No, I don't see that. 不,我没有看到。 Or at least, I don't see you doing that. 至少,我看不到你那样做 The two-character sequence \\n is significant primarily to the C compiler ; 两个字符的序列\\n主要对C 编译器有意义; it has no inherent special significance in data files, whether those files are consumed by a C program or not. 无论数据文件是否由C程序使用,它在数据文件中都没有固有的特殊意义。

Indeed, if the system recognizes line feeds as line terminators, then by definition, it is impossible to embed a literal line feed in a physical line. 确实,如果系统将换行识别为换行符,那么根据定义,就不可能在物理行中嵌入文字换行。 What it looks like you are trying to do is to encode line feeds as the "\\n" character sequence. 您看起来想要做的就是将换行编码为“ \\ n”字符序列。 That's fine, but it's quite a different thing from embedding a line feed character itself. 很好,但是与嵌入换行符本身完全不同。

But after all, i read file successfuly. 但是毕竟,我成功读取了文件。 But if i try to call my text which has feed line \\n, feed line character just printed on device screen as \\ & n characters. 但是,如果我尝试调用带有换行符\\ n的文本,则换行符仅以\\&n字符形式显示在设备屏幕上。

Of course. 当然。 Those are the characters you read in ( not a line feed), so if you write them back out then you reproduce them. 这些是您读入的字符( 不是换行符),因此,如果您将它们写回去,则可以重现它们。 If you are encoding line feeds via that character sequence, then your program must de code that sequence if you want it to output literal line feeds in its place. 如果您在编码线过该字符序列饲料,然后如果你想让它输出你的程序必须取消代码序列在其位文字换行。

I already try with getline(...) method. 我已经尝试使用getline(...)方法。 How can i handle \\n character without raising the complexity and read file line by line. 如何处理\\ n字符而不增加复杂性并逐行读取文件。

You need to process each line read to decode the \\n sequences in it. 您需要处理读取的每一行,以解码其中的\\ n序列。 I would write a function for that. 我会为此编写一个函数。 Any way around, however, your program will be more complex, because the current version simply doesn't do all the things it needs to do. 但是,无论如何,您的程序都会更复杂,因为当前版本根本无法完成它需要做的所有事情。

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

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