[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with reading files from text source.

Posted on 2004-11-24
6
Medium Priority
?
241 Views
Last Modified: 2010-04-17
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


0
Comment
Question by:travishaberman
  • 4
6 Comments
 
LVL 11

Assisted Solution

by:lbertacco
lbertacco earned 1000 total points
ID: 12672132
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
 

Accepted Solution

by:
Tex54 earned 1000 total points
ID: 12672142
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
 

Author Comment

by:travishaberman
ID: 12672180
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:travishaberman
ID: 12672186
My last post was addressed to lbertacco let me examine your suggestion Tex54..  Be right back.

-TH
0
 

Author Comment

by:travishaberman
ID: 12672253
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
 

Author Comment

by:travishaberman
ID: 12672322
You know what.. I figured it all out! But, both of your suggestions were fine for my original quesiton.  So thank you.

-TH
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Simple Linear Regression
Progress

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question