Thread: Read string

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    16

    Read string

    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?

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    16
    Quote Originally Posted by 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.
    Thanks
    Can u tell me the thread? if u remember....

    But i can't use fgets() i can only use getchar()

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    16
    Quote Originally Posted by andyhunter
    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
    }
    Can i use this code without using Realloc?

  7. #7
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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:
    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;
    }
    Real nice Salem. I like it.

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Replies: 1
    Last Post: 05-30-2003, 02:31 AM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM
  5. read records fron file into a binary tree
    By Kirsten in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 02:48 PM