www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/03/31/05:47:31

From: "Alex P." <roboto11 AT hotmail DOT com>
Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c++,comp.os.msdos.djgpp
References: <nj7x6.782$ym1 DOT 6435 AT nntp1 DOT chello DOT se>
Subject: Re: Need some help with reading from files
Lines: 72
X-Newsreader: Microsoft Outlook Express 5.00.2919.6700
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6700
Message-ID: <2tix6.164752$tP3.2535930@news1.rdc1.bc.home.com>
Date: Sat, 31 Mar 2001 10:38:54 GMT
NNTP-Posting-Host: 24.113.131.218
X-Complaints-To: abuse AT home DOT net
X-Trace: news1.rdc1.bc.home.com 986035134 24.113.131.218 (Sat, 31 Mar 2001 02:38:54 PST)
NNTP-Posting-Date: Sat, 31 Mar 2001 02:38:54 PST
Organization: Excite AT Home - The Leader in Broadband http://home.com/faster
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019