C++ Question

zoobyzooby Join Date: 2003-08-26 Member: 20236Members
<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.

Comments

  • SkulkBaitSkulkBait Join Date: 2003-02-11 Member: 13423Members
    edited January 2004
    I don't use ap classes, but I'd guess that theres a method in them for getting a char * to the actual string ( fileName.c_str() ?). Otherwise, I'd just declare fileName as a char * instead.
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    edited January 2004
    Don't use apstring (whatever the hell it is). What's wrong with std::strings?
    <!--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-->
  • WheeeeWheeee Join Date: 2003-02-18 Member: 13713Members, Reinforced - Shadow
    edited January 2004
    yes, in order to use apstrings (which you probably shouldn't in any case...since they're copyrighted by the college board people), in a fstream/iostream file argument, you have to typecast it to a cstring (which as was earlier mentioned was some function like c_str()...bleh)
  • Marik_SteeleMarik_Steele To rule in hell... Join Date: 2002-11-20 Member: 9466Members
    edited January 2004
    The ap classes were used last year (and as far as I know, every year before) for computer science courses in high school that would prepare you for the AP tests Computer Science A and Computer Science AB.

    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?
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    You know, learning C++ instead of Java isn't exactly going to kill him. Java's almost a subset of C++ <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo-->
  • CreepieCreepie Join Date: 2003-02-19 Member: 13734Members
    What is apstring ?
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    <!--QuoteBegin--_Creep_+Jan 22 2004, 04:22 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (_Creep_ @ Jan 22 2004, 04:22 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> What is apstring ? <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    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-->
  • CreepieCreepie Join Date: 2003-02-19 Member: 13734Members
    <!--QuoteBegin--SoulSkorpion+Jan 22 2004, 05:15 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Jan 22 2004, 05:15 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin--_Creep_+Jan 22 2004, 04:22 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (_Creep_ @ Jan 22 2004, 04:22 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> What is apstring ? <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    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 ?
Sign In or Register to comment.