Thread: buffer issue

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    116

    buffer issue

    hi guys

    I have a program where I read data char by char and store them into a buffer.Every byte I read I want to put it in the buffer so I do this:

    Code:
    char buf[128];
    char c;
    //read one char
    buf+= c;
    Is this correct?
    Also when I meet a certain character I have to check the previous one.How can I achieve that?
    Thanks in advance

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    No, you need a counter variable.
    Modify something like this to include your check,
    Code:
        char buf[11];
        int i=-1;
        buf[10]=0;
        while(++i!=10)
        {
            buf[i]=(char)getchar();
        }
        puts(buf);

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest a variant of manasij7479's example using a for loop instead:
    Code:
    char buf[11] = "";
    int i;
    for (i = 0; i < 10; ++i)
    {
        buf[i] = getchar();
    }
    puts(buf);
    You may also want to cater for the possibility of getchar() returning EOF.

    Also when I meet a certain character I have to check the previous one.How can I achieve that?
    With the loop counter, you can just subtract 1 to get the previous character's index. Of course, you need to account for the very first character, upon which there is no previous character.
    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

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by manasij7479 View Post
    No, you need a counter variable.
    Modify something like this to include your check,
    Code:
        char buf[11];
        int i=-1;
        buf[10]=0;
        while(++i!=10)
        {
            buf[i]=(char)getchar();
        }
        puts(buf);

    Quote Originally Posted by manasij7479 View Post
    No, you need a counter variable.
    Modify something like this to include your check,
    Code:
        char buf[11];
        int i=-1;
        buf[10]=0;
        while(++i!=10)
        {
            buf[i]=(char)getchar();
        }
        puts(buf);
    thanks that was useful!
    what if I want after printing the contents of the buffer to restore new stuff in there?
    I can do:

    Code:
    i=-1
    all over again,but maybe the second time the data will not fill the buffer,let's say the second time I only read 5 characters.
    When I print them the 5 new ones will be print along with 5 old ones that were not copied..
    So is there a way to "delete" things in the buffer and then restore new data safely?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by quo
    So is there a way to "delete" things in the buffer and then restore new data safely?
    When you are done writing to buf, append a '\0' to null terminate the new string that buf holds.
    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
    Join Date
    Jul 2011
    Location
    Champaign, Illinois, United States
    Posts
    27
    Quote Originally Posted by quo View Post
    thanks that was useful!
    what if I want after printing the contents of the buffer to restore new stuff in there?
    I can do:

    Code:
    i=-1
    all over again,but maybe the second time the data will not fill the buffer,let's say the second time I only read 5 characters.
    When I print them the 5 new ones will be print along with 5 old ones that were not copied..
    So is there a way to "delete" things in the buffer and then restore new data safely?
    Certainly there is a way to do this. As mentioned previously '\0' is the null terminating character that you should use to end EVER c-string. You can do this one of two ways. Either you can simply put a '\0' as the last character when you write the the string again or you can use the loop we showed you above and replace every character with a '\0'. Either option will work and I don't think one is better than the other except that the first method would be a little quicker.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class for both dynamic allocated buffer and static buffer
    By TotalTurd in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2012, 09:09 PM
  2. fgets skipping, buffer issue?
    By TLW in forum C Programming
    Replies: 2
    Last Post: 02-20-2011, 08:15 PM
  3. scanf issue - character in buffer
    By demps in forum C Programming
    Replies: 2
    Last Post: 01-19-2010, 07:51 PM
  4. Buffer overflow issue.
    By caroundw5h in forum C Programming
    Replies: 3
    Last Post: 12-27-2003, 12:13 PM
  5. A quick buffer issue
    By Merld_One in forum C Programming
    Replies: 4
    Last Post: 06-12-2002, 01:04 PM