How to read line break into C file?

Asked

Viewed 739 times

2

Hello! My question is the following, there is a file in which I own the following content: \nteste\n, but when I read it with the following code:

int main(){
    FILE* file;
    file = fopen("text.txt", "r");
    char buffer[20];

    fscanf(file, "%s", buffer);

    printf("%s", buffer);
    fclose(file);

    return 0;
}

No line breaks and is displayed exactly the file content (\nteste\n), considers n to be the bar and the letter n separate. Is there any way to make it "interpret" the line break?
Thanks!

  • Your file does not contain line breaks, but the literal " n". Why?

  • Because I want to use the file to feed variables, each line being a variable, you know? And they have content and size different from each other, if I use the literal line break in the file this method becomes unviable

  • 1

    In C is the fgets() function that reads a file line by line. fscanf() is to do Parsing.

2 answers

1


Unfortunately there is no function in the standard library capable of interpreting literal strings for its equivalent escape sequence.

An alternative would be to implement a function capable of doing this, see:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void converter( char * saida, const char * entrada )
{
    int estado = 0;
    *saida = '\0';

    while(*entrada)
    {
        if(!estado)
        {
            if(*entrada == '\\')
                estado = 1;
            else
                strncat(saida, entrada, 1);
        }
        else
        {
            switch(*entrada)
            {
                case 'a'  : strcat(saida, "\a"); break;
                case 'b'  : strcat(saida, "\b"); break;
                case 'n'  : strcat(saida, "\n"); break;
                case 't'  : strcat(saida, "\t"); break;
                case 'r'  : strcat(saida, "\r"); break;
                case 'f'  : strcat(saida, "\f"); break;
                case '\"' : strcat(saida, "\""); break;
                case '\'' : strcat(saida, "\'"); break;
                case '\\' : strcat(saida, "\\"); break;
                default: break;
            }
            estado = 0;
        }
        entrada++;
    }
}

int main(void)
{
    FILE* file;
    file = fopen("text.txt", "r");
    char buffer[100];
    char bufaux[100];

    fscanf(file, "%s", buffer);
    converter(bufaux, buffer);

    printf("%s", bufaux);
    fclose(file);

    return 0;
}

For example, if the contents of your file are something like:

foo\nbar\nbaz\n

The exit would be:

foo
bar
baz

See working on Repl.it

0

Hello! First of all, thank you to everyone who was willing to help, and forgive the delay in answering the topic. I’ve come up with a perfect solution to my case, considering Lacobus' code.
Text file (example):

 Digite os dados abaixo para criar sua conta.\n

The code then traverses the entire sentence, and the bar " " is replaced by a blank space, and "n" is replaced by character 10 (line break), something like that:

for(i = 0;i<array_size;i++) {
        // 92 = \, 110 = n, 32 = space, 10 = \n
        for(j = 0;j<strlen(gameTextContent[i]);j++) {
            if(gameTextContent[i][j] == 92 && gameTextContent[i][j+1] == 110) {
                gameTextContent[i][j] = 32;
                gameTextContent[i][j+1] = 10;
            }
        }
}

If it’s a little strange at first glance, I’m using a multidimensional array:

gameTextContent[array_size][content_size]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.