'\n' newline in the printf statements

Hi,
I have two pieces of code to use ‘\n’. In the code below, I can see ‘\n’ makes a new line, however,

TString test_str = "\n";
printf("test this %s test this\n", test_str.Data() );

in the code below, I cannot use ‘\n’ in my configure/env file to make a new line. Confused… :question:

TEnv * env = new TEnv("test.config");
TString test_str = env->GetValue("test.Add", "");
printf("%s \n", test_str.Data() );

In my test.config, I have:

The output is:

instead of

.

So, your test.config contains, in fact, two symbols - back slash and ‘n’?
Something like

TString s("test this\\ntest this");
printf("%s\n", s,Data());

?

Did you notice the difference between “\n” and “\n” ?

In fact, what I want to do in my configure file is to use ‘\n’ to make a new line. For example, in my configure file, I have:

test_str.Add: this is a test1 \n
+test_str.Add: this is a test2 \n
+test_str.Add: this is a test3 \n

then I use the code

TString sss = env->GetValue("test_str.Add","");
printf("%s\n", sss.Data());

to print out something like:

However, the printed is:

.

[quote=“tpochep”]So, your test.config contains, in fact, two symbols - back slash and ‘n’?
Something like

TString s("test this\\\ntest this");
printf("%s\n", s,Data());

?

Did you notice the difference between “\n” and “\\n” ?[/quote]

[quote=“zhiyiliu”]In fact, what I want to do in my configure file is to use ‘\n’ to make a new line. For example, in my configure file, I have:

test_str.Add: this is a test1 \n
+test_str.Add: this is a test2 \n
+test_str.Add: this is a test3 \n

[/quote]

Do you mean you have

test_str.Add: this is a test1
+test_str.Add: this is a test2
+test_str.Add: this is a test3

or you have a symbol ‘’ and symbol ‘n’ in your file?
’’ + ‘n’ is an escape sequence in a string or character literal (and compiler convertes them into
new-line symbol during one of compilation phases), but in your case you simply have a string with two symbols: \ and n. One more time.

printf("\n");//this is what you want
printf("\n");//this is what you have

Sorry, no. That is not what I mean.

I just uploaded two files to illustrate the problem. Please run the code and see the printout.

Thanks.
read_and_print.C (322 Bytes)
test.txt (109 Bytes)

[quote=“zhiyiliu”]Sorry, no. That is not what I mean.

I just uploaded two files to illustrate the problem. Please run the code and see the printout.

Thanks.[/quote]

Looks like my explanation was not good. One more time. \ + n is not a new line symbol, they are two symbols - back slash and small letter ‘n’. Such a combination inside string literal or character literal in your program will be converted by compiler into new-line symbol during compilation. But this is not your case, you simply have a file with two symbols \ and n. Of course, if you write a parser, which reads , looks forward for n, combines them and puts into the buffer only one symbol - new-line, you’ll have what you want.
But TEnv works differently.

Okay, I see now. The compiler does not know my configure file until the code starts to run.

:slight_smile: Better say - TEnv’s parser does not treat symbols '' and ‘n’ as a new-line and does not support escape sequences.