473,395 Members | 1,653 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,395 software developers and data experts.

get the "space" when reading a file

254 100+
i read a file, and the file contains this:
Expand|Select|Wrap|Line Numbers
  1. +-------------------+
  2. |                     |
  3. +-------------------+
  4.  
when i read it into array, and then display it out to cout exactly the same as display in text file, but i failed to do so. it give me this :
Expand|Select|Wrap|Line Numbers
  1. +++++++++++++++++++++++
  2. +++++++++++++++++++++++
  3. +++++++++++++++++++++++
  4.  
here is my code(it is the constructor ):
Expand|Select|Wrap|Line Numbers
  1. Terrain(char* filename =" " ): width(25) , height(11), ter_array(width, height){
  2.  
  3.                 ifstream in(filename);
  4.                 if(!in){
  5.                     cout << "Cannot open terrain file." << endl;
  6.                 }
  7.  
  8.                 while(in >> piece){
  9.                     for(unsigned y = 0; y < height; y++){
  10.                         for(unsigned x = 0; x < width; x++){
  11.                             ter_array.Put(x,y,piece);
  12.                         }
  13.                     }
  14.                 }
  15.                 in.close();
  16.             };                // should i put ; in the end of this constructor?
  17.  
note the comment line.

any one help...pls
I want the space inside the text file, store in array, so that it display the exactly the same to cout as in text file.

thanks in advance.
Nick
Dec 13 '06 #1
4 2055
DeMan
1,806 1GB
The nested loops concern me, especially the while....

The exact logic I'm not sure of, but I think basically, you always populate your array with all of 1 character...This means the array is full of (copies) of the current character at any time. because + is your last character, thi is what fills the array after you have read from the file.

Since you know the height & width, you could instead do something like:

Expand|Select|Wrap|Line Numbers
  1. Terrain(char* filename =" " ): width(25) , height(11), ter_array(width, height){
  2.  
  3.                 ifstream in(filename);
  4.                 if(!in){
  5.                     cout << "Cannot open terrain file." << endl;
  6.                 }
  7.  
  8.  
  9.                     for(unsigned y = 0; y < height; y++){
  10.                         for(unsigned x = 0; x < width; x++){
  11.                                                                 in>>piece;
  12.                             ter_array.Put(x,y,piece);
  13.                         }
  14.                     }
  15.  
  16.                 in.close();
  17.             };                // should i put ; in the end of this constructor?
  18.  
As far as I know too many semi-colons are never a problem, so even if you don't need the last one, there is little harm in including it
Dec 14 '06 #2
nickyeng
254 100+
thanks but still, it cannot "get" whitespace between the characters.
the original text file:
Expand|Select|Wrap|Line Numbers
  1. +-----------------------+
  2.  |                         |
  3.  |           +-+--+ +-+
  4.  |               |         |
  5.  | +----+ +-------+    |
  6.  | |      |                 |
  7.  | |  +---+     +        |
  8.  | |               |        |
  9.  | +-----+  +--+ +    |
  10.  |                   |     |
  11. +------------------+----+
  12.  
the whitespace inside the terrain, i want to saved them as one character with ' '.
Any method to get all the whitespace?
Dec 14 '06 #3
DeMan
1,806 1GB
you could try using getchar() et al to get input....
Dec 14 '06 #4
nickyeng
254 100+
getchar() fails.

but get() works.

thanks for the tip.

:)
Dec 14 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Xah Lee | last post by:
how to represent the unicode "em space" in regex? e.g. i want do something like this: fracture=re.split(r'\342371*\|\342371*',myline,re.U) Xah xah@xahlee.org ∑ http://xahlee.org/
2
by: Petr Jakes | last post by:
Hi, I am trying to set-up communication to the coin change-giver from my Linux box using the Python code. The change giver uses MDB (Multi Drop Bus) serial protocol to communicate with the...
0
by: serge | last post by:
I have an XML file with an XSD file. When i am reading the XML file and putting its content into a DataSet I notice that the field that has a single space character is being converted to a field...
1
by: Steve | last post by:
I am suddenly getting the obnoxious error C1060 when I try to compile my project. I have tried adjusting the /Zm switch as suggested by the help documents, but this is a project setting and not a...
2
by: Kannan | last post by:
In section 7.19.6.1 on ISO C specification, it says about a format character "space". When and how we will use this "space" format? I mean is there a usage, for e.g. like printf("%space",...); ? ...
2
by: Lloyd Dupont | last post by:
I am "defining" an XML schema. In fact I'm not really writing any schema but I am writing an XML writer and reader for my data. It's all custom XML and I would like it to be XML tool friendly. ...
2
by: Sound | last post by:
Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Add a field without giving any name and save the...
1
by: pippyn | last post by:
I'm programming for a CE device with reduced key board, no mouse, and no touch screen. As such, I have to tab from button to button to get the focus on the button i want. Then to invoke the button...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.