Login Page - Create Account

Support Board


Date/Time: Sat, 20 Apr 2024 08:17:21 +0000



[Programming Help] - Is it possible to use sc.WriteFile() to write in text mode?

View Count: 1710

[2018-12-03 13:18:42]
User287992 - Posts: 51
I was able to write only binary files using sc.WriteFile(), is it possible to use it to create human readable text files?

Also I have noticed that documentation of sc.OpenFile() (sc.OpenFile()) mentions constants like MODE_CREATE_AND_OPEN_FOR_READ_WRITE but only those I found were n_ACSIL::FILE_MODE_CREATE_AND_OPEN_FOR_READ_WRITE and similar.
[2018-12-03 15:53:52]
Sierra Chart Engineering - Posts: 104368
This does not make sense, you can write whatever you want using that function. Just write the data that you need including text strings. And you can also terminate the text string with a new line character.

We will update the documentation.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2018-12-03 19:27:20
[2018-12-10 10:52:46]
User287992 - Posts: 51
The following code results in file that contains
5465 7374 00
which is HEX representation of "Text" plus line ending. I would expect that it will contain the string as text.

sc.OpenFile(DebugFile, n_ACSIL::FILE_MODE_CREATE_AND_OPEN_FOR_READ_WRITE, DebugFileHandle);
unsigned int * p_BytesWritten = 0;
sc.WriteFile(DebugFileHandle, "Test", sizeof("Test"), p_BytesWritten);

[2018-12-10 14:03:02]
Sierra Chart Engineering - Posts: 104368
We have no idea what you are doing wrong or where you get that data from. Here is an example:
  if (sc.Index == sc.ArraySize - 1)
  {
    int FileHandle;
    sc.OpenFile("Testing.txt", n_ACSIL::FILE_MODE_OPEN_TO_APPEND, FileHandle);
    unsigned int BytesWritten;
    sc.WriteFile(FileHandle, "Test Line\r\n", 11, &BytesWritten);
    sc.CloseFile(FileHandle);
  }

This will write "Test Line" to the file. There simply is no problem with this whatsoever. This is point blank simple. And it does not matter that we also added the \r\n characters.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2018-12-10 14:03:48
[2018-12-10 15:30:42]
User287992 - Posts: 51
int FileHandle;
sc.OpenFile("Testing.txt", n_ACSIL::FILE_MODE_CREATE_AND_OPEN_FOR_READ_WRITE, FileHandle);
unsigned int * p_BytesWritten = 0;
sc.WriteFile(FileHandle, "Test", sizeof("Test") - 1, p_BytesWritten);
sc.CloseFile(FileHandle);

Writes Test correctly. But

sc.WriteFile(FileHandle, "Test", sizeof("Test"), p_BytesWritten);

writes 5465 7374 00.

Why is sizeof off by one byte?
[2018-12-10 15:57:13]
ganz - Posts: 1048
User287992

http://www.cplusplus.com/reference/string/string/size/

This is the number of actual bytes that conform the contents of the string, which is not necessarily equal to its storage capacity.

[2018-12-10 16:16:15]
User287992 - Posts: 51
Well if size of string doesn't fit the number of bytes written, then there should be a way to get that because it's obviously impossible to hardcode arbitrary values to use WriteFile method.

Which is then my question, how can I get real size of data to write when calling WriteFile method?
[2018-12-10 19:52:18]
Sierra Chart Engineering - Posts: 104368
This is wrong:
sizeof("Test") - 1

You need to use string length instead:
strlen("Test")
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2018-12-11 01:33:54]
ganz - Posts: 1048
User287992

bool WriteFile(const int FileHandle, const char* Buffer, const int BytesToWrite, unsigned int* p_BytesWritten);

https://en.cppreference.com/w/cpp/string/byte/strlen

std::size_t strlen( const char* str );

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account