Mail Archives: djgpp/1999/05/24/11:05:27
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 -