Solved

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

Posted on 2007-11-17
5
237 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

735 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