unwanted value in file reading

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ycoerohan
    New Member
    • Jan 2010
    • 5

    unwanted value in file reading

    if i read a file whenever i read it line by line while reading i try to check it with some string by the strcmp command .This is like thats
    the string to be read from file is "rohan sharma".
    while(fgets(buf rftext,1200,fp)
    { printf("the read string is :%s"bufftext") ;
    i=strcmp(buffte xt,"rohan sharma")

    printf("the value of i :%d",i);
    }

    • the expected o/p is :
    • the read string is : rohan sharma //this o/p comes
    • the value of i : 0// this o/p is always 1.


    please try to help me in this issue if want to remove the unwanted character that comes along the string which you read from file that makes string comparision impossible.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Perhaps there is a newline at the end of bufftext.

    If nothing seems to work, then you could print out each successive byte in bufftext as a number ("%02x"). This should make it easy to see if there is something unexpected in there.

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      As donbock says there is a new line at the end of bufrftex.
      But you are comparing with "rohan sharma" which actually contain '\0' at the end of line. It may not contain '\n' at the end of line if it is the last line of the file.

      Here what you can do
      1. get the lenght of bufrftex.
      2. add '\0' at the length position. i.e
      len=strlen(bufr ftex);
      bufrftex[len]='\0';

      3. then you compare. you will get desired result.

      By the way, you can check value of bufrftex while debugging one by one.

      Regrds,
      Johny

      Comment

      Working...