Computer problems,Computer help
*AX SOFT>>>Programming & Design

C++: How do I set my file name as a variable so I can open whatever file I want during execution of program?


My codes looks something like this:

ifstream infile
infile.open("hours.dat");

infile>>Records[i].....so on.

I want to be able to write:
cin>>Variable
infile.open(Variable);
infile>>Records[i].....

This isn't working for me. What am I doing wrong. Is it even possible to use a variable for a file name? How do I do it? Thanks!

void open ( const char * filename, ios_base::openmode mode = ios_base::in );


Your file name must be of type const char *.
Your variable "Variable" must be a string since you are using
cin >> Variable;

So, to open the file, use:
infile.open(Variable.c_str());

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • What is the demand for web designers and coders for the next decade?

    It's looking really good dude! You don't even have to go to college for this field since most of it is available through the internet for free.

    ...
  • I want to learn a programming language. Where should I start, and can you recommend some books?

    The comment above about learning how to program is spot on. Once you know how to program and understand the concepts, the knowledge is pretty well transferable to all languages. A great place f...

  • Borland C++ compiler problems.?

    Sounds like a bad character in the hello.cpp file, probably on line 2. Note that the bad character may not display. I suggest retyping the line exactly as it appears, then deleting the origin...

  • How can I launch Windows Media Player 11 from a java program?

    Make sure you are using the full pathname. "c:\Program Files\blahblah\wmplayer.exe mymovie.mpeg" I haven't used that command in a while, but I believe this could help you. If ...

  • The Webalizer or analog?

    Neither. Have a long look at Google Analytics. Full featured, powerful, and free. And you can start using it without signing up if you've already got a GMail account.

    ...
  • What are the pros and cons of web hosting?

    Not sure exactly if you're asking if you should host your website from your personal computer at home or purchase a web hosting account with a company? If that's the case, I'd wou...

  • What is a good website builder (free) plz help 10 points for sure?

    BLUE VODA rocks..! its is very easy to use.......n its freeware...u can have it frm its web tht is ...

  • How do I go on blocked sites at school? [with my own computer/no proxy]?

    Try it by the Ip numbers instead of www.myspace.com If that fails, I am afraid we are back to proxies.

    ...
  •  

    Categories--Copyright/IP Policy--Contact Webmaster