944,141 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3733
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 28th, 2005
0

Error trying to write to existing file

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Yoshidex is offline Offline
21 posts
since Jun 2005
Jun 28th, 2005
0

Re: Error trying to write to existing file

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/fs..._filebuf::open
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 28th, 2005
0

Re: Error trying to write to existing file

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

I'll post you what im using now:

  1. filebuf iobuf;
  2.  
  3. iobuf.open("Character.xls",ios_base::in | ios_base::out);
  4.  
  5. \\ loads of input text
  6.  
  7. 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Yoshidex is offline Offline
21 posts
since Jun 2005
Jun 28th, 2005
0

Re: Error trying to write to existing file

Quote originally posted by Yoshidex ...
Okay, I tried something from the link you posted, but I just got more confused :lol:
Yeah, me too.

Quote originally posted by Yoshidex ...
I'll post you what im using now:

  1. filebuf iobuf;
  2.  
  3. iobuf.open("Character.xls",ios_base::in | ios_base::out);
  4.  
  5. \\ loads of input text
  6.  
  7. iobuf.close();
<< moderator edit: added [code][/code] tags >>


Quote originally posted by Yoshidex ...
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
  1. // loads of output text
doesn't tell me a whole lot about what you expect to accomplish.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 28th, 2005
0

Re: Error trying to write to existing file

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.
Attached Files
File Type: cpp StarWarsRPG.cpp (4.9 KB, 13 views)
Last edited by Yoshidex; Jun 28th, 2005 at 6:57 pm. Reason: Attach file not found
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Yoshidex is offline Offline
21 posts
since Jun 2005
Jun 28th, 2005
0

Re: Error trying to write to existing file

Quote ...
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.
  1. ofstream fout;
  2. // ...
  3. filebuf iobuf;
  4. 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 28th, 2005
0

Re: Error trying to write to existing file

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.

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.
Attached Files
File Type: h RPG.H (90.0 KB, 13 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Yoshidex is offline Offline
21 posts
since Jun 2005
Jun 28th, 2005
0

Re: Error trying to write to existing file

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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 28th, 2005
0

Re: Error trying to write to existing file

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:
Last edited by Yoshidex; Jun 28th, 2005 at 7:31 pm. Reason: Forgot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Yoshidex is offline Offline
21 posts
since Jun 2005
Jun 28th, 2005
0

Re: Error trying to write to existing file

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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Yoshidex is offline Offline
21 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Threading (multi threading) in Linux
Next Thread in C Forum Timeline: Random Number Guessing Game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC