• Advertisement

Archived

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

fstream in out

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
When you say "doesn''t work" what exactly is the problem?

Share this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
ok easy(i think):

change

int space[30][30];

to

unsigned int space[30][30];

Share this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
I dont even know what a vector is, so that wont help at all. Im using this as part of antoher program so I know exactly the size of the file. I really dont even need this for my program, I just got side tracked...I wanted to see which of two sets of code I wrote would solve a maze in less moves, so I wrote this. I have a program that generates a random maze, and then two different programs that solve a 30x30 maze, the only problem is that between each # in the maze there has to be a space for the maze solvers to read it, so I made this program.

!!!!Now when I use fin.get(spacer) and then output that to a file, alls i get is spaces!!!!! I dont get it!

new source

#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <windows.h>

using namespace std;

int main()
{

ifstream fin;
ofstream fout;
char spacer;

fin.open("MazeOut.txt");
fout.open("maze.txt");

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

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



while(fin.get(spacer))
{
fout << spacer << " ";
}


fin.close();
fout.close();

system("pause");
return 0;
}


[edited by - betrayer_of_code on March 7, 2004 6:05:30 PM]

[edited by - betrayer_of_code on March 7, 2004 6:24:46 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Betrayer_of_Code
I dont even know what a vector is, so that wont help at all. Im using this as part of antoher program so I know exactly the size of the file. I really dont even need this for my program, I just got side tracked...I wanted to see which of two sets of code I wrote would solve a maze in less moves, so I wrote this. I have a program that generates a random maze, and then two different programs that solve a 30x30 maze, the only problem is that between each # in the maze there has to be a space for the maze solvers to read it, so I made this program.

and what is the syntax for the get() function?

[edited by - betrayer_of_code on March 7, 2004 5:57:21 PM]


msdn is your friend

Share this post


Link to post
Share on other sites
Ok i got it, mostly. Im getting 2 wierd charachters at the beggining of the output thats really thiroughing me off...

ÿ þ these two charachters show up at the begginning of any output that I make with this program and I dont know why, ill repost the code above

[edited by - betrayer_of_code on March 7, 2004 6:23:58 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Betrayer_of_Code
Ok i got it, mostly. Im getting 2 wierd charachters at the beggining of the output thats really thiroughing me off...

ÿ þ these two charachters show up at the begginning of any output that I make with this program and I dont know why, ill repost the code above

[edited by - betrayer_of_code on March 7, 2004 6:23:58 PM]


hmmmm tried char* spacer [][]; instead of char spacer; ?

Share this post


Link to post
Share on other sites
try this as your copy code

#include <algorithm.h> // stl library


...

copy (istream_iterator <char> (fileIn), istream_iterator <char>(), ostream_iterator <char> (fileOut, " "));



edit: remeber to use the std namespace

[edited by - hewhay on March 7, 2004 11:28:29 PM]

Share this post


Link to post
Share on other sites

  • Advertisement