Thread: Building a string of characters

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Building a string of characters

    Hello everybody,

    I'm a novice and I need to figure out how to build a strung using a series of characters. I want to read a file character by character and add each character to a string if the character is not white space.

    I can use gerchar() to get each character but strcat only works with strings, it won't let me use a string and a character.

    Can anyone shed some light on this for me?

    Thanks!


    Tim

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't use strcat then. Or do use strcat! Either way, it'll be fine.

    Not...
    Code:
    char buf[BUFSIZ] = {0}; /* <-- your string-to be */
    int c; /* <-- The character you read. */
    size_t s = 0; /* <-- The position in the array you're using. */
    
    repeat until at the end of the file, or the string is full
    {
        read a character
        if not whitespace
            put into array at 's' space, increment 's'
        else
            ignore it, ie: do nothing with it, don't increment 's'
    }
    Or do...
    Code:
    char buf[BUFSIZ] = {0};
    char cat[2] = {0};
    int c;
    
    repeat blah blah
        read a character into 'cat[0]'
        strcat cat onto buf
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Quote Originally Posted by quzah
    Don't use strcat then. Or do use strcat! Either way, it'll be fine.

    Not...
    Code:
    char buf[BUFSIZ] = {0}; /* <-- your string-to be */
    int c; /* <-- The character you read. */
    size_t s = 0; /* <-- The position in the array you're using. */
    
    repeat until at the end of the file, or the string is full
    {
        read a character
        if not whitespace
            put into array at 's' space, increment 's'
        else
            ignore it, ie: do nothing with it, don't increment 's'
    }
    Quzah.
    I did forget to mention, that I need to be able to read stdin. Since input from stdin can be redirected from a file, I can use the above method (..and I already have coded one that works..) to read characters until EOF. But what if stdin is coming from the command line instead? Now my exit condition for the loop has to be that getchar()=='\n'. Obviously this method won't work for a file because it will only grab the first line of the file. Is there a way to write ONE function to handle stdin from both file redirection and the command line.

    Thanks a lot!
    tim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM