Re: Input string vs. char array

From: Josh Sebastian (usenet_at_inglorion.com)
Date: 02/05/04


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


Relevant Pages

  • Extracting strings...how?
    ... I'm trying to find the best way to go about extracting all strings that are ... contained within parenthesis from a text box. ... I'd like to strip out the digits that follow the last decimal as ... While I use parse code I have that uses a delimiter, ...
    (microsoft.public.vb.general.discussion)
  • Re: Loop through column and change contents
    ... They have a space as delimiter, but I prefer to do it programmatically so I can learn from this. ... Dim MyRange As Range ... Also how do I loop through another column containing strings and insert a dash between the third and forth character in that string? ...
    (microsoft.public.excel.programming)
  • Re: RegEx: How to ignore the number of whitespaces?
    ... I want to match strings while ignoring the number of whitespaces. ...
    (microsoft.public.dotnet.framework)
  • Re: problem with textscan - delimiter occurs in string field
    ... I want to textscan a file with, (comma) delimiter. ... file contains numbers and strings. ...
    (comp.soft-sys.matlab)
  • Re: DTS export to text file question
    ... You don't say if the strings are quotable or if you have a column ... You may be able to do what you require by adding an extra column on the end ... If the vertical bar is your column delimiter then ... in which cast do the same sort of thing for the column separator ...
    (microsoft.public.sqlserver.programming)

Loading