Jennifer Forten
Jennifer Forten

Reputation: 19

How to write to a file in c++ win32? (VS 2010)

I'm using visual studio 2010 and I'm writing a c++ win32 application(not a console application). I need to know how to write to a file from this application. so far my application runs properly but when I tried to include iostream and fstream in my project the compiler gave me errors.

I need to know what libraries I should include so I can write to a file and I need to know what function I should use to do this task.

Another thing I need is how can I move from a character to another and from a line to another inside the file.

I am sorry for all the requests but I'm in desperate need to learn those things. Thank you...

Upvotes: 1

Views: 1313

Answers (2)

user93353
user93353

Reputation: 14039

You can use

  1. C Libraries
    fopen, fputs, fwrite etc

  2. C++ Libraries
    ofstream, fstream etc

  3. You can use Windows APIs.
    CreateFile, WriteFile etc.

  4. You can use POSIX like functions
    _open, _creat, _write etc.

Upvotes: 2

Nathan Hiemenz
Nathan Hiemenz

Reputation: 61

Take a look at this example for how to work with file streams:

How to redirect cin and cout to files?

I'm not sure if by move through the file, you want a get version or a set version.

For getting:
Line: Use std::getline like in the example.
Character: Use file.get() to retrieve a single character.

For writing, just add std::endl to add a new line. You can alternatively use the escape character '\n'

Upvotes: 1

Related Questions