I'm searching for a code to read a string in input. The length of the scring in unknow.
I've to use getchar and malloc or calloc......
it's like gets() but i don't find it where i can find?
I'm searching for a code to read a string in input. The length of the scring in unknow.
I've to use getchar and malloc or calloc......
it's like gets() but i don't find it where i can find?
You would pass this code the char* you created in your other function. See if you can figure out how it works, it is pretty basic.
Code:void getLine(char** message) { int ch, len = 0; while((ch = getchar()) != '\n') { //check for end of line *message = realloc(*message, len+1); //increase inputString by 1 char if(*message == NULL) { puts("Error allocating memory"); exit(1); } (*message)[len++] = ch; } (*message)[len] = '\0'; //add end token }
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem
> it's like gets() but i don't find it where i can find?
gets() doesn't allow you to read an unknown length of input.
It just does it, and so in the process is likely to trash an unknown amount of memory in the process.
If you really want to provide such a feature, then use fgets() and malloc()
Several ways of doing this were posted not too long ago.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
ThanksOriginally Posted by Salem
Can u tell me the thread? if u remember....
But i can't use fgets() i can only use getchar()
Gee, I wonder how hard "Search: Key Word(s): fgets, malloc ; Posts Made By: Salem " can possibly get....
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Can i use this code without using Realloc?Originally Posted by andyhunter
Well yes you can, however the code Salem referred you to does the job a lot nicer I might say. So without further adue here is Salem's code slightly modified:
Real nice Salem. I like it.Code:#include <stdio.h> #include <stdlib.h> #include <string.h> char* getLine(int); int main(void) { char* myInput; printf("Enter a line of text:"); myInput = getLine(0); printf("%s\n", myInput); return 0; } char* getLine(int size) { char *s; int myChar; myChar= getchar(); if( myChar == '\n' ) { s = malloc( size + 1 ); s[size] = '\0'; } else { s = getLine( size + 1 ); s[size] = myChar; } return s; }
If you have questions ask.
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem