Hi, I'm new to the forum and need some help writing my first 'complicated' program.

I have trouble writing to an existing file.

I want to write to an Excel file I already created and edited exclusively for the program I wrote, but when I write the data it destroys all the editing of the file and I don't know what's wrong or if it's even possible to do what I'm trying to.

Im pretty sure all the writing to the file goes in blank spaces, so I don't know what is the problem.

I'm following the commands (the only one I know to write to files) described below:

fout.open("Character.xls");

// loads of output text

fout.close();

All my program works fine except for the writing to the Excel file, I don't get any errors.

Well, I think that's it

Hope somebody can help me soon.

Recommended Answers

All 11 Replies

Data type is usually the most important thing to include -- how is fout declared? If it is an ofstream, you are using the default mode, which I believe means to truncate an existing file. I think you would need to use an fstream opened for read/write ("r+" in fopen-speak), perhaps even in binary mode as well.

http://www.dinkumware.com/htm_cpl/fstream.html#basic_filebuf::open

Okay, I tried something from the link you posted, but I just got more confused :lol:

I'll post you what im using now:

filebuf iobuf;

	iobuf.open("Character.xls",ios_base::in | ios_base::out);

\\ loads of input text

        iobuf.close();

<< moderator edit: added [code][/code] tags >>

I'm still using the fout because the compiler tells me its necessary to input the data, but now the program wont return me anything, it leaves the file in blank, just like it was. So:
1. Do I have to create another way of calling a file output?
2. Do I need something else than #include <fstream>?

I've worked in this program four days in a row, just stopping to eat, sleep and other neccessities, and now that I've reached the end it just seems I worked for nothing. Its frustating, please help :cry:

Im kind of new to programming, only the past 2 weeks with tutorials all by myself, if you can explain something to me I'm willing to learn, just go easy ;)

Okay, I tried something from the link you posted, but I just got more confused :lol:

Yeah, me too.

I'll post you what im using now:

filebuf iobuf;

	iobuf.open("Character.xls",ios_base::in | ios_base::out);

\\ loads of input text

        iobuf.close();

<< moderator edit: added [code][/code] tags >>

:?:

I'm still using the fout because the compiler tells me its necessary to input the data, but now the program wont return me anything, it leaves the file in blank, just like it was. So:
1. Do I have to create another way of calling a file output?
2. Do I need something else than #include <fstream>?

I asked what type it was. You still haven't mentioned it. What is fout?

The page I linked to was to help explain the open modes.

Also, this

// loads of output text

doesn't tell me a whole lot about what you expect to accomplish.

I'm really sorry, I forgot your question.

Yes, fout is ofstream.

Theres is a lot of output text, so I didn't want to get anyone bored, well, if you're asking for it, here it is, my complete code.

I didn't attach the header file because is way too messy, after all, Im noob :cheesy: , but I think this holds everything you need to detect a problem.

Thank you for your time.

I didn't attach the header file because is way too messy, after all, Im noob , but I think this holds everything you need to detect a problem.

Without the header, I can't compile. ;)

ofstream fout;
// ...
filebuf iobuf;
iobuf.open("Character.xls",ios_base::in | ios_base::out);

I think you want fout to be an fstream (not an ofstream) and open it in the proper mode.

fstream fout;
// ...
fout.open("Character.xls",ios_base::in | ios_base::out);

And it really seems that you file is a tab-separated-values (.tsv) file, not an .xls.

Now it writes at the top of the file, destroys all my editing done in the file and returns funny characters after all of the data was input. It looks very weird, and funny at the same time. Well, this happen after I did what you told me too in your last reply.

I'll add the header file, too bad I can't attach the Excel file, I really made it look good.

Thanks again, I really appreciate that you are responding to my messages. :D

Theres a few things you need to know about my program:

After buying the first weapon and returning to the menu of weapons you must quit or will be charged and what you already bought will be deleated, same for the second weapon.

Code in a header...! You have much to learn young grasshopper.

Sorry I haven't been that great of a help thus far. But have you considered outputting your data to a bland old .tsv file and then use Excel's wonderful import abilities to spruce things up on that end?

Thanks again,

I have some more doubts:

What is a .tsv file?
I did it but no file appears in the directory. "Character.tsv"
And how do i get excel to import it and paste it where i want to?

I tell you, now that I'm remembering, I only have a week programming, any suggestions will be happily accepted. :mrgreen:

I'll just have my program save an Excel file and then copy everything to the formatted one, still a bit of trouble, but it's the best I could do. Thanks for your help, I really appreciated it.

(If you happen to find a way, please post it)

>What is a .tsv file?

A tab separated values file, like a .csv is a comma separated values file. A text file with field delimiters.

>And how do i get excel to import it and paste it where i want to?

Something like this.

[IMG]http://img277.echo.cx/img277/5011/excel11cp.th.jpg[/IMG] [IMG]http://img277.echo.cx/img277/2338/excel20hk.th.jpg[/IMG] [IMG]http://img277.echo.cx/img277/239/excel35gt.th.jpg[/IMG] [IMG]http://img277.echo.cx/img277/5059/excel44zc.th.jpg[/IMG] [IMG]http://img277.echo.cx/img277/4274/excel54ym.th.jpg[/IMG]
Before you plop it in, you can set up the properties.
[IMG]http://img277.echo.cx/img277/7555/excel64ub.jpg[/IMG] [IMG]http://img277.echo.cx/img277/7793/excel71wx.th.jpg[/IMG]
Then you can update the file from your source code and see the updates via Excel doing a lot of the dirty work.
[IMG]http://img277.echo.cx/img277/2043/excel80ac.th.jpg[/IMG]

Thank you for your help, it was really useful.

:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.