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

x
?
Solved

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

Posted on 2007-11-17
5
Medium Priority
?
244 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

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