Thread: Read string

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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?

  3. #3
    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