About EOF
Posted: Fri Apr 20, 2007 9:38 pm
Hi
how can i take a string as input till EOF??
how can i take a string as input till EOF??
Code: Select all
string s;
while (cin >> s) {
//statement
}
Code: Select all
char s[255];
while (cin.getline(s,255)) {
//statement
}
Code: Select all
char s[255];
while (gets(s)) {
//statement
}
so I would using something else. It's better using fgets like this:/tmp/ccSVOabh.o: In function `main':
gets.c:(.text+0x1e): warning: the `gets' function is dangerous and should not be used.
Code: Select all
char k[200];
while(fgets(k, 200, stdin)) {
printf("%s", k);
}
Code: Select all
char *string;, array[100];
string=array;
while(scanf("%s", string)!=EOF) {
printf("%s\n", string);
}
This code is equally bad (dangerous, whatever) for exactly the same reason as gets(): you don't specify size of buffer to scanf, so a large enough input will cause buffer overflow.KaDeG wrote:Code: Select all
char *string;, array[100]; string=array; while(scanf("%s", string)!=EOF) { printf("%s\n", string); }