Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print text with formatting

Tags:

c

My problem is when I try to print text with a "\n", this special characters are invisible for printf and puts after echoing it to file, and reading it again.

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

int main()
{   
    FILE *f;
    char *s = (char*) malloc (2919);
    strcpy(s, "printf 'H4sIAIM4aFYCAwtJLS6JyQsBklwAMrDLnwsAAAA=' | base64 -d | gunzip > r"); //Test\nTest after decoding
    system(s);
    f = fopen("r", "r");
    fseek(f, SEEK_SET, 0);
    fread(s, 2919, 1, f);
    printf("%s", s); //puts(s); gives the same result
    fclose(f);
    system("rm r");
    free(s);
    return 0;
}

Output should look like:

Test
Test

and it looks like Test\nTest. What am I doing wrong? Learning purpose, so please be nice.

like image 747
Kris Avatar asked Dec 11 '22 19:12

Kris


1 Answers

The text you've encoded looks like this:

Test\nTest

This is a 10 character string with "\" for the fifth character and "n" for the sixth. This is different from this:

char str[]="Test\nTest";

Which is a 9 character string with a newline for the fifth character.

If you want to print a newline, your encoded string needs to contain it. Either that, or you have to parse the resulting string and perform the newline substitution manually.

like image 137
dbush Avatar answered Dec 28 '22 19:12

dbush