Thread: unwanted value in file reading

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    13

    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".

    Code:
    while(fgets(bufrftext,1200,fp)
    { printf("the read string is :%s"bufftext");
    i=strcmp(bufftext,"rohan sharma")
    
    printf("the value of i :%d",i);
    }
    • the expected o/p is :
    • the read string is : rohan sharma
    • the value of i : 1// this o/p should always comes 0 .

    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.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    When you read input with fgets the newline will be retained if the buffer is large enough.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So get rid of it.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
      char bufftext[] = "Let It Be";
      char buff2[80];
      int match;
    
      printf("\n\n >> %s    Enter your string: \n >> ", bufftext);
    
      fgets(buff2, 80, stdin);
      buff2[strlen(buff2)-1] = '\0';
      match = strcmp(bufftext, buff2);
    
      printf("\n %s \n %s", bufftext, buff2);
    
      if(!match)
        printf("\n\n strings are a match");
      else
        printf("\n\n strings are not a match");
      
      match = getchar();
      return 0;
    }
    Last edited by Adak; 01-23-2010 at 01:13 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Adak's suggestion has an edge case problem: if it so happens that a particular line of input has 1199 characters, all of them will be stored, but the character at index 1198 will not be the newline that you want to remove (i.e., the newline will not have been stored in the first place).

    To avoid this edge case problem, use strchr() to find the newline character, and if it is found, set it to be 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

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Adak's suggestion has another problem.

    If you have a char string declared like bufftext is, it won't have a newline char in it - just a terminating zero char.

    So you have to watch out for that if you get in the habit of using this crazy way of removing a newline.

    This line of code is from the forum tutorial, btw.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    If you have a char string declared like bufftext is, it won't have a newline char in it - just a terminating zero char.
    True, but how is that a problem?
    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
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just that it isn't a robust line of code. Either a while loop with an if in it, or strchr() is stronger code.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    13
    I am facing the situation where i recieve the unwanted variable before and after the desired varaible is like that.

    "[][] rohan sharma []";

    i want to eliminate this variable.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by er.rohan88 View Post
    I am facing the situation where i recieve the unwanted variable before and after the desired varaible is like that.

    "[][] rohan sharma []";

    i want to eliminate this variable.
    OK, if you are "receiving" these unwanted char's, then you need to eliminate them. You need to keep one char after the last 'a', so C will know that this is a string, and not just a bunch of char's.

    That's the end of string char, and usually looks like '\0' <== that.

    This code will remove the last newline char which may or may not, be inside your string:

    Code:
    //You must include string.h
    
    num i;
    i = strlen(myString);
    while(i > -1) {
      if(myString[i] == '\n')
        myString[i] = '\0';
      --i;
    }
    That will take care of the right hand side of your string, after it's read in from a file.

    For the left hand side of your string, you can remove some junk char's if you want, but I'm very curious why they would be in your file, next to your name? Maybe there is junk getting into the file, that we should make sure never gets into your file?

    In order to fix your code, you're going to have to post up your code that shows this problem. We're not seeing enough to run it and see what's going on, just yet.
    Last edited by Adak; 01-25-2010 at 12:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM