Solved

[noob][c++]  something very simple is wrong, but what is it?

Posted on 2007-11-17
5
238 Views
Last Modified: 2010-08-05
// reading a text file
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

string open_file_return_lines ( string string1 );

int main ()
{
      string string1 = "example.txt";
     


      string1 = open_file_return_lines ( string1 );
      
      cout << "about to print string1" << endl;
      cout << string1 << endl;
      return 0;
}






string open_file_return_lines ( string string1 )
{
      string line;
      ifstream myfile ( string1 );
     
      int number_of_lines;
      number_of_lines = 0;
      
      // first get the number of lines there are.
      if ( myfile.is_open() )
      {
            while ( ! myfile.eof() )
            {
                  getline (myfile,line);
                  number_of_lines = number_of_lines + 1;
                  cout << line << endl;
            }
     
            myfile.close();
      }

      else
      {
            cout << "Unable to open file";
      }

      // cout << number_of_lines << endl;
      // create an array + 1, store array size - 1 in 0
      // loop again to get all the lines into the array.
     
     
      // return array.
      return line;
      
}
0
Comment
Question by:Troudeloup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20305281
     ifstream myfile ( string1.c_str() );
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20305287
And as I said in your other post : can you post the error message next time ? It makes things easier for us to help you.
0
 

Author Comment

by:Troudeloup
ID: 20305289
>C:\MinGw\bin\g++ -pedantic -Os -c test.cpp -o test.o
test.cpp: In function `std::string open_file_return_lines(std::string)':
test.cpp:50: error: no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)'
C:/MinGw/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/iosfwd:90: note: candidates are: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)
C:/MinGw/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/fstream:524: note:                 std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
C:/MinGw/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/fstream:509: note:                 std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char, _Traits = std::char_traits<char>]
0
 

Author Comment

by:Troudeloup
ID: 20305291
  ifstream myfile ( string1.c_str() );



what does this mean?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 20305333
You have to use that line instead of :

      ifstream myfile ( string1 );

The ifstream constructor expects a const char*, not a std::string. So, you have to "convert" the std::string string1 to a const char* using c_str().

        http://www.cplusplus.com/reference/iostream/ifstream/ifstream.html
        http://www.cplusplus.com/reference/string/string/c_str.html
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 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