Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, August 21, 2017 6:24 AM
My codes are similar to below snippet. I got error of "...error C3861: 'getline': identifier not found ...".
If I added #include <iostream> and change getline() as std::getline(), I got error:
"'std::getline': no matching overloaded function found".
'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': expects 2 arguments - 3 provided
How should I use getline() correctly? My platform is VS2015.
#include <stdio.h>
#include <stdlib.h>
``FILE`` *file = ``fopen``(filename, ``"r"``);
.....
``// read each line and print it to the screen
``int`` line_number = ``0``;
``while``(``-1`` != ``getline``(&buffer, &buffer_size, file))
{
``printf``(``"``%d``: ``%s``"``, ++line_number, buffer);
}
All replies (7)
Monday, August 21, 2017 7:39 AM ✅Answered | 1 vote
Hello,
use the std-lib like this:
#include <fstream>
CString szFilename = _T(" <your filename> ");
std::ifstream fi;
fi.open((LPCTSTR)szFilename);
if (!fi.is_open())
{
return;
}
char szline[10240];
while (fi.getline(szline, 10240))
{
// ...
}
fi.close();
Regards, Guido
Monday, August 21, 2017 11:34 AM ✅Answered | 1 vote
On 8/21/2017 2:24 AM, Stan Huang at Taiwan wrote:
FILE *file = fopen(filename, "r");
std::getline is designed to read from std::istream or a class derived therefrom; not from FILE*. And it reads into std::string, not a raw buffer. If you insist on using FILE* and raw buffer, there's fread(). Otherwise:
std::ifstream file(filename);
std::string line;
int line_number = 0;
while (std::getline(file, line)) {
std::cout << ++line_number << ": " << line;
}
Monday, August 21, 2017 7:18 PM ✅Answered | 1 vote
How should I use getline() correctly? My platform is VS2015.
#include <stdio.h>
#include <stdlib.h>
``FILE`` *file = ``fopen``(filename, ``"r"``);
.....
``// read each line and print it to the screen
``int`` line_number = ``0``;
``while``(``-1`` != ``getline``(&buffer, &buffer_size, file))
{
``printf``(``"``%d``: ``%s``"``, ++line_number, buffer);
}
By way of fleshing out some of the details, take note that there are
two getline() functions:
(1) istream::getline
https://msdn.microsoft.com/en-us/library/aa277361(v=vs.60).aspx
This is a member function of basic_istrean and its derivatives.
It reads the string into a "raw" or "native" character array.
The reply from Guido uses this version of getline.
(2) std::getline (from <string>)
https://msdn.microsoft.com/en-CA/library/2whx1zkx(v=vs.100).aspx
https://msdn.microsoft.com/library/1a4ffd11-dce5-4cc6-a043-b95de034c7c4.aspx#getline_template_function
Unlike (1) above, this is not a member function of a class.
It reads the string into a std::string.
The reply from Igor uses this version of getline.
As noted by Igor, if you must use the input streams from the C Standard
Library (fopen, FILE*) then the preferred route would be to use the
functions provided for manipulating such streams. To read strings, see:
fgets, fgetws
https://msdn.microsoft.com/en-us/library/c37dh6kf.aspx
- Wayne
Tuesday, August 22, 2017 3:39 AM ✅Answered | 1 vote
On 8/21/2017 11:33 PM, Stan Huang at Taiwan wrote:
It works basically. But why the line "
<p>std::cout << ++line_number << <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline; color:#a31515">": "</span> << line;"</p><p></p><p>I got error of</p>"error C2039: 'cout': is not a member of 'std'"
You are probably missing #include <iostream>
Tuesday, August 22, 2017 5:13 AM ✅Answered | 1 vote
I learned the usage of getline() form http://timmurphy.org/2010/10/31/reading-from-a-file-in-c-2/
I guessed it worked at some situation. So, how come it doesn't work at VS environment?
That uses a non-standard C extension that is supported by some compilers
on some platforms. It is not part of the ISO C99 Standard nor the ISO
C++ Standard, and is not supported by Visual C++. See:
undefined reference to `getline' in c
https://stackoverflow.com/questions/13112784/undefined-reference-to-getline-in-c
- Wayne
Tuesday, August 22, 2017 3:33 AM
It works basically. But why the line "
<p>std::cout << ++line_number << <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline; color:#a31515">": "</span> << line;"</p><p></p><p>I got error of</p>
"error C2039: 'cout': is not a member of 'std'"
Tuesday, August 22, 2017 4:21 AM
I learned the usage of getline() form http://timmurphy.org/2010/10/31/reading-from-a-file-in-c-2/
I guessed it worked at some situation. So, how come it doesn't work at VS environment?