Spaces in a pointer. Help

F

foenotes

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.
 
D

David Harmon

On 13 Feb 2007 10:09:38 -0800 in comp.lang.c++,
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";
}
 
J

John Harrison

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
 
D

Default User

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
 
J

Jim Langston

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).
 
F

foenotes

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.
 
J

Jerry Coffin

[ 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top