Created
December 5, 2012 04:33
-
-
Save lolgear/4212256 to your computer and use it in GitHub Desktop.
fgets normal input from stdin with stop at newline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it is the best, that i ever seen in internet. (can be changed to any symbol, if you change '\n' to )