Thread: Directly create dynamic string from stdin

  1. #1
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26

    Directly create dynamic string from stdin

    Is it possible to create a dynamic char array on the fly directly from stdin? I do not want to create a fixed length array before hand and then copy contents of it into a malloc created array.

    Code:
    //[1]
    char line[MAX1];
    gets(line);
    
    //[2]
    char line[MAX1];
    fgets(line,MAX1-1,stdin);
    
    //[3]
    char *str = (char *)malloc(sizeof(char)*(strlen(line)));
    I could do either [1](buffer overflow problem) or [2] and then goto [3]. But both will have a problem if the input is more than the size MAX1(use defined).

    Is it possible to do something of the effect of readLine() method of BufferedReader class in Java or the Console.readLine in .NET? Is it possible to peek into stdin and see the size of the input and then creat an array of the same exact size?
    Last edited by laughing_man; 09-14-2014 at 08:37 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you are willing to use a non-standard function, then the POSIX standard getline function will do what you want (remember to call free when done).

    If not, what you can do is to call fgets in a loop (reading into a fixed size array) and append the string read to your result string which is resized with realloc. When fgets returns a null pointer or when the string read has a newline character, you terminate the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26
    laserlight Yes!

    I have even tried these:-
    [1]first read into linked list of chars and then combine them into a single string. But then, I read somewhere that malloc and family of statement are costly to process; and what I do may leave a lot of fragmented memory(no holes but fragmented). So..
    [2]I then created a linked list of char arrays, meaning each element of the list was a constant length array of char. This reduced the calls to malloc by 1/n compared to the previous method where n is the size of the linked list element which is an array.
    Last edited by laughing_man; 09-14-2014 at 08:53 AM.

  4. #4
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26
    laserlight I am sorry, I forgot to mention that I am working on Windows XP. Is that "POSIX standard getline function " available in win32.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, I missed the requirement that stated that you "do not want to create a fixed length array before hand and then copy contents of it into a malloc created array".

    Quote Originally Posted by laughing_man
    [1]first read into linked list of chars and then combine them into a single string. But then, I read somewhere that malloc and family of statement are costly to process; and what I do may leave a lot of fragmented memory(no holes but fragmented).
    If you are going to combine them into a single string, then why not just copy the contents of the fixed length array into a "malloc created array"?

    Quote Originally Posted by laughing_man
    [2]I then created a linked list of char arrays, meaning each element of the list was a constant length array of char. This reduced the calls to malloc by 1/n compared to the previous method where n is the size of the linked list element which is an array.
    Then you have a linked list of char arrays, not a single string.

    What kind of input do you expect to read from stdin? The approach that I would reach for will have as many calls to malloc as there are calls to fgets. If the string is going to be so long that this is a concern, then you might want to double (or increased by some other factor greater than 1) the capacity of the array each time you need to expand it... or maybe you should not be reading everything at once but rather should process the input in parts.

    Quote Originally Posted by laughing_man
    I forgot to mention that I am working on Windows XP. Is that "POSIX standard getline function " available in win32.
    It depends on your compiler and the libraries that it comes with, e.g., it is possible that the MinGW port of gcc supports it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26
    laserlight BTW in the last method (linked list of char arrays), in the end I would combine them into one dynamically created char array.

    My main aim is to not worry about the size while creating a dynamically created array of char; just like in Java. Something similar to the following Java code:

    Code:
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s = br.readLine();

  7. #7
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26
    laserlight One more thing. I am using PellesC and it has that getline function you spoke about. But it seems to be similar to fgets, in that I have to mention th size there too and provide a char array to. Is there something that would just create an array and just return the pointer to me.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by laughing_man
    in the last method (linked list of char arrays), in the end I would combine them into one dynamically created char array.
    That would work. It is just a variant of what I suggested except that you complicate it by involving linked lists. Maybe the linked lists will make it more efficient, maybe they won't, but I do know that they mean that at one point you will have allocated twice as much memory as you need for the final string (just before you combine into one char array).

    Quote Originally Posted by laughing_man
    I am using PellesC and it has that getline function you spoke about. But it seems to be similar to fgets, in that I have to mention th size there too and provide a char array to. Is there something that would just create an array and just return the pointer to me.
    As I wrote in post #2, getline will do what you want. However, I assumed that you would have read the manual entry on getline.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using app config to create a dynamic menu
    By dana_cc1 in forum C# Programming
    Replies: 2
    Last Post: 04-29-2008, 11:29 PM
  2. how to create a Dynamic Array storing objects?
    By simjay in forum C++ Programming
    Replies: 6
    Last Post: 11-05-2006, 04:39 PM
  3. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  4. Create Dynamic Array of Unions
    By Duncan Booth in forum C++ Programming
    Replies: 1
    Last Post: 05-20-2002, 11:58 AM