Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: OutputFileSteam  (Read 2824 times)

0 Members and 1 Guest are viewing this topic.

intrinsic

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
OutputFileSteam
« on: August 21, 2016, 07:11:20 am »
I'm currently reading about <fstream> and how to use it. I'm finding it difficult to write an output file in binary using the fstream::write() function. This is due to the fact that it only accepts a char* type instead of void* as it's first param. I know that SFML has an InputFileStream class, but I would like to see a OutputFileStream class as well.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: OutputFileSteam
« Reply #1 on: August 21, 2016, 08:53:57 am »
As far as I'm aware, the binary mode in C & C++ is only "useful" on Windows. (The doc reads: "POSIX implementations do not distinguish between text and binary streams".) But this ain't your issue right now.

You have to be aware that SFML does not intend to provide replacement for S(T)L features; on the contrary, when switching to C++11 (or any more recent standard), SFML will drop some of its features in favour of S(T)L implementations. So if one wants something to be added to SFML, one would have to build a strong use case scenario.  ;)

Now, yes, the std::fstream::write operation (actually inherited from std::basic_ostream) only takes C strings as arguments but that's not an issue at all. First of all, using void* is really not recommended as a general practice in C++. Secondly, you have the std::basic_ostream::operator<< family of operators at your disposal for printing more complex types, like you would do it with std::cout, for example.
SFML / OS X developer

intrinsic

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: OutputFileSteam
« Reply #2 on: August 21, 2016, 04:00:21 pm »
Oh great! I can use the insertion/extractor operators. This is way more help than what I got on stackoverflow when that wasn't the reason for my post.

Thank you!
<3 this community.

intrinsic

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: OutputFileSteam
« Reply #3 on: August 22, 2016, 01:16:07 am »
Well thanks to your help, I figured it out. Thank you so much. I have something to work with now.
Here is what I came up with.

#include <fstream>
#include <iostream>


int main()
{
        int testVariable = 72;
        int testVariable2 = 0;

        std::cout << testVariable << std::endl;

        std::ofstream outputFile;
        outputFile.open("C:/binary.txt", std::ios::out | std::ios::binary);
        outputFile << testVariable;
        outputFile.close();

        std::ifstream inputFile;
        inputFile.open("C:/binary.txt", std::ios::in | std::ios::binary);
        inputFile >> testVariable2;
        inputFile.close();

        std::cout << testVariable2 << std::endl;
       
        system("pause");
    return 0;
}

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: OutputFileSteam
« Reply #4 on: August 23, 2016, 02:52:45 pm »
Propably offtop (a bit), sorry.  :-[

Do not write monoplatform code if you don't have to do so.
"C:/binary.txt"
 
This will work only on Windows, use relative paths or typedefs to specify root directory.

std::ios::binary
 
Linux (and propably any Unix-like system) doesn't really support binary mode. Of course code will work, but files will contain normal, readable data.

intrinsic

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: OutputFileSteam
« Reply #5 on: August 24, 2016, 12:21:02 am »
Propably offtop (a bit), sorry.  :-[

Do not write monoplatform code if you don't have to do so.
"C:/binary.txt"
 
This will work only on Windows, use relative paths or typedefs to specify root directory.

std::ios::binary
 
Linux (and propably any Unix-like system) doesn't really support binary mode. Of course code will work, but files will contain normal, readable data.

Although writing code that would be compatible for other OS' is a little too advanced for me right now, I'll keep that in mind and learn it in the future.

Here's a pastebin of my saving and loading code:
http://pastebin.com/gedzPuJS

Thanks for all the help and suggestions everyone!