ZuuZuu
ZuuZuu

Reputation: 15

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. 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. 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.

I already try with getline(...) method. How can i handle \n character without raising the complexity and read file line by line.

Upvotes: 1

Views: 303

Answers (1)

John Bollinger
John Bollinger

Reputation: 180073

As you se in the Sample from text file i have to use \n Feed Line character on some of my lines.

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; it has no inherent special significance in data files, whether those files are consumed by a C program or not.

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. 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.

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 decode that sequence if you want it to output literal line feeds in its place.

I already try with getline(...) method. How can i handle \n character without raising the complexity and read file line by line.

You need to process each line read to decode the \n sequences in it. 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.

Upvotes: 2

Related Questions