Problem with reading files from text source.

Hello Experts,


When I use this function...


     ifstream inFile("filename");  // where file name is a literal string.

my code compile without error.

but when I use this code...


     string myFileName = "filename";   // where filename is a litteral string and myFileName contains the value "filename"
     ifstream inFile(myFileName);      


I get this compilation error...

     cannot convert parameter 1 from 'std::string' to 'const char *'

How can I fix this?  And what am I doing wrong?

thank you,

-TH


travishabermanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lbertaccoCommented:
You need to pass the filename as an ASCII_Z (aka C style) string.

Just use:

 string myFileName = "filename";   // where filename is a litteral string and myFileName contains the value "filename"
     ifstream inFile(myFileName.c_str());      
0
Tex54Commented:
its seems the infile method is trying to take a char array, rather then just a string literal.

instead of this line:

string myFileName = "filename";

do this:

char myFileName[] = "filename"

so your code becomes:

     char myFileName[] = "filename"
     ifstream inFile(myFileName);

Regards Thomas    

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
travishabermanAuthor Commented:
I allready tried that, and it works... but then my getline function is no good.  That is, I have to use this...

   inFile.getline(char, int)  <-- in this case I have to know the size of my     char array[size]     in order to acomidate the entire line.

instead of this

     getline(ifstream, string)     <-- in this I dont have to know the size because the string grows dynamically.


if I use the function you suggest the second getline function will give me a similar compilation error.

Any thoughts?

Thank you,

-TH
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

travishabermanAuthor Commented:
My last post was addressed to lbertacco let me examine your suggestion Tex54..  Be right back.

-TH
0
travishabermanAuthor Commented:
I am still getting problems.. Let me approac this from a different angle.  I need to prompt a user for a file name.  Then I need to open the file and read in one line of it at a time.  I would like to use this getline funciton because then, no matter the line lenght, it will read the entire line and store it to a string. So,

What is the best way to approach that?

Thank you,

-TH
0
travishabermanAuthor Commented:
You know what.. I figured it all out! But, both of your suggestions were fine for my original quesiton.  So thank you.

-TH
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.