How do I save a text file?

Does anyone know? I'm using Win32 btw.
Using Winapi:
CreateFile() function:
http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
WriteFile()
http://msdn.microsoft.com/en-us/library/aa365747(v=VS.85).aspx
ReadFile()
http://msdn.microsoft.com/en-us/library/aa365467(v=VS.85).aspx


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

int main()
{
char buffer[]="Write this text to file";
DWORD dwWritten; // number of bytes written to file
HANDLE hFile;  file handle

hFile=CreateFile("C:\\file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if(hFile==INVALID_HANDLE_VALUE)
  {
    MessageBox(0,"Could not create/open a file","Error",16);
    return 1;
  }
WriteFile(hFile,buffer,sizeof(buffer),&dwWritten,0);
CloseHandle(hFile);


return 0;
}
Last edited on
@Bazzy: Yeah, I'm pretty sure that's not what I asked for help on. I'm not doing Console programming, you should read the full post before jumping to the conclusion I am just another seeking the same thing as the rest.

@Null: Ah! Thanks, that's really helpful :) You are awesome!
fstream is not for the console...
YOU should read before jumping to the conclusion
That's what you get for trying to be a nice guy and help someone, Bazzy! Did you not know better??? -:)
*king of all facepalms*
*breaks beak*

Comet, what were you thinking? Bazzy sent you a link to the tutorial that has /files/ in the link, not /console/. fstream, ifstream, and ofstream inherit a lot from istream and ostream, but they are objects for messing about with functions, as you would have known if you read what Bazzy sent you carefully, and/or checked the reference.

And by the way, there are a lot of people in this forum who need help with file i/o, so ironically jumping to that conclusion would get the correct result. :)

Relax, and trust us veterans here know what we're talking about.

-Albatross
Last edited on
*facepalm* I'm not doing console v.v

Please tell me where in the link she posted, is for Win32 stuff? I don't see a WriteFile();, ReadFile(); or any of that. Only Null's post was useful.

I am not here to argue with any of you over the internet, "amagawd i iz big tough guise cuz i can argur ovar eternetz".

Ironically I posted that I was needing help on Win32, I'm pretty sure GUI is different than Console. You stating that proves my point about her jumping to conclusion I am here for what everyone else was. Thanks.

You don't even use a main(); function in Win32. It's WINMAIN();, get it? WINMAIN();

I do not have time to argue with people that try to prove they're right even though they know they are wrong. Just be mature and face it, I doubt anyone here knows you in real life so there is nothing to feel embarrassed about ;)

Thank you to Null for reading my post and posting something useful. You sir, are awesome!

This thread needs to be locked before there is further trolling from our "Veterans" rofl.

@Lamlion: Trying to help someone by posting something that doesn't even help? Hell, I'll post "zomgawd hax" in a thread when someone is asking for "Hello World", yeah boy, that was helpful.
fstreams work for Win32, Mac, Unix, etc. Unlike WriteFile() etc which is Windows only.

I use fstreams all the time in my Windows GUI programs.
So I can use fstream in Win32? If so, then I apologize. Perhaps if you hadn't just posted a link and told me a little bit of how to do things since I am new and don't know, I wouldn't have been so rude. Sorry.

Can you give me an example of how to use that in a Win32 program?
Bazzy's links will help you understand the dynamics of file streams. However, in a straight Win32 API, CreateFile() and WriteFile() are easier to use. IOW, you need less knowhow to use those functions, but ultimately if your going to do file I/O you need to learn the dynamics.
CometJack
Relax. And don't be rude, because most of us are more than capable of denying your answer in the event that you're rude. Those few that aren't are very special people.

firedraco said:
fstreams work for Win32, Mac, Unix, etc. Unlike WriteFile() etc which is Windows only. I use fstreams all the time in my Windows GUI programs.

Trust this guy to know what he's talking about.

All of the C++ standard library is cross-platform. You can use the functions on any platform (even Windows, miracle of miracles). system() is one of the few functions that is not 100% cross-platform. Also, in this forum, cross-platform > system-dependent. Heck, I virtually never visit this sub-forum.

The veterans here are NOT trolls. Never, that would be a disgrace. You want trolls, go on an adventure through the mines of Moria. :)

Also, the only person with the power to lock threads in this forum as far as I know is the admin. Actually, I'm not even sure if he has that power. Even if he did, he... pretty much never shows up. 11 posts since this forum started...

-Albatross
Topic archived. No new replies allowed.