Thread: reading textfiles using variable names

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    7

    reading textfiles using variable names

    This is probably a really silly question (as I'm only a beginner at this!), but I'm having major dramas with reading textfiles.

    The thing is, I can get it to work, if I use the following code:
    <code>
    ifstream cardFile;
    cardFile.open("flush.txt");
    </code>

    But what I want to do, is use a variable for the name of the textfile. I have a menu in which the user can select one of 9 textfiles. eg. if they select 1, then I assign fileName = "flush.txt" or
    if they select 2, then I assign fileName = "FourKind.txt"; (Using a swtich statement).

    So I have the code:
    <code>
    cardFile.open(fileName);
    </code>

    The error message I get when I try to compile is:
    'no matching function for call to 'ifstream:pen(string &)' candidates are ifstream:pen(const char *, int, int)'

    fileName I have declared as a global variable, but as a string.
    Is this why I'm having all the dramas? If so, what am I meant to declare fileName as? I tried declaring it as a char array, but had even more dramas!!

    Any help would be greatly appreciated!!

    Also, before I go, what difference does it make if you add
    using namespace std; into your code? I was told that you can't use cin or cout without it, but I don't put it it and I have no dramas using cin or cout.

    Thankyou! :-)

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    7

    how do u use that code tag?

    Don't mind me, I'm just an idiot!!! I tried using that <code> tag thing, but it didn't work!!

    How do you use code tags?

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    7

    I worked it out!!

    Actually, I didn't work it out. I just found someone elses post on here with the same problem.

    I changed cardFile.open(fileName);

    to cardFile.open(fileName.c_str());

    Don's ask me what it does, but it works!!
    Thankyou to the person who answered the other persons question!! :-)

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    This is probably a really silly question
    Don't worry about asking silly questions! But, first read your book, or your tutorial, or whatever... Then check the Programming FAQ, then search the board to make sure that your question hasn't been answered already. And, show your code, so that nobody thinks you want them to write it for you!

    Also, before I go, what difference does it make if you add
    using namespace std; into your code? I was told that you can't use cin or cout without it, but I don't put it it and I have no dramas using cin or cout.
    See the FAQ. You can use cin and cout, but sometimes using namespace std can make the syntax simpler, when you're using the formatting features of cin and cout.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random variable names
    By NeMewSys in forum C Programming
    Replies: 7
    Last Post: 06-12-2008, 11:26 AM
  2. Random Variable names
    By Milhas in forum C Programming
    Replies: 14
    Last Post: 03-26-2008, 05:03 PM
  3. reading folder names..how is it done ?
    By roalme00 in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2008, 10:34 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM