Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Spaces in a pointer. Help

Reply
Thread Tools

Spaces in a pointer. Help

 
 
foenotes@googlemail.com
Guest
Posts: n/a
 
      02-13-2007
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.

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      02-13-2007
On 13 Feb 2007 10:09:38 -0800 in comp.lang.c++,
"(E-Mail Removed)" <(E-Mail Removed)> 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";
}


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      02-13-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-13-2007
(E-Mail Removed) 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
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      02-14-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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).


 
Reply With Quote
 
foenotes@googlemail.com
Guest
Posts: n/a
 
      02-15-2007
On 14 Feb, 08:57, "Jim Langston" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) oups.com...
>
>
>
>
>
> >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.


 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      02-17-2007
In article <(E-Mail Removed). com>,
(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSD to allow A-Z 0-9 and spaces, but not leading/trailing spaces johkar XML 2 12-10-2009 09:24 AM
[CSS] how can I show spaces as spaces? Tomasz Chmielewski HTML 21 09-10-2009 06:43 PM
how can I show spaces as spaces, part 2 Tomasz Chmielewski HTML 14 09-10-2009 03:54 PM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM



Advertisments