The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Trying to output to a text file
Discuss Trying to output to a text file in the C Programming forum on Dev Shed. Trying to output to a text file C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 5th, 2005, 04:48 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 342
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.
|

July 5th, 2005, 04:50 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 342
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 ?
|

July 5th, 2005, 04:55 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 342
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.
|

July 5th, 2005, 05:02 PM
|
 |
!Ruff Ryder!
|
|
Join Date: Jun 2004
Location: Québec, Canada... Represent!
Posts: 689
  
Time spent in forums: 3 Days 7 h 10 m 12 sec
Reputation Power: 11
|
|
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!***)
|

July 5th, 2005, 05:15 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 342
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.
|

July 5th, 2005, 05:43 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 342
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.
|

July 5th, 2005, 06:16 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 342
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
|

July 5th, 2005, 06:41 PM
|
 |
!Ruff Ryder!
|
|
Join Date: Jun 2004
Location: Québec, Canada... Represent!
Posts: 689
  
Time spent in forums: 3 Days 7 h 10 m 12 sec
Reputation Power: 11
|
|
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.
|

July 5th, 2005, 07:30 PM
|
|
Brony & F/OSS Advocate
|
|
Join Date: Jul 2003
Location: Anaheim, CA (USA)
|
|
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". 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|