Thread: The content read from txt file is followed by a square, when using fgets() function.

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Dear all,

    In my app, I used fgets(_Name, 30, fp_t) to read a string (for instance either "NAME_ID", "A DOG" or whatever that is not specific) from a txt file and save the value to a string pointer _Name. I.e. _Name is the string pointer of which value is "NAME_ID_Square" after reading. _Square means there is a square following NAME_ID, which is shown in the debug windows in VS2005. However, this leads strcmp("NAME_ID", "NAME_ID_SQUARE") returns none-zero values, while it ought to be the same in the scenario.

    Therefore, do you have any idea about the problem? And how shall I remove the square in the pointer of which value is "NAME_ID_Square"?

    Thanks in advance,

    Regards,

    Qing
    Last edited by qingxing2005; 06-15-2008 at 04:03 AM. Reason: Better specification

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Strip off the end-of-line character (or whatever other special character it is) from the read-in input.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Quote Originally Posted by tabstop View Post
    Strip off the end-of-line character (or whatever other special character it is) from the read-in input.
    Hey tabstop,

    I knew it, while how can I do it?

    I allocated the memory before using the string pointer, i.e. _Name = (char*) malloc( sizeof(char) ) since the content in the txt file is not specific, e.g. they can be either "NAME_ID" or "THERE IS A DOG" or whatever... So how can I remove the "square" in the string pointer?

    Thanks in advance,

    Qing

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Just search the string for _Square, then null terminate it.
    Code:
    char* ptr = strstr(_Name, "_Square");
    if(ptr)
      *ptr = 0;
    
    printf("%s\n", _Name);

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Quote Originally Posted by 39ster View Post
    Just search the string for _Square, then null terminate it.
    Code:
    char* ptr = strstr(_Name, "_Square");
    if(ptr)
      *ptr = 0;
    
    printf("%s\n", _Name);
    Hey 39ster,

    Actually, the "_Square" I used in the description is not a real string, just a symbol which is shown like a "square" following the main part of the string pointer _Name. E.g. if the string in txt file is "NAME_ID", the result is "NAME_ID_ASQUAREFOLLOWED". It can be only seen from the debugger in Visual Studio. I don't know how the square is read from the txt file...

    Thanks,

    Qing

  6. #6
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Open the file in a hex editor and get the hex value for it. Then you will know which character to exclude.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Quote Originally Posted by carrotcake1029 View Post
    Open the file in a hex editor and get the hex value for it. Then you will know which character to exclude.
    Hey carrotcake1029,

    I checked it by hex editor, there are "0D0A" after "NAME_ID", by which means carriage return and new line. I think they are fine.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Try trim the line like this:

    Code:
    size_t nameLen = strlen(_Name);
    
    while(nameLen)
    {
      if(_Name[nameLen-1] < ' ')
        _Name[--nameLen] = 0;
      else break;
    }
    I think fgets leaves in \r\n. That should remove anything with an ascii value < then the space bar from the end of the line.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I think fgets leaves in \r\n.
    If you open the file in text mode, then the OS line ending should be converted into just \n (independent of the host OS).

    If you try to read a text file opened in binary mode, then you don't get this translation happening.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by 39ster View Post
    Try trim the line like this:

    Code:
    size_t nameLen = strlen(_Name);
    
    while(nameLen)
    {
      if(_Name[nameLen-1] < ' ')
        _Name[--nameLen] = 0;
      else break;
    }
    I think fgets leaves in \r\n. That should remove anything with an ascii value < then the space bar from the end of the line.
    This is not the most portable code, seeing as there's no guarnatee everything lower than ' ' is a non-visuable character.
    For the record, I believe '\r' is 0xA (10) and '\n' is 0xD (13).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Yes that's correct. It should be

    Code:
    size_t nameLen = strlen(_Name);
    
    while(nameLen)
    {
      if(!isprint(_Name[nameLen-1]))
        _Name[--nameLen] = 0;
      else break;
    }
    Last edited by 39ster; 06-16-2008 at 03:12 AM.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Elysia View Post
    For the record, I believe '\r' is 0xA (10) and '\n' is 0xD (13).
    I believe it's the other way around.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I can't check right now, but I believe '\n' is 13, if I don't remember wrong. Not sure, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    http://www.asciitable.com/

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	printf("'\\r' = &#37;d\n'\\n' = %d\n\n", '\r', '\n');
    	return 0;
    }
    Output:

    Code:
    '\r' = 13
    '\n' = 10

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I can't check right now, but I believe '\n' is 13, if I don't remember wrong. Not sure, though.
    \n is 10 and \r is 13.
    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. read mix file: fscanf fgets
    By cfdprogrammer in forum C Programming
    Replies: 1
    Last Post: 03-20-2009, 11:38 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Replies: 6
    Last Post: 05-01-2003, 02:25 AM