Re: Input string vs. char array
From: Josh Sebastian (usenet_at_inglorion.com)
Date: 02/05/04
- Next message: Christopher Benson-Manica: "Re: [C] simple string question"
- Previous message: E. Robert Tisdale: "Re: [C] simple string question"
- In reply to: KPR: "Input string vs. char array"
- Next in thread: Ben Measures: "Re: Input string vs. char array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 04 Feb 2004 22:58:29 -0500
On Thu, 05 Feb 2004 02:25:03 GMT, KPR <kpruppel@optonline.net> wrote:
> Okay, here's my next "simple" question.
> ------------------------------------------------------------------------------------------
> #include <fstream>
> #include <iostream>
> #include <string>
> using namespace std;
>
> int main()
> {
> string inbufstr; ifstream MtgRec("L:\\C++\\AAMtgs2.txt");
> ofstream AnalRec("L:\\C++\\Analysis.txt");
> if (MtgRec.fail()) {cout << "Cannot open file" << endl;}
>
> while (! MtgRec.eof())
> { MtgRec >> inbufstr;
> cout << inbufstr; AnalRec << inbufstr;
> } }
> --------------------------------------------------------------------------------------------
>
> The above code inputs a string. My problem is, there are whitespaces in
> the input records, and inputting into a *type string* uses the
> whitespaces as line delimiters (which unacceptably breaks up names,
> addresses, etc.).
> Inputting into a *type char* array uses the newline character as the
> line delimiter.
No, it doesn't. If you use the operator>> with std::string or with C-style
strings, it uses whitespace as the delimiter. What you want is to use
getline (std::getline for std::string, istream::getline for C-style
strings), which allows you to use an arbitrary character as the delimiter
(defaults to '\n').
-- Josh
- Next message: Christopher Benson-Manica: "Re: [C] simple string question"
- Previous message: E. Robert Tisdale: "Re: [C] simple string question"
- In reply to: KPR: "Input string vs. char array"
- Next in thread: Ben Measures: "Re: Input string vs. char array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|