www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/03/31/07:33:20

From: "John Harrison" <jahhaj AT bigfoot 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: 55
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
Message-ID: <YOjx6.6567$MZ2.1113022@news2-win.server.ntlworld.com>
Date: Sat, 31 Mar 2001 13:12:23 +0100
NNTP-Posting-Host: 213.104.61.8
X-Complaints-To: abuse AT ntlworld DOT com
X-Trace: news2-win.server.ntlworld.com 986040632 213.104.61.8 (Sat, 31 Mar 2001 13:10:32 BST)
NNTP-Posting-Date: Sat, 31 Mar 2001 13:10:32 BST
Organization: ntlworld News Service
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"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.
>

Why do you need to manipulate the file position?

Despite what others have told you about seekg, it is an error to pass
anything to seekg that isn't either the end of file, the start of file or a
position previously returned from tellg UNLESS you are dealing with a binary
file. You want to use a text file so seekg is going to be of limited use to
you (unless you want to write non-portable code).

But as I said why manipulate the file position, why not just read the first
128 lines and then 'read' the first 128 bytes of those lines, e.g.

ifstream maze("filename");
for (int i = 0; i < 128; ++i)
{
    string line;
    getline(is, line);
    string row(line, 0, 128);    // first 128 chars
    // do domething with row
}

john

> 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