Public Group

#### Archived

This topic is now archived and is closed to further replies.

# fstream in out

This topic is 5192 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to read several numbers from a text file into my program, then I want to put a space between every charachter, but i cant seem to do this. Here is the code I have, it doesnt work though.
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <windows.h>

using namespace std;

int main()
{

ifstream fin;
ofstream fout;
int space[30][30];

fin.open("mazeOut.txt");

if(fin.fail())
{
cout << "\nError\n";
system("pause");
exit(0);
}

for(int r = 0; r < 30; ++r)
{
for(int c = 0; c < 30; ++c)
{
fin >> space[c][r];
cout << space[c][r];
cout << " ";

}
}
cout << "\n";

fout.open("mazeOut.txt");
for(int r = 0; r < 30; ++r)
{
for(int c = 0; c < 30; ++c)
{
fout << space[c][r];
fout << " ";
cout << space[c][r];
cout << " ";

}
}
system("pause");
return 0;
}

[edited by - betrayer_of_code on March 7, 2004 4:43:06 PM]

##### Share on other sites
When you say "doesn''t work" what exactly is the problem?

##### Share on other sites
It turns this:
1111111111111111111111111111111301000000101001111010000000011000000101111000110110100100

1111100001010101000010001000110110110100000100011000100110001111101000001010010000010000

0001110000101011000100101010101101101011000110010000000100001001101000010100001010001000

1000111000001001100010010000011100011100011000010100000010100101111010101000001010000001

1110100110010000011000110010010111110110100000110000111000011110000111000110100000000010

0000011101110101000001000001011000000001100000001001110000000110001101101100010001000010

0000000001011110000010000000101100111000011001000010000000100010000011111000000001100001

0100000100001110111010011010001000110101110111001011101000010000111000000111001100000100

1010000010100101100110010100000000100011001101110001100100100000110110000011101000111010

0001101110010001011001000001110001001001011000111000111000010011000000000000211111111111

11111111111111111111

into thiswith a lot more #'s)

9240914 13172936 327684 4259904 8192124 12124344 16056564 2291696 35652128 50397953 34603520 50397953 50463008 2069518680 2012550901 1802135912 -471670304 119 4010016 2009196879 16777216 1 0 2292304 0 0 12 2293496 2360896 2359672 9372029 13304010 458758 4390978 8323198 12255418 16187638 256 35652128 50397953 34603777 50397953 117835012 2138996092 17 1869507948 -404298268 3998072 3998072 4010024 2292152 2009196879 70 2011780608 2147225628 24 32 2012750850 0 27 538443920 13435084 589832 4522052 8462508 12386492 16318712 2291184 35652128 50397953 35652353 50397953 185207048 -2088599168 4000184 1936879984 -336926232 2012553347 0 2048 4294108 1 560 2293004 2147226658 8 19070180 2012697888 -2129971616 512 538714137 13566158 720906 4653126 26353690 12517566 16449786 256 35652128 50397953 35652353 50397953 252579084 -2021227132 3997696 2004252020 -269554196 8 0 2009196879 3415768 3416160 70 2012750850 2147226654 2292800
SORRY, for the huge post, ill delete most of it

[edited by - betrayer_of_code on March 7, 2004 4:46:34 PM]

##### Share on other sites
Try to change int space[30][30]; to char space[30][30]; and see if that helps.

##### Share on other sites
I tried that, when i do it, i get a whole bunch of boxes and % signs....

##### Share on other sites
ok easy(i think):

change

int space[30][30];

to

unsigned int space[30][30];

nope, didnt help

##### Share on other sites
hmmmmm maybe it wants byte?

try

unsigned char space[30][30];

##### Share on other sites
fin >> x; Where x is of type int will read an entire number from the input, not just one character. To read a single character you can use the get() function of ifstream, since there is no operator >> that takes a char&.
Also: There is no need to create an array of data first, unless you plan to do something else with it later on.

##### Share on other sites
quote:
Original post by twanvl
fin >> x; Where x is of type int will read an entire number from the input, not just one character. To read a single character you can use the get() function of ifstream, since there is no operator >> that takes a char&.
Also: There is no need to create an array of data first, unless you plan to do something else with it later on.

My thoughts:
1.I agree with using get(). However, I think you should read in everything as a character versus an int, and then cast the char into an int to get the ASCII key code.
2.Then, you can test each element to see if it is a space, and delete any spaces that were accidentley put in while typing up MazeOut.txt. That is not required, but will help.
3.You could also have the program require an argument of the filename so it would work with any file.
4.Instead of an array, a vector would probably be more useful. Then, you can expand it to the size needed for the program.
5. Finnaly, you can keep a counter of what element you are at in the vector, and then add a space element between each entry, probably using a for loop to do this.
Would these suggestions help everything work?

Scott Simontis
e-mail:ageofscott@NOSPAM.comcast.net
AIM:ssimontis

• 16
• 9
• 13
• 41
• 15