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
Your file does not contain line breaks, but the literal " n". Why?
– bfavaretto
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
– Thiago
In C is the fgets() function that reads a file line by line. fscanf() is to do Parsing.
– epx