Mail Archives: djgpp/2001/03/31/05:47:31
First you do a seekg(), then you read. Something like this (note: no
error-checking is done here):
#include <fstream>
#include <iostream>
using namespace std;
#ifdef _WIN32
#define EOL_LEN 2
#else
#define EOL_LEN 1
#endif
int main()
{
fstream fstr( "file.txt" );
int rows = 128;
int cols = 128;
int row_len = cols + EOL_LEN;
char ch = 0;
for ( int ii = 0; ii < rows*row_len; ii += row_len )
{
fstr.seekg( ii );
fstr >> ch;
// do something with the data here...
cout << "ch = " << ch << endl;
}
return 0;
}
Note that since you're using a text file, you have to take into account the
platform-dependant end-of-line characters (two characters for DOS/Win, one
for UNIX etc.). That's what you see the EOL_LEN do in the above code. Also,
notice that this code reads the first character of each row since ii starts
at zero.
HTH,
Alex
"Daniel Eliasson" <danieleliasson AT chello DOT se> wrote in message
news:nj7x6.782$ym1 DOT 6435 AT nntp1 DOT chello DOT se...
> Hi, I'm currently working on a small game utilizing a 2d array of int's.
> That is, grid[128][128].
>
> What I want to do is read a map from a text file. The format of maps is
that
> every character in the map file is a certain type of int that will be
stored
> in the grid.....
> My problem is, if I want to read the first 128 characters on the first 128
> rows in the text file, how do I get the iostreams to do this?
>
> I've checked several C++ books, but they never tell me how to manipulate
the
> place it reads from. Any code/pseudo-code would be greatly appreciated.
>
> Thanks in advance,
> --
> // Daniel Eliasson
>
> PS. I hope you don't mind the crosspost, just trying to maximise my odds
of
> getting help.
> -------------------
> I believe information should be free.
> www.gnu.org
>
>
- Raw text -