Spaces in a pointer. Help

Discussion in 'C++' started by foenotes@googlemail.com, Feb 13, 2007.

  1. Guest

    I am pulling the following from a .txt file each into a pointer 1 at a
    time.


    west sussex
    East sussex
    Middlesex
    Dorset

    etc.

    Some of the countys have spaces in them so would require two pointers.
    is there any symbol to use in the text file so both parts are read as
    one pointer and displayed still with a space in the program.

    so in txt it is: West/$Sussex
    but still shows : West Sussex in the c++ program.
     
    , Feb 13, 2007
    #1
    1. Advertising

  2. David Harmon Guest

    On 13 Feb 2007 10:09:38 -0800 in comp.lang.c++,
    "" <> wrote,
    >I am pulling the following from a .txt file each into a pointer 1 at a
    >time.
    >
    >west sussex


    Into a pointer? You cannot put text into a pointer, and it would be a
    bad idea if you could. Use std::string. Use getline() to read a line
    at a time without breaking on spaces.

    #include <iostream>
    #include <fstream>
    #include <string>

    int main(void)
    {
    std::string line;
    std::ifstream fh("file.txt");
    while (std::getline(fh,line));
    std::cout << "-->" << line << "<--\n";
    }
     
    David Harmon, Feb 13, 2007
    #2
    1. Advertising

  3. wrote:
    > I am pulling the following from a .txt file each into a pointer 1 at a
    > time.
    >
    >
    > west sussex
    > East sussex
    > Middlesex
    > Dorset
    >
    > etc.
    >
    > Some of the countys have spaces in them so would require two pointers.


    That is not true.

    > is there any symbol to use in the text file so both parts are read as
    > one pointer and displayed still with a space in the program.


    No. And you are thinking about this problem in completely the wrong way.
    You want to read one line at a time, not one word at a time. So stop
    thinking about special characters and start thinking about how to read
    one line at a time.

    >
    > so in txt it is: West/$Sussex
    > but still shows : West Sussex in the c++ program.
    >


    Read a line at a time, not a word at a time. Use one of the standard
    routines to read a line at a time, or write your own.

    John
     
    John Harrison, Feb 13, 2007
    #3
  4. Default User Guest

    wrote:

    > I am pulling the following from a .txt file each into a pointer 1 at a
    > time.
    >
    >
    > west sussex
    > East sussex
    > Middlesex
    > Dorset
    >
    > etc.
    >
    > Some of the countys have spaces in them so would require two pointers.
    > is there any symbol to use in the text file so both parts are read as
    > one pointer and displayed still with a space in the program.
    >
    > so in txt it is: West/$Sussex
    > but still shows : West Sussex in the c++ program.



    You don't need to do anything like that. Show us the code that doesn't,
    don't try to describe it.

    Chances are, you're reading into char buffers using >>.




    Brian
     
    Default User, Feb 13, 2007
    #4
  5. Jim Langston Guest

    <> wrote in message
    news:...
    >I am pulling the following from a .txt file each into a pointer 1 at a
    > time.
    >
    >
    > west sussex
    > East sussex
    > Middlesex
    > Dorset
    >
    > etc.
    >
    > Some of the countys have spaces in them so would require two pointers.
    > is there any symbol to use in the text file so both parts are read as
    > one pointer and displayed still with a space in the program.
    >
    > so in txt it is: West/$Sussex
    > but still shows : West Sussex in the c++ program.


    You don't put text into a pointer, you can only put text into where the
    pointer is pointing at. If you are using std::ifstream >> just use
    std::getline( std::ifstream, myvar )

    You probably want to use a std::string rather than a "pointer" though.

    std::string City;
    std::getline( MyFile, City );

    That will read an entire line into the std::string.

    MyFile >> City;
    wouly only read the first word (up to first whitespace or end of line).
     
    Jim Langston, Feb 14, 2007
    #5
  6. Guest

    On 14 Feb, 08:57, "Jim Langston" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    >
    >
    >
    >
    > >I am pulling the following from a .txt file each into a pointer 1 at a
    > > time.

    >
    > > west sussex
    > > East sussex
    > > Middlesex
    > > Dorset

    >
    > > etc.

    >
    > > Some of the countys have spaces in them so would require two pointers.
    > > is there any symbol to use in the text file so both parts are read as
    > > one pointer and displayed still with a space in the program.

    >
    > > so in txt it is: West/$Sussex
    > > but still shows : West Sussex in the c++ program.

    >
    > You don't put text into a pointer, you can only put text into where the
    > pointer is pointing at. If you are using std::ifstream >> just use
    > std::getline( std::ifstream, myvar )
    >
    > You probably want to use a std::string rather than a "pointer" though.
    >
    > std::string City;
    > std::getline( MyFile, City );
    >
    > That will read an entire line into the std::string.
    >
    > MyFile >> City;
    > wouly only read the first word (up to first whitespace or end of line).- Hide quoted text -
    >
    > - Show quoted text -



    what fi there is more data in that line in the text file. I got to
    include population etc.
     
    , Feb 15, 2007
    #6
  7. Jerry Coffin Guest

    In article <>,
    says...

    [ having been told to use std::getline to read text including spaces ]

    > what fi there is more data in that line in the text file. I got to
    > include population etc.


    You'll need something specific that separates one item on the line from
    the next. By default, the string extraction operator assumes that any
    white-space character is such a separator. When you call getline, you
    can specify a different character of your choice. For example, if
    there's a comma between the name and the population of the city, you
    could specify the comma as the separator.

    What you need is a character that you're sure will NOT occur inside of a
    data item, but WILL occur between them. If there is no such character,
    then you probably want to change how you store the data.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Feb 17, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John B. Matthews
    Replies:
    4
    Views:
    682
    John B. Matthews
    Sep 12, 2008
  2. Roedy Green
    Replies:
    3
    Views:
    641
  3. Tomasz Chmielewski

    [CSS] how can I show spaces as spaces?

    Tomasz Chmielewski, Sep 5, 2009, in forum: HTML
    Replies:
    21
    Views:
    1,635
    Jan C. Faerber
    Sep 10, 2009
  4. Tomasz Chmielewski

    how can I show spaces as spaces, part 2

    Tomasz Chmielewski, Sep 9, 2009, in forum: HTML
    Replies:
    14
    Views:
    894
    Jan C. Faerber
    Sep 10, 2009
  5. johkar
    Replies:
    2
    Views:
    2,962
    Mayeul
    Dec 10, 2009
Loading...

Share This Page