1

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...

  • 3
    Can you show the errors you got when including iostream and fstream ? – JBL May 30 '13 at 15:32
  • 2
    You can use iostreams and fstreams in Windows applications without any problems (I've done so many times). Can you show minimal code that demonstrates the problem(s) you encountered? – Jerry Coffin May 30 '13 at 15:33
  • iostream works but won't do anything useful unless you take steps to create a console window and hook it up to the stream. fstream works perfectly well though, I use it all the time. As a wild guess based on no information are you remembering it's in the std namespace? (So you need std::ofstream for example)? – jcoder May 30 '13 at 15:42
  • Split your question to 2 questions. Each question should have only 1 question. – user93353 May 30 '13 at 15:57
  • Please be more specific in your questions. "the compiler gave me errors" means nothing to anyone except you, because you provided no code and no details about what "error" means. Please include the code that you're using that isn't working for you, and describe the errors including the **exact text** of any error messages you're getting. "It doesn't work, but I won't show you what I tried or explain why not. Please help me." isn't quite how StackOverflow works. :-) Thanks. – Ken White May 31 '13 at 02:47

2 Answers2

2

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.

user93353
  • 13,733
  • 8
  • 60
  • 122
  • You can also use asynchronous writing through glib/glibmm, which allows continuing to use the computer without waiting for the writing process to finish (the Gedit text editor does that, and it proves to be an amazing feature) – cfa45ca55111016ee9269f0a52e771 May 30 '13 at 15:54
  • 1
    `WriteFile` also can do asynchronous writes. – user93353 May 30 '13 at 15:56
  • I guess it does. But glib/glibmm works everywhere, while WinAPI works only on one propeirtary platform, Windows, through a proprietary library, on the limited set of platforms for which Windows is available. – cfa45ca55111016ee9269f0a52e771 May 30 '13 at 16:33
  • @fr33domlover True... however, presumably, if you're writing a `Win32` application the only platforms you care about are those platforms which Windows supports. – Nik Bougalis May 30 '13 at 21:48
  • Any decent OS allows continuing to use the computer while one application is busy writing. (If you mean that the application stays responsive, that's also useful, but different) – Ben Voigt May 31 '13 at 02:38
  • Yes, I meant you can use the same app. For example, when Eclipse saves your workspace, you see a progress bar and it just occupies the screen. Instead, Eclipse could just close the GUI and do the operation in the background. But that's not a big issue: In many apps, every time you save you have to wait until the process is over. This is something that asynchronous writing easily avoids – cfa45ca55111016ee9269f0a52e771 May 31 '13 at 09:47
  • @fr33domlover - if Eclipse is saving your workspace - it can close the GUI and do the operation in another thread. You don't need async writing for that. – user93353 May 31 '13 at 10:27
  • That's what I meant, it can do that in another thread instead of having the GUI wait until the process is complete – cfa45ca55111016ee9269f0a52e771 May 31 '13 at 10:33
1

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'

Community
  • 1
  • 1