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

This is a discussion on The content read from txt file is followed by a square, when using fgets() function. within the C Programming forums, part of the General Programming Boards category; Dear all, In my app, I used fgets(_Name, 30, fp_t) to read a string (for instance either "NAME_ID", "A DOG" ...

  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,185
    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
    398
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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,211
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21