hi all,
how can i get an input string with an unknown size ?
tnx
m_g
hi all,
how can i get an input string with an unknown size ?
tnx
m_g
for getting strings from an stream, search out about the fgets() function.
hi vber,
tnx for your quick answer, i've took a look to fgets, but seem that
you have to set a maximum number of character, i was looking for something that doesn't care about size of input string.
char *fgets(char *BUF, int N, FILE *FP);
DESCRIPTION
Reads at most N-1 characters from FP until a newline is found. The characters including to the newline are stored in BUF. The buffer is terminated with a 0.
by
m_g
>how can i get an input string with an unknown size ?
You have to be careful about it when it comes to unknown sized strings. The most commonly used way is to dynamically allocate memory for a string and if you run out of memory, realloc it:
-PreludeCode:#include <stdio.h> #include <stdlib.h> #include <string.h> #define BASE 256 #define STEP ( BASE + 256 ) /* Or whatever suits you */ static char *readline ( FILE *f ) { size_t size, nread; char *buf, *save, *read; if ( ( buf = malloc ( BASE ) ) == NULL ) return NULL; size = nread = BASE; read = buf; if ( fgets ( read, nread, f ) == NULL ) { free ( buf ); return NULL; } while ( ( save = strchr ( read, '\n' ) ) == NULL ) { nread = STEP + 1; size += STEP; if ( ( save = realloc ( buf, size ) ) == NULL ) return buf; buf = save; read = buf + ( size - STEP - 1 ); if ( fgets ( read, nread, f ) == NULL ) { free ( buf ); return NULL; } } *save = '\0'; nread = save - buf; if ( ( save = realloc ( buf, nread + 1 ) ) == NULL ) return buf; return save; } int main ( void ) { char *p = readline ( stdin ); puts ( p ); free ( p ); return 0; }
My best code is written with the delete key.
thank you all guys!
by
m_g
Use realloc , or use a linked list
One possible solution
$echo 11223 | ./ a.outCode:#include <stdlib.h> #include <stdio.h> #include <string.h> char *addc(char *s,char c,int i) { char *tmp; if(!s) { s=malloc(1); if(!s) { perror(NULL); exit(1); } s[0]=c; return s; } tmp=realloc(s,i+1); if(tmp) { s=tmp; s[i]=c; } else { perror(NULL); exit(1); } i++; return s; } int main(void) { char *str=0,c,*tmp; int i=0; while( (c=getchar()) != EOF) { str=addc(str,c,i++); } str=addc(str,0,i); printf("str=%s",str); return 0; }
$str=11223
The one who says it cannot be done should never interrupt the one who is doing it.
@pinko_liberal
A couple of comments:
malloc'ing one byte of memory at a time is a relatively expensive way of doing business. It would be better to malloc in chunks in a real app (ie not a demo, like this one)
>>i++;
This achieves nothing, as you don't use i after incrementing it.
>>char c;
>>while( (c=getchar()) != EOF)
getchar() returns an int, not a char.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
You are right, I wonder how I could have declared c a char, reallocing in chucks is always better of course, but this was just a quick and dirty program.
Originally posted by Hammer
@pinko_liberal
A couple of comments:
malloc'ing one byte of memory at a time is a relatively expensive way of doing business. It would be better to malloc in chunks in a real app (ie not a demo, like this one)
>>i++;
This achieves nothing, as you don't use i after incrementing it.
>>char c;
>>while( (c=getchar()) != EOF)
getchar() returns an int, not a char.
The one who says it cannot be done should never interrupt the one who is doing it.