www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/24/11:05:27

From: kezman AT bigfoot DOT com (Kieran Farrell)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Reading from a file...
Message-ID: <3748b5f7.2470873@news.pasteur.dialix.com.au>
References: <uaB1cAATcoR3EwoG AT zaynar DOT demon DOT co DOT uk>
X-Newsreader: Forte Agent 1.5/32.452
MIME-Version: 1.0
Organization: DIALix Internet Services
Lines: 40
Date: Mon, 24 May 1999 02:24:29 GMT
NNTP-Posting-Host: 203.12.3.8
X-Complaints-To: abuse AT telstra DOT net
X-Trace: nswpull.telstra.net 927512546 203.12.3.8 (Mon, 24 May 1999 12:22:26 EST)
NNTP-Posting-Date: Mon, 24 May 1999 12:22:26 EST
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Sat, 22 May 1999 11:29:39 +0100, Unigni <unigni AT zaynar DOT demon DOT co DOT uk>
wrote:

I bet 10 bucks your using fscanf, which will only read up untill a
space or newline. I have found the best way is to load character by
character by using getc, sounds a pain but once you've written the
function you can use it forever. I'll give you a hint on what I mean,
you want to read a whole line so here goes.

char *ReadLine( FILE *fp )
{
    int i = 0;
    char ch;
    char buf[MAX]
    
    do
    {
        /* Gets a char from file */
        ch = getc(fp);
        /* Add ch to buf */
        buf[i] = ch;
        /* Increment i */
        i++;
    }while( ch != '\n' && !feof(fp) );
    /* Need to add the null terminator */
    buf[i] = '\0';
    /* If you dont want the newline in your string */
    /* use this line instead */
    /* buf[--i] = '\0'; */
    return (buf);
}

Only look at that code as an algorithm and not code as I just typed it
out, I havent compiled it to test for errors.

>I've got a data file and can read words and numbers from it (each piece
>of data is on a separate line), and want to read a whole line of text --
>however, I can only get it to read up to the first space...
> Any help will be greatly appreciated!

- Raw text -


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