Solved

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

Posted on 2007-11-17
5
231 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
Comment Utility
     ifstream myfile ( string1.c_str() );
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
Comment Utility
>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
Comment Utility
  ifstream myfile ( string1.c_str() );



what does this mean?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

763 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

12 Experts available now in Live!

Get 1:1 Help Now