简体   繁体   English

如何用c ++ win32写入文件? (VS 2010)

[英]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). 我正在使用visual studio 2010,我正在编写一个c ++ win32应用程序(不是控制台应用程序)。 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. 到目前为止我的应用程序运行正常,但当我试图在我的项目中包含iostream和fstream时,编译器给了我错误。

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

You can use 您可以使用

  1. C Libraries C库
    fopen , fputs , fwrite etc fopenfputsfwrite

  2. C++ Libraries C ++库
    ofstream , fstream etc ofstreamfstream

  3. You can use Windows APIs. 您可以使用Windows API。
    CreateFile , WriteFile etc. CreateFileWriteFile

  4. You can use POSIX like functions 您可以使用POSIX之类的函数
    _open , _creat , _write etc. _open_creat_write

Take a look at this example for how to work with file streams: 看一下这个例子,了解如何使用文件流:

How to redirect cin and cout to files? 如何将cin和cout重定向到文件?

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. Line :在示例中使用std :: getline。
Character : Use file.get() to retrieve a single character. 字符 :使用file.get()来检索单个字符。

For writing, just add std::endl to add a new line. 对于写入,只需添加std :: endl即可添加新行。 You can alternatively use the escape character '\\n' 您也可以使用转义字符'\\ n'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM