Thread: excluding characters

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231

    excluding characters

    hey everyone I was wondering how do I take the \n out of a string which I received with fgets

    ex:
    Code:
    while(fgets(buffer, sizeof buffer, f))
    {
       // take the \n off of the string here
    }
    thanks in advance
    and will this work with any character, so I can comma delimit something.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Use strpos() to find the '\n', then assign '\0' to that character, if it is found.

    But if you want to tokenise a string based on commas, then strtok() would be a better bet.
    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
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by laserlight View Post
    Use strpos() to find the '\n', then assign '\0' to that character, if it is found.
    Something tells me that you have been immersed in PHP code these days.

    @ the OP:

    There are many ways to find the newline and then get rid of it. Some are more interesting than others. Have a look here for a few.
    Last edited by kermit; 07-06-2010 at 02:23 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by kermit
    Something tells me that you have been immersed in PHP code these days.
    Grr... ironically, no, I am immersed in Python these days. PHP was from long ago. But yes, it should be strchr(). Speaking of this, I am quite irritated by C-like additions to the PHP built-in functions. They should have gone for proper names from the start.
    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

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I think the name of PHP strchr() is just evil for any C programmers... Why the heck didn't they call it strstr()?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by kermit
    I think the name of PHP strchr() is just evil for any C programmers... Why the heck didn't they call it strstr()?
    As implied by my previous post, I agree that strchr() and strstr() are rather "evil" names, but at least the tradition of such crazy abbreviations is more or less consistent in the C standard library. Obviously, they cannot call strchr strstr since they are different functions... plus naming strchr strstr would be even crazier and harder to remember.
    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

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by xniinja
    thanks guys so would this work on commas
    What exactly do you want to do with those commas?
    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

  8. #8
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    delete or replace them, but my txt file is setup like this
    number,number,number,"the user types something here"
    but at the part that the user types something it might delete the commas inside, and I have to delete the quotes so I was thinking something like this


    Code:
    while(fgets(buff1,50, pfile))
    {
    while(get one character from the buff1 and put it into a variable called character){
    if(num1 != 3){
             if(character == ','){
             printf(" ")
             num1 = num1 + 1 
              }
             
             else{
             printf("%s",character)
              }
        }
            else{
            printf("%s",character)
             }
    }
       
    
    
    
    }
    This doesn't take care of the quotes (because they might enter quotes) so can you help me with that and the parts I don't know the functions for

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by xniinja
    delete or replace them, but my txt file is setup like this
    You need to be absolutely clear on what you are trying to do. Do you want to delete all of them? Replace all of them... with what?

    Quote Originally Posted by xniinja
    but at the part that the user types something it might delete the commas inside, and I have to delete the quotes so I was thinking something like this
    Inside what? What might delete the commas inside that thing? The user? The program? If you delete the quotes, how does that fix the problem?

    For example, you might say that this is some sample input:
    Code:
    number,number,number,"the user types something here"
    Maybe you want to tokenise the input based on the comma, i.e., from the above input, you would get four strings consisting of the three numbers and '"the user types something here"'. The problem is that if '"the user types something here"' was '"hello,world"', you might end up with five strings instead: the three numbers, and '"hello' and then 'world"'. So, how can such commas within double quotes be ignored in the tokenisation?
    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

  10. #10
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    I made it so the num1 (in my code) will be incrimented by 1 every time it removes a comma, so once it hits three it doesn't delete another comma, but I have to format it like this because it has to be readable by excel. (it is a .xls file)

    number,number,number,"the user types something here"
    so all I am asking is how do I do the part that I said in the above code, this part:

    Code:
    while(get one character from the buff1 and put it into a variable called character)
    thanks for the help

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You could just loop over the characters of buff1 until you encounter a null 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

  12. #12
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    Can you explain how to do that or a link to a website that can tell me how, thanks
    <------(i am a noob)

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Like this:
    Code:
    while (fgets(buff1, 50, pfile))
    {
        char *current = buff1;
        while (*current != '\0') /* " != '\0'" can actually be left out */
        {
            if (num1 != 3 && *current == ',')
            {
                putchar(' ');
                ++num1;
            }
            else
            {
                putchar(*current);
            }
            ++current;
    }
    EDIT:
    You might notice that I edited my code because the example in which I accommodated your use of the character variable did not increment the pointer. It would be simpler to just use the pointer directly, but I have edited to use another pointer in case buff1 is actually an array.
    Last edited by laserlight; 07-07-2010 at 02:05 PM.
    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

  14. #14
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    This is in a windows program so I have a lot of case values, I keep on getting this error with your code

    jump to case label
    crosses initialization of `char*current'
    how do I fix this, and thanks for all the help so far
    Last edited by xniinja; 07-07-2010 at 02:36 PM. Reason: quotes

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Learn to use functions. You can solve this problem in another way, but you might as well take this opportunity to practice writing and calling functions.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM