| | |
i gave up! anyone knows "ofstream"
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2009
Posts: 4
Reputation:
Solved Threads: 0
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.
#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.
-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.
•
•
Join Date: Sep 2008
Posts: 55
Reputation:
Solved Threads: 10
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:
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.
To open a file using ofstreams you first create the object, then open the file. Like so:
CPP Syntax (Toggle Plain Text)
ofstream file; file.open("a.txt"); //Do something 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.
0
#5 Nov 1st, 2009
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <string> int main() { for ( char ch = 'A'; ch <= 'Z'; ch++ ) { std::string fn = ch + std::string(".txt"); std::ofstream file(fn.c_str()); } return 0; }
Я из Молдавии. Говорю на Русском.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Need to know how to Start
- Next Thread: Filtering a vector's contents based on regex
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






