Help: I cannot use getline(...)
-
fstream file; char buffer[MAXBUFSIZE]; string line; file.open(..., ios_base::in); file.getline(buffer, MAXBUFSIZE-1); //getline(file, line); //both of the above fail to work //and the vc.net2003 print such error info: //error C2663: “std::basic_istream<_Elem,_Traits>::getline” : //2 //overloaded functions lack a legal conversion of pointer "this".
-
fstream file; char buffer[MAXBUFSIZE]; string line; file.open(..., ios_base::in); file.getline(buffer, MAXBUFSIZE-1); //getline(file, line); //both of the above fail to work //and the vc.net2003 print such error info: //error C2663: “std::basic_istream<_Elem,_Traits>::getline” : //2 //overloaded functions lack a legal conversion of pointer "this".
This works fine: -------------------------------------- #include #include using namespace std; void main() { fstream file; const int MAXBUFSIZE = 256; char buffer[MAXBUFSIZE]; file.open("a.txt", ios_base::in); do { file.getline(buffer, MAXBUFSIZE-1); //getline(file, line); string s = buffer; cout << s.c_str() << endl; } while (! file.eof() ); } Maxwell Chen
-
This works fine: -------------------------------------- #include #include using namespace std; void main() { fstream file; const int MAXBUFSIZE = 256; char buffer[MAXBUFSIZE]; file.open("a.txt", ios_base::in); do { file.getline(buffer, MAXBUFSIZE-1); //getline(file, line); string s = buffer; cout << s.c_str() << endl; } while (! file.eof() ); } Maxwell Chen
Might I suggest something more like this:
#include <iostream>
#include <fstream>
#include <string>int main() {
std::ifstream file("filename");
std::string line;while (std::getline(file, line)) {
std::cout << line << '\n';
}return 0;
}Just a couple of points:
- C++ defines main as: "int main()" or "int main(int argc, char* argv[])". There's nothing in the code that limits us to Visual C++ and its non-standard "void main()" extension.
- Using std::getline (defined in "string") we avoid using a char* buffer as we have to with std::basic_istream<...>::getline.
- Invoking s.c_str() on something we write out using iostreams anyway might incur the following overhead: a char* buffer that can contain the contents is allocated, the contents of s is copied over into it, a terminating null character is added and this buffer is returned. This is because the contents of std::string isn't guaranteed to be null terminated.
Hope this helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^])
-
Might I suggest something more like this:
#include <iostream>
#include <fstream>
#include <string>int main() {
std::ifstream file("filename");
std::string line;while (std::getline(file, line)) {
std::cout << line << '\n';
}return 0;
}Just a couple of points:
- C++ defines main as: "int main()" or "int main(int argc, char* argv[])". There's nothing in the code that limits us to Visual C++ and its non-standard "void main()" extension.
- Using std::getline (defined in "string") we avoid using a char* buffer as we have to with std::basic_istream<...>::getline.
- Invoking s.c_str() on something we write out using iostreams anyway might incur the following overhead: a char* buffer that can contain the contents is allocated, the contents of s is copied over into it, a terminating null character is added and this buffer is returned. This is because the contents of std::string isn't guaranteed to be null terminated.
Hope this helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^])
thank you for the response. Finnally, I figured out the key problem. the fstream object is the data member of my class not declared mutable, but in the member function declared const, the fstream.getline() failed. //ps: the error information vc.net2003 offers me //just confuses me:(
-
Might I suggest something more like this:
#include <iostream>
#include <fstream>
#include <string>int main() {
std::ifstream file("filename");
std::string line;while (std::getline(file, line)) {
std::cout << line << '\n';
}return 0;
}Just a couple of points:
- C++ defines main as: "int main()" or "int main(int argc, char* argv[])". There's nothing in the code that limits us to Visual C++ and its non-standard "void main()" extension.
- Using std::getline (defined in "string") we avoid using a char* buffer as we have to with std::basic_istream<...>::getline.
- Invoking s.c_str() on something we write out using iostreams anyway might incur the following overhead: a char* buffer that can contain the contents is allocated, the contents of s is copied over into it, a terminating null character is added and this buffer is returned. This is because the contents of std::string isn't guaranteed to be null terminated.
Hope this helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^])
Henrik, you got my 5! ;) Basically I agree with all of your view points, and they are quite true. It reminds me to pay efforts in studying the Standard Library. Regarding to the prototype of main, you are right too, as we don't find any 'void main()' in Stroustrup's book. It's just my bad custom for simplicity, since in the MSDN Library it is stated: Alternatively, the main and wmain functions can be declared as returning void (no return value). If you declare main or wmain as returning void, you cannot return an exit code to the parent process or operating system using a return statement; to return an exit code when main or wmain is declared as void, you must use the exit function. Anyway, I like your attitude! :) Maxwell Chen
-
Might I suggest something more like this:
#include <iostream>
#include <fstream>
#include <string>int main() {
std::ifstream file("filename");
std::string line;while (std::getline(file, line)) {
std::cout << line << '\n';
}return 0;
}Just a couple of points:
- C++ defines main as: "int main()" or "int main(int argc, char* argv[])". There's nothing in the code that limits us to Visual C++ and its non-standard "void main()" extension.
- Using std::getline (defined in "string") we avoid using a char* buffer as we have to with std::basic_istream<...>::getline.
- Invoking s.c_str() on something we write out using iostreams anyway might incur the following overhead: a char* buffer that can contain the contents is allocated, the contents of s is copied over into it, a terminating null character is added and this buffer is returned. This is because the contents of std::string isn't guaranteed to be null terminated.
Hope this helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^])
thank you for the response. Finnally, I figured out the key problem. the fstream object is the data member of my class not declared mutable, but in the member function declared const, the fstream.getline() failed. //ps: the error information vc.net2003 offers me //just confuses me:(