Re: std::ifstream

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"David Wilkinson" wrote:

Fil wrote:
Hi,

I am learing C++ with Thinking in C++ (Bruce Eckel) and we've done some
exercises on ifstream in the past chapter.
If I pass to my ifstream object a constant filePath (as we did in he book)
it works, but it doesn't as soon as I pass it a variable.
I have got one error araising from linesCount.cpp, line 6.

main.cpp
-------------------------------------------------------------------------------------
#include "linesCount.h"

int main()
{
return linesCount("C:\\TICPP-2nd-ed-Vol-one\\html\\Frames.html");
}
-------------------------------------------------------------------------------------

linesCount.h
-------------------------------------------------------------------------------------
#include <string>
int linesCount(std::string filePath);
-------------------------------------------------------------------------------------

linesCount.cpp
-------------------------------------------------------------------------------------
#include <string>
#include <fstream>

int linesCount(std::string filePath)
{
std::ifstream inFile(filePath);
std::string line;
int count(0);
while (std::getline(inFile,line))
{
count++;
}
return count;
}
-------------------------------------------------------------------------------------

The error message is:

linescount.cpp(6) : error C2664:
'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const char
*,std::ios_base::openmode,int)' : cannot convert parameter 1 from
'std::string' to 'const char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called

Fil:

You have to use c_str() member to get const char* from a string;

Thanks a lot. I trust that ifstream will be able to find the length of the
string by itself.
Usually when I am passing an array to a function through a pointer I also
pass the length.

int linesCount(const std::string& filePath)
{
std::ifstream inFile(filePath.c_str());
std::string line;
int count(0);
while (std::getline(inFile,line))
{
count++;
}
return count;
}

That's cool, it works perfectly.

Note that you should pass the string by const reference here, to avoid
unnecessary copy.

I am not sure why but I hope it is explained somewhere in my textbook
("Thinking in C++"). Thanks again.

--
David Wilkinson
Visual C++ MVP

.



Relevant Pages

  • Re: std::ifstream
    ... If I pass to my ifstream object a constant filePath it works, but it doesn't as soon as I pass it a variable. ... int linesCount; ... You have to use c_strmember to get const char* from a string; ...
    (microsoft.public.vc.language)
  • Re: unique numbers using srand( ) and rand( ) functions in C++[ caution long post]
    ... /*for declaring objects of the ofstream and ifstream classes for a data ... int main ... /*consume # character betwwen numbers of each record in data file or the ... return SUCCESS; ...
    (alt.comp.lang.learn.c-cpp)
  • Slow ifstream close when accessing network files
    ... The code in question opens an ifstream, calls seekgwith a non zero value, ... unsigned int threadHandle = 0; ... ifstream *ifs; ... void TestFileAccess2{ ...
    (microsoft.public.vc.stl)
  • std::ifstream
    ... If I pass to my ifstream object a constant filePath ... int linesCount; ... No user-defined-conversion operator available that can perform this ...
    (microsoft.public.vc.language)
  • Re: Read from txt file
    ... When you read an int using>>, it will stop reading when it reaches the ... ifstream input; ... while (input>> num) ...
    (comp.lang.cpp)