473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Spaces in a pointer. Help

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 '07 #1
6 1339
On 13 Feb 2007 10:09:38 -0800 in comp.lang.c++,
"fo******@googlemail.com" <fo******@googlemail.comwrote,
>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";
}
Feb 13 '07 #2
fo******@googlemail.com 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
Feb 13 '07 #3
fo******@googlemail.com 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
Feb 13 '07 #4
<fo******@googlemail.comwrote in message
news:11**********************@v45g2000cwv.googlegr 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).
Feb 14 '07 #5
On 14 Feb, 08:57, "Jim Langston" <tazmas...@rocketmail.comwrote:
<foeno...@googlemail.comwrote in message

news:11**********************@v45g2000cwv.googlegr 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.
Feb 15 '07 #6
In article <11*********************@a75g2000cwd.googlegroups. com>,
fo******@googlemail.com 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.
Feb 17 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
15
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string character by character and copying onto another...
3
by: ayan4u | last post by:
well i need to deal with white spaces in charecter arrays... with static arrays its fine.. char ss; cin.getline(ss, sizeof ss); .... //deals with white spaces
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.