473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

why can't use string for getline()?

Excuse me, could you point out why the command below will generate error?
And how to overcome this problem by still using string?? i know char array
is ok but i don't want to use it. Thanks a lot

int main(){

ifstream ifs("abc.txt", ios::in);
if (!ifs){
cerr << "Cannot open abc.txt for input" << endl;
return 1;
}

string buffer;
ifs.getline(buffer, 500, '\n'); // why has error this line

Billy
Jul 22 '05 #1
4 2050
news.hku.hk wrote in news:40******@newsgate.hku.hk in comp.lang.c++:
Excuse me, could you point out why the command below will generate
error? And how to overcome this problem by still using string?? i know
char array is ok but i don't want to use it. Thanks a lot

int main(){

ifstream ifs("abc.txt", ios::in);
if (!ifs){
cerr << "Cannot open abc.txt for input" << endl;
return 1;
return 1 *isn't* portable, se code below for the only 2 portable
return values for main.
}

string buffer;
ifs.getline(buffer, 500, '\n'); // why has error this line


You need to use non-member std::getline with std::string:

#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib> // EXIT_*

int main()
{
using namespace std;

istringstream ifs( "file content\nfor a\nusnet posting\n" );
if (!ifs)
{
cerr << "Cannot open abc.txt for input" << endl;
return EXIT_FAILURE;
}

string buffer;
int line = 0;

while ( getline(ifs, buffer) )
{
cout << "line " << ++line << ": " << buffer << '\n';
}

cout.flush();

/* return 0 and no return are equivalent to this
*/
return EXIT_SUCCESS;
}

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message
news:40******@newsgate.hku.hk...
Excuse me, could you point out why the command below will generate error?
And how to overcome this problem by still using string?? i know char array
is ok but i don't want to use it. Thanks a lot


getline(ifs, buffer);

john
Jul 22 '05 #3
Rob Williscroft wrote:
[...]
return 1 *isn't* portable,
[...]


In what sense?

curious,
- J.
Jul 22 '05 #4
Jacek Dziedzic wrote in news:c6**********@korweta.task.gda.pl in
comp.lang.c++:
Rob Williscroft wrote:
[...]
return 1 *isn't* portable,
[...]


In what sense?

curious,
- J.


In the context of Standard C++.

The Standard gives us 2 portable values to return from main()
EXIT_SUCCESS and EXIT_FAILURE, they're defined in <cstddef> and
they are macros. Additionaly we can return 0 from main() and
that is required to be the same as returning EXIT_SUCCESS.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Roberto Dias | last post by:
Hi all, What to do for searching for more than one string occurrence in the same string (this last are line of a text). I have used getline(), to get the text lines by means of WHILE loop and...
3
by: algorithm | last post by:
Hi, I've a simplified program snippet in which getline behaves in a way, which at least I find odd: // File: getline_example.cpp #include <iostream> #include <string> using namespace std;
6
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
4
by: Sanchit | last post by:
I want to know thta how can i edit a file in C++ For Example my file is Mr XyZ FFFFFF 65 And now i want go change this number 65 to 87.... how can i Do this..... I...
7
by: shendraeg | last post by:
I'm trying to use fstreams to format/encode a text file in a way that is useful for another program of mine, and I came upon a little snag... The input file is formated as such: int (line count)...
3
by: dirtysouth6975 | last post by:
I am writing a program for an assignment in which you choose from a menu to append on a file, show the results of the file or exit. However, I can't figure out how to show the results or exit it....
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
1
by: eraserwars | last post by:
My compiler keeps saying LNK2019, and my teacher says to look for spelling error. He says that most likely what is happening is that a spelling error is messing my program up. I searched, and I did...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.