Skip to content

Instantly share code, notes, and snippets.

@lolgear
Created December 5, 2012 04:33
Show Gist options
  • Save lolgear/4212256 to your computer and use it in GitHub Desktop.
Save lolgear/4212256 to your computer and use it in GitHub Desktop.
fgets normal input from stdin with stop at newline
char* inputStringWithStopChar(char stopChar){
int n = 5;
int size = n;
char* const_str = (char*)malloc(n*sizeof(char));
char* substring = (char*)malloc((n+n)*sizeof(char));
char*p;
while((fgets(const_str,n,stdin)!=NULL)&&(strchr(const_str,stopChar)==NULL)){
strcat(substring,const_str);
size += n;
substring = (char*)realloc(substring,size*sizeof(char));
}
strcat(substring,const_str);
size += n;
substring = (char*)realloc(substring,size*sizeof(char));
/*
printf("<%s> is \n",const_str);
printf("%s is \n",substring);
printf("%d is \n",size);
*/
if ((p=strchr(substring,stopChar))!=NULL){
p[0]='\0';
}
if(feof(stdin)){
changeToFull();
}
return substring;
}
char* inputString(){
int n = 5;
int size = n;
char* const_str = (char*)malloc(n*sizeof(char));
char* substring = (char*)malloc((n+n)*sizeof(char));
char*p;
while((fgets(const_str,n,stdin)!=NULL)&&(strchr(const_str,'\n')==NULL)){
strcat(substring,const_str);
size += n;
substring = (char*)realloc(substring,size*sizeof(char));
}
strcat(substring,const_str);
size += n;
substring = (char*)realloc(substring,size*sizeof(char));
/*
printf("<%s> is \n",const_str);
printf("%s is \n",substring);
printf("%d is \n",size);
*/
if ((p=strchr(substring,'\n'))!=NULL){
p[0]='\0';
}
if(feof(stdin)){
changeToFull();
}
return substring;
}
@lolgear
Copy link
Author

lolgear commented Dec 5, 2012

it is the best, that i ever seen in internet. (can be changed to any symbol, if you change '\n' to )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment