C++ Question
<div class="IPBDescription">file writing</div> I was wondering if there was a way to open user-specified files in c++ (like user types in file name, program opens that file) as illustrated below
#include <fstream.h>
#include "apstring.h" //for string class
#include <iostream.h>
void writeFile()
{
ofstream outfile;
apstring fileName;
cout << "Enter file name: ";
cin >> fileName;
outfile.open( fileName, ios::out ); // open file for writing
}
It gives compiling error: "cannot convert parameter 1 from 'class apstring' to 'const char *" so i'm guessing it has something to do with the way apstring is defined in apstring.h, but if you have any knowledge about this, or about how to make it so it works, I'd be very thankful.
#include <fstream.h>
#include "apstring.h" //for string class
#include <iostream.h>
void writeFile()
{
ofstream outfile;
apstring fileName;
cout << "Enter file name: ";
cin >> fileName;
outfile.open( fileName, ios::out ); // open file for writing
}
It gives compiling error: "cannot convert parameter 1 from 'class apstring' to 'const char *" so i'm guessing it has something to do with the way apstring is defined in apstring.h, but if you have any knowledge about this, or about how to make it so it works, I'd be very thankful.
Comments
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
#include <fstream> //putting in the .h is depracated.
#include <string> //the standard library has a string class.
#include <iostream>
using namespace std;//just stick this in. trust me :)
void writeFile()
{
ofstream outfile;
string fileName;
cout << "Enter file name: ";
cin >> fileName;
#ifdef THIS_WORKS //hehe. I'm not sure if the first way works or not;)
outfile.open(fileName);
#else
outfile.open(fileName.c_str()); //.c_str() retrieves the character array version of a string
#endif
}
<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
What I'm wondering is, if you're taking these courses, why are you using C++? I was told that the college board had switched all AP tests taking place at the end of this school year from C++ (the language I first learned) to Java. Is this teacher of mine an idiot for teaching the wrong language now, or are you using apstring for another reason?
Unfortunately, no one can be... told what apstring is...
...you have to read Marik's post for yourself. <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo-->
Unfortunately, no one can be... told what apstring is...
...you have to read Marik's post for yourself. <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo--> <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
Ah. Some basic wrapper then. What's wrong with CString or std::string ?