Separate string into multiple lines

Posted By: tomna1993

Separate string into multiple lines - 06/08/21 23:17

Hi,
I have a long string which I use for formatting data I want to save. It looks like this:
Code
string LogDataFormat = "%s,%i,%s,%i,%.15f,%.15f,%i,%i,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%i,%.2f,%.2f,%.2f,%.2f,%.2f,%i,%.2f,%i,%.2f,%.2f,%.2f,%i,%.2f,%.2f,%i%i,%i,%i%i,%.2f\n";


I want to separate it to multiple rows to be much more readable, if it is possible use comment for each row. I already tried one solution which works in C but the formatting of my data in log file was exactly the same looking as the multirow string:

Code
string LogDataFormat = "%s,\
%i,\
%s,\
%i,\
%.15f,%.15f,\
%i,%i,%.2f,%.2f,\
%.2f,%.2f,\
%.2f,%.2f,\
%i,%.2f,\
%.2f,%.2f,\
%.2f,%.2f,\
%i,%.2f,\
%i,%.2f,\
%.2f,%.2f,\
%i,%.2f,\
%.2f,\
%i%i,%i,%i%i,\
%.2f\n";


Is there any solution to format that string much more readable in Lite-c?
Posted By: Grat

Re: Separate string into multiple lines - 06/09/21 07:27

mayby:
Code
string LogDataFormat = "%s,
%i,
%s,
%i,
..
%.2f";
Posted By: tomna1993

Re: Separate string into multiple lines - 06/09/21 07:56

Already tried it, does not work.
Posted By: Grant

Re: Separate string into multiple lines - 06/09/21 09:27

Replace the string type with char* in your second code example.
Posted By: tomna1993

Re: Separate string into multiple lines - 06/09/21 10:01

It still gives the same result.

I think I wrongly described my problem. The code I included works nice in a manner of code readability but in my log file it looks like on the attached picture. I want to log each trade in separate rows and also print a header for my file in one row like a csv document (see picture). The problem is, that the code I included in the first post puts each word in new row and puts a "\r" characters after the comma.

Attached picture Annotation 2021-06-09 115945.png
Attached picture Annotation 2021-06-09 120821.png
Posted By: Grant

Re: Separate string into multiple lines - 06/09/21 14:05

OK, can you try this?

string LogDataFormat = "%s,\n\
%i,\n\
%s,\n\
%i,\n\
%.15f,%.15f,\n\
%i,%i,%.2f,%.2f,\n\
%.2f,%.2f,\n\
%.2f,%.2f,\n\
%i,%.2f,\n\
%.2f,%.2f,\n\
%.2f,%.2f,\n\
%i,%.2f,\n\
%i,%.2f,\n\
%.2f,%.2f,\n\
%i,%.2f,\n\
%.2f,\n\
%i%i,%i,%i%i,\n\
%.2f\n";

(or else LogDataFormat as char*)
Posted By: tomna1993

Re: Separate string into multiple lines - 06/09/21 20:55

Same result.
Posted By: Grant

Re: Separate string into multiple lines - 06/09/21 21:50

Sorry to hear. I've tried the following simple example that works fine, but I have no clue on how you've implemented this LogDataFormat string, so can you show me your code?

char* LogDataFormat = "Trade 1.\n\Trade 2.\n\Trade 3.";
print(TO_CSV, LogDataFormat);
Posted By: tomna1993

Re: Separate string into multiple lines - 06/10/21 08:51

I use file append to add information to the txt file.
Code
file_append(LogDestination, LogHeader, 0)
file_append(LogDestination, LogData, 0)

And use the formatting to save the informations formatted to LogData String.
Code
strcat(LogData, strf(LogDataFormat, (string)Log_Asset,
(int)TradeID, 
(string)Log_TradeDirection, 
(int)Log_TradeTimeFrame, 
(var)TradeDate, 
(var)TradeExitDate, 
(int)TradeBars, 
(int)Lots, 
(var)TradePriceOpen, 
));
Posted By: jcl

Re: Separate string into multiple lines - 06/14/21 07:13

If I understand it right, you want the format string to go over several lines in your source code, but behave as if it were a single line.

I think this won't work in lite-C. You can do it with #defines, but not with strings.
Posted By: Grant

Re: Separate string into multiple lines - 06/14/21 09:45

It's not the most elegant solution, but you could try one of the following substring methods when your string has a fixed length,

https://www.programmingsimplified.com/c/source-code/c-substring
Posted By: tomna1993

Re: Separate string into multiple lines - 06/15/21 09:51

You understand it right!

Grant: yeah, the problem is that the length is not fixed. I hoped this to be easier (like in C) but doesn't matter, I fight with it. That is the easiest part of the program, the debugging both visually and by values are more pain in the ass.

Thanks for both of your help and have a nice day!
Posted By: Grant

Re: Separate string into multiple lines - 06/15/21 10:57

Within Excel I use the LEN() and MID() functions for a similar scenario with non-fixed string lengths, but in your case I believe it would be much easier to work with an array.
Best of luck and have a nice day as well.
Posted By: tomna1993

Re: Separate string into multiple lines - 06/16/21 12:42

Later on I will check your idea! Thanks!
© 2024 lite-C Forums