Re: How to take in a string of any size?
From: SM Ryan (wyrmwif_at_tango-sierra-oscar-foxtrot-tango.fake.org)
Date: 08/22/04
- Next message: Richard Pennington: "Re: How to take in a string of any size?"
- Previous message: Darklight: "Re: any good"
- Maybe in reply to: zalzon: "How to take in a string of any size?"
- Next in thread: Richard Pennington: "Re: How to take in a string of any size?"
- Reply: Richard Pennington: "Re: How to take in a string of any size?"
- Reply: William L. Bahn: "Re: How to take in a string of any size?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 22 Aug 2004 15:18:23 -0000
zalzon <zalzonishappy@zalll.com> wrote:
# On Sun, 22 Aug 2004 02:07:17 -0600, William L. Bahn wrote:
#
# > You could write it to a file, counting the characters as you go,
# > and then allocate the memory and read it back in.
#
# Except you have to read the string in first before you write it to a file
# stream... which leads to the original question of how do you read a string
# of unspecified size in and store it.....
char *s = 0; int m = 0,n = 0,c; FILE *f = ...;
*s = 0;
while ((c=fgetc(f))!=EOF) {
if (n+2>=m) {m = 2*(n+2); s = realloc(s,m);}
s[n++] = c; s[n] = 0;
if (c=='\n') {s = realloc(s,n+1); break;}
}
if (s) {
....
}else {
end of file....
}
-- SM Ryan http://www.rawbw.com/~wyrmwif/ This is one wacky game show.
- Next message: Richard Pennington: "Re: How to take in a string of any size?"
- Previous message: Darklight: "Re: any good"
- Maybe in reply to: zalzon: "How to take in a string of any size?"
- Next in thread: Richard Pennington: "Re: How to take in a string of any size?"
- Reply: Richard Pennington: "Re: How to take in a string of any size?"
- Reply: William L. Bahn: "Re: How to take in a string of any size?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|