Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-19-2005, 07:17 AM   #1 (permalink)
Lordieth
Registered User
 
Join Date: Sep 2005
Posts: 12
Lordieth is on a distinguished road
Filestreams (multiples)

This one has been bugging me for some time, so i'd appreciate it if anyone could help.

Right, when I declare my stream like so:

Code:
fout.open("Log.txt");
and use the stream as you normally would, everything is fine - but the problem is, I want to create another one, for a different file in the same program - how do I go about doing that? I tried changing it from fout to something else when creating a new txt file, and got this:

Code:
'fout2' undeclared (first use this function)
and obviously, just trying to re-open a different file with the same stream, doesn't seem to work, unless i'm missing something? this is probably something really simple i'm missing, but i've never had it explained it to me, in books, tutorials, or otherwise, so I would really appreciate it!
Lordieth is offline   Reply With Quote
Old 09-19-2005, 08:03 AM   #2 (permalink)
gitter1226
Registered User
 
Join Date: Sep 2005
Posts: 3
gitter1226 is on a distinguished road
You can have as many files open as you want, within reason of course. They don't need to be named fout or anything similar to that, it is just a convention. If you want to have two open filestreams just do this :

Code:
ofstream f1out, f2out;
f1out.open("Log.txt");
f2out.open("Another.txt");
You can use those two file streams as normal.
If this is not working can I see all your code?

HTH,
Jeff
gitter1226 is offline   Reply With Quote
Old 09-19-2005, 10:29 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
You can use the same stream OBJECT as well. Just don't forget to close the object before loading a new file:
Code:
#include <iostream>
#include <fstream>

int main()
{
  std::ofstream fout("file_one.txt", std::ios::app);
  fout << "modified" << std::endl;
  
  //Close first!
  fout.close();
  
  fout.open("file_two.txt", std::ios::app);
  fout << "modified" << std::endl;
  return 0;
}
__________________
Valmont is offline   Reply With Quote
Old 09-19-2005, 11:17 AM   #4 (permalink)
Lordieth
Registered User
 
Join Date: Sep 2005
Posts: 12
Lordieth is on a distinguished road
Quote:
Originally Posted by gitter1226
You can have as many files open as you want, within reason of course. They don't need to be named fout or anything similar to that, it is just a convention. If you want to have two open filestreams just do this :

Code:
ofstream f1out, f2out;
f1out.open("Log.txt");
f2out.open("Another.txt");
You can use those two file streams as normal.
If this is not working can I see all your code?

HTH,
Jeff
Heh, I was declaring ofstream twice, and for some reason it doesn't like that - see, I knew it'd be something simple - thanks

and thankyou aswell Valmont, that'll come in handy
Lordieth is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 10:49 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting