User avatar
ksharindam
Posts: 156
Joined: Sat Jan 09, 2016 4:16 pm

platform independent c++ string length

Fri Dec 13, 2019 8:13 am

I am creating a cross platform program which has a pdf writer.
I am writing string like this,

Code: Select all

    std::string header;
    header = "%PDF-1.4\n";
    int offset = header.size();
    
    std::ofstream stream;
    stream.open(filename);
    stream << header;
    stream.close();
in both linux and windows, the returned offset is 9. In linux number of actual written bytes is 9, but in windows it is 10.
This may be due to the different line ending types. linux uses LF and windows uses CRLF (\r\n).

How can I get the actual size of the string that is written?
Or how can I use only LF newline even in windows platform?

scotty101
Posts: 3861
Joined: Fri Jun 08, 2012 6:03 pm

Re: platform independent c++ string length

Fri Dec 13, 2019 8:33 am

Seems more like a question for stack overflow than a Raspberry Pi forum.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

LdB
Posts: 1479
Joined: Wed Dec 07, 2016 2:29 pm

Re: platform independent c++ string length

Fri Dec 13, 2019 8:53 am

tellp
http://www.cplusplus.com/reference/ostr ... eam/tellp/
In your case read the value stream.tellp();

njh
Posts: 69
Joined: Wed Aug 09, 2017 2:07 pm

Re: platform independent c++ string length

Fri Dec 13, 2019 11:35 am

It's not the question you asked, but: To write the identical PDF file on any platform (including a Pi) you should open a new file in binary mode and write the bytes required. Otherwise you will run into trouble when you write the "xref" section, or if you need any compressed or binary objects.
Last edited by njh on Fri Dec 13, 2019 11:58 am, edited 1 time in total.

User avatar
ksharindam
Posts: 156
Joined: Sat Jan 09, 2016 4:16 pm

Re: platform independent c++ string length

Fri Dec 13, 2019 11:50 am

LdB wrote:
Fri Dec 13, 2019 8:53 am
tellp
http://www.cplusplus.com/reference/ostr ... eam/tellp/
In your case read the value stream.tellp();
Tried it, but it returns 9 in both windows and linux.

User avatar
topguy
Posts: 6288
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: platform independent c++ string length

Fri Dec 13, 2019 12:16 pm

But you are saying that the file-size on Windows is 10 bytes ?

( I also think Njh had the best idea )

User avatar
ksharindam
Posts: 156
Joined: Sat Jan 09, 2016 4:16 pm

Re: platform independent c++ string length

Fri Dec 13, 2019 12:21 pm

njh wrote:
Fri Dec 13, 2019 11:35 am
It's not the question you asked, but: To write the identical PDF file on any platform (including a Pi) you should open a new file in binary mode and write the bytes required. Otherwise you will run into trouble when you write the "xref" section, or if you need any compressed or binary objects.
Thanks. It works.
I opened the file in binary mode using...

Code: Select all

stream.open(filename, std::ios::out|std::ios::binary);

Return to “C/C++”