Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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.

929 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now