Files...creating if one doesn't exist.

Posted by: acid45

Files...creating if one doesn't exist. - 03/02/04 02:08 PM

Hey guys. What I'm trying to do is to a file based on the username. ie:
fstream saveout(filename);
where filename is the username + .txt0
0 being the end marker of a character array.

It's a character array so adding .txt isn't hard and if the file already exists it will write to that file;

I can't seem to find a way to create a file of filename name if(!saveout); I've checked a couple sites and found that this "can't" be done...ha!

It's prety sad when you start using; instead of . :p I just caught myself on the last sentence. Any ideas would be great. Sorry for the technical indifficulty.
Posted by: acid45

Re: Files...creating if one doesn't exist. - 03/02/04 03:26 PM

-_-*
Code:
nfbuf = saveout.rdbuf();
nfbuf->open(filename, fstream::out);
saveout<< name << endl;
Posted by: acid45

Re: Files...creating if one doesn't exist. - 03/02/04 04:22 PM

I can output a character array using this method but there are also integer properties to the class I'm trying to write to file. I'm trying to use filebuf *nfbuf; to output a name and a bunch of stats and save values to file which are integer and can't think of a way to convert an int into a *char other than String... any ideas would be greatly appreciated.
Posted by: SilentRage

Re: Files...creating if one doesn't exist. - 03/03/04 03:08 PM

Somebody pointed me to this thread expecting me to answer it. But you're asking a C++ question, and I only deal with C. Will you be satisfied with the C solution? Basically all you want to do is create a file if it doesn't already exist? Also tell me if you will be using this on windows or linux or both?

As for converting ints to strings you can do this:

int intv = 123;
char strv[4];

itoa(intv, strv, 10);

OR

sprintf(strv, "%u", intv);
Posted by: acid45

Re: Files...creating if one doesn't exist. - 03/04/04 01:02 PM

We're using windows as a common operating system between team members.

Well, I'm failry new to c/c++ and still haven't goten the grasp of how to use c and c++ inter-changably. I found how to use filebuf to create a file if it doesn't exist but this method doesn't make it easy to write to file with any format other than char*, which is why I wanted to know how to convert an int into a character array. I think if I can use itoa() then I will be well on my way to working in the map from the C version.

We had to chagne almost everything anyway because the system we had was usable for one map only and it would have taken a lot of coding to add new maps. The locig and math can be copied and pasted so we shouldn't have any problems converting most of the program. We just wanted to get the c - c++ differances out of the way.

Thanks for the input Rage.
Posted by: SilentRage

Re: Files...creating if one doesn't exist. - 03/04/04 02:22 PM

so I take it you're not interested in the C solution?
Posted by: acid45

Re: Files...creating if one doesn't exist. - 03/12/04 09:21 PM

I have the C solution thanks anyway, we're still trying to work it out theough we had it working with strings as I said, we should be able to get it done before the end of this week. It's a side porject me and another programmer have and are working on in between jobs and classes and shit. Going out into the workforce next week . Building a database for reporting and marketing info. Building a distributed system to make a user able to purchase access codes to the internet and then use the cod or x ammount of minutes and then have to purchase a new code. We also have to spiff up the website.