C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 5th, 2005, 04:48 PM
jjj93421 jjj93421 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 342 jjj93421 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 2 Days 3 h 34 m
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
Trying to output to a text file

Hi. I'm need to output variables and text to a text file or some file that I can open and read later. I found this on the net and I tried this but can't find the file if it did create one.
Using the include:
#include <fstream.h>

Code:

ofstream f("/myfile.txt");

f << "points is "<<points[0]<<" "<<points[1]<<" "<<points[2]<<" "<<points[3]<<endl;
f << "Point is " <<point<<endl;

cout<<"\n";
f.close();



"point" is an int from 1 to 50 usually. "points" is a 4 element array. Thanks.

Reply With Quote
  #2  
Old July 5th, 2005, 04:50 PM
jjj93421 jjj93421 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 342 jjj93421 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 2 Days 3 h 34 m
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
Oh, wow. I just found it. It was on my C drive base directory. If anyone has a better way, let me know. I kind of like this way, though. It's easy. BTW, how would you put it in a different directory ?

Reply With Quote
  #3  
Old July 5th, 2005, 04:55 PM
jjj93421 jjj93421 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 342 jjj93421 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 2 Days 3 h 34 m
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
ok, it keeps printing over the last line used in the text file. I need it to print a new line each time it gets a print command.

Reply With Quote
  #4  
Old July 5th, 2005, 05:02 PM
gt3_dk's Avatar
gt3_dk gt3_dk is offline
!Ruff Ryder!
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2004
Location: Québec, Canada... Represent!
Posts: 689 gt3_dk User rank is Corporal (100 - 500 Reputation Level)gt3_dk User rank is Corporal (100 - 500 Reputation Level)gt3_dk User rank is Corporal (100 - 500 Reputation Level)gt3_dk User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 7 h 10 m 12 sec
Reputation Power: 11
Send a message via Yahoo to gt3_dk
Theres two things I want to tell you.

First of all, do not precede the filename with a " / " because as you noticed it puts it at the root of the drive. If you want it in the same directory as your program you should just type "myfile.txt" and NOT "/myfile.txt".

Also I suggest you pick up the habit of checking for errors because they will happen. Right now this may seem useless because you wrote such a small program, but when you get into bigger apps you'll be glad you added some useful debug features or error messages!

A simple example of error checking would be:
Code:
ofstream f("/myfile.txt");
if (!f.good()) {
     cout << "myfile.txt could not be created! Program now exiting.";
     exit(1);


So if forever reason ofstream "f" could not of been created (ie, it already existed or the disk was full) you would know that this is the problem. Without this you would probably be pulling out your hair looking for what caused your program to crash with no findings in the actual code...
__________________
My b0x:
AMD XP Barton 2800+ @ Stock speed... (going up again soon in winter!!!)
2 x 512MB Kingston RAM for 1GB dual channel
1 x 120GB HDD (soon another ) ( <-- God I've been saying this for like a year )
SoundBlaster Audigy 2 ZS (0wN5!!!)
Logitech Z-5300 speakers (0wN5!!!)
ATI All-In-Wonder 9800 Pro (0wN5!!!)
Black ViewSonic A91f+@1280x1024@75Hz (0wN5!!!) (Well, too bad I can't go 1600x1200 above 60Hz )

If I helped you out please consider helping a poor college student get a free iPod for his long commute by signing up here with my referal link:
My Link
(I will pay you $10-$20 if you want to cover the cost of the offer for you. ***This ISNT a scam. I personally know a girl who recieved one a few months ago!***)

Reply With Quote
  #5  
Old July 5th, 2005, 05:15 PM
jjj93421 jjj93421 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 342 jjj93421 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 2 Days 3 h 34 m
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
Thanks. How do I write data to the text file without re-writing over the previous line ? I've tried several ways now and I can't get it to write more than the 2 lines shown. This code is in a loop that only exits when I close the program so I want it to write to the text file everything until I close the program. Thanks.

Reply With Quote
  #6  
Old July 5th, 2005, 05:43 PM
jjj93421 jjj93421 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 342 jjj93421 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 2 Days 3 h 34 m
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
anyone ? Geez, this is annoying. I can't find crap on this.

Reply With Quote
  #7  
Old July 5th, 2005, 06:16 PM
jjj93421 jjj93421 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 342 jjj93421 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 2 Days 3 h 34 m
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
nevermind, I got it. I was still looking at the C drive file when I forgot that I moved the file to another directory. No wonder it kept looking the same.....LOL

Reply With Quote
  #8  
Old July 5th, 2005, 06:41 PM
gt3_dk's Avatar
gt3_dk gt3_dk is offline
!Ruff Ryder!
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2004
Location: Québec, Canada... Represent!
Posts: 689 gt3_dk User rank is Corporal (100 - 500 Reputation Level)gt3_dk User rank is Corporal (100 - 500 Reputation Level)gt3_dk User rank is Corporal (100 - 500 Reputation Level)gt3_dk User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 7 h 10 m 12 sec
Reputation Power: 11
Send a message via Yahoo to gt3_dk
Quote:
Originally Posted by jjj93421
anyone ? Geez, this is annoying. I can't find crap on this.

Keep in mind that this isn't like MSN... People visit these a few times a day tops. In the future please post only if you're sure you have tired everything... and I'll tell you now before some moderator b*tches about it but use the EDIT function on these forums instead of starting a bunch of new one sentence posts.

Reply With Quote
  #9  
Old July 5th, 2005, 07:30 PM
codergeek42 codergeek42 is offline
Brony & F/OSS Advocate
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Jul 2003
Location: Anaheim, CA (USA)
Posts: 6,638 codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)  Folding Points: 39542 Folding Title: Starter FolderFolding Points: 39542 Folding Title: Starter Folder
Time spent in forums: 1 Month 2 Weeks 6 Days 1 h 33 m 45 sec
Reputation Power: 2474
Send a message via ICQ to codergeek42 Send a message via AIM to codergeek42 Send a message via Yahoo to codergeek42 Send a message via Google Talk to codergeek42
Facebook
I'm somewhat confused on what exactly you're trying to do. Are you trying to append to the file? If so you can use the second parameter of the open() function to specify this:
Code:
#include <fstream>

int main(void) 
{
  using namespace std;
  /* ... */
  ofstream out_file;
  out_file.open("myfile.txt", ofstream::app | ofstream::out);
  out_file << "output text" << endl;
  out_file.close();
  /* ... */
  return 0;
}
That would create the file if it does not exist or append to it if it does. More information is available here

On a side note, you really shoudl be using better variable naming than simply "f".
__________________
~~ Peter ~~ :: ( Who am I? ) :: ( Peter's Musings: Uploading myself, bit by bit... ) :: ( Electronic Frontier Foundation ) :: ( I'm a GNU/Linux addict and Free Software Advocate. ) :: ( How to Ask Questions the Smart Way ) :: ( The Fedora Project, sponsored by Red Hat ) :: ( GNOME: The Free Software Desktop Project ) :: ( GnuPG Public Key ) :: ( About me on the WIki )

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Trying to output to a text file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap