i gave up! anyone knows "ofstream"

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 4
Reputation: apo is an unknown quantity at this point 
Solved Threads: 0
apo apo is offline Offline
Newbie Poster

i gave up! anyone knows "ofstream"

 
-1
  #1
Nov 1st, 2009
Hi, I'm trying to create a quick and small programs that works when you press F9, it should create files on my desktop using "ofstream" from A to Z. I can't get it to run when i put ofstream in a for cycle. Here is the code to see it better.

#include <cstdlib.h>
#include <iostream.h>
#include <conio.h>
#include<fstream.h>
//------------------
void main()
{
int i;
char letras[3];
letras[0]='a';
letras[1]='b';
letras[2]='c';

for(i=0;i<3;i++)
ofstream print(letras[i]);

}

thanks you so much for any help.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,571
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
Nov 1st, 2009
The parameter to open() has to be a null-terminated character array (commonly called a string). All you are trying to send it is a single character. Try this:
int main()
{
int i;
char* letras[3] = {"a","b","c"};

for(i=0;i<3;i++)
{
    ofstream print(letras[i]);
}

}
Last edited by Ancient Dragon; Nov 1st, 2009 at 10:15 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 55
Reputation: BeyondTheEye is an unknown quantity at this point 
Solved Threads: 10
BeyondTheEye BeyondTheEye is offline Offline
Junior Poster in Training
 
0
  #3
Nov 1st, 2009
An ofstream is an object, not a function.
To open a file using ofstreams you first create the object, then open the file. Like so:
  1. ofstream file;
  2. file.open("a.txt");
  3. //Do something
  4. file.close();

In your case you could place it in a loop. You'll have to make a string object to get the name of the file right.
Oh, the open function requires a cstring, not a standard string.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 4
Reputation: apo is an unknown quantity at this point 
Solved Threads: 0
apo apo is offline Offline
Newbie Poster
 
0
  #4
Nov 1st, 2009
thanks for the help! it solved my problem.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 39
Reputation: niXman is an unknown quantity at this point 
Solved Threads: 10
niXman's Avatar
niXman niXman is offline Offline
Light Poster
 
0
  #5
Nov 1st, 2009
  1. #include <fstream>
  2. #include <string>
  3. int main() {
  4. for ( char ch = 'A'; ch <= 'Z'; ch++ ) {
  5. std::string fn = ch + std::string(".txt");
  6. std::ofstream file(fn.c_str());
  7. }
  8. return 0;
  9. }
Я из Молдавии. Говорю на Русском.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC