Results 1 to 10 of 10

Thread: Wrong output when retrieving string into text file

  1. #1

    Thread Starter
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Wrong output when retrieving string into text file

    hello guys im having a problem.. i have a text file name employee.txt and it is the construction of the text file..

    Juan Dela cruz,A02-0001,1
    C plusplus,A02-0002,2
    Pedro Dela Cruz,A02-0003,3

    now i have done coding when the user input the emp id and then it will search through the file..when i search A02-0001

    it only outputs

    A02-0001,1 (no Juan Dela Cruz)

    and also when i search the A02-0002 nothing happens..
    by the way here's my code.

    Turbo c++ Code:
    1. #include <conio.h>
    2. #include <stdio.h>
    3. #include <string.h>
    4.  
    5. main(){
    6.  clrscr();
    7.  FILE *rf;
    8.  char getLine[50];
    9.  char searchWord[50];
    10.  char *rWord;
    11.  printf("Enter emp ID: ");
    12.  scanf("%s",getLine);
    13.  rf = fopen("C:\\employee.txt","r");
    14.  fgets(searchWord,50,rf);
    15.  if (!feof(rf)){
    16.      rWord = strstr(searchWord,getLine);
    17.      printf (rWord);
    18.  }
    19.  getch();
    20.  return 0;
    21. }

    i use the turbo c++ compiler..

    thanks in advance

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Wrong output when retrieving string into text file

    The reason why it only outputs "A02-0001,1" and not "Juan Dela Cruz", is because you are printing rWord, which is a pointer to the location in the string where strstr finds a match. If you search for "A02-0001,1", and strstr finds it, there is no reason why you would also have "Juan Dela Cruz" included in rWord.

    And the reason why nothing happens when you search for "A02-0002,2" is because you only read the first 50 characters of the file and I suspect that this will not include the entire second line. This would cause rWord to be NULL and you would be printing NULL. I'm not sure how printf behaves when passing a null pointer to it but in worst cases it could lead to segmentation faults.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Re: Wrong output when retrieving string into text file

    Quote Originally Posted by Atheist View Post
    The reason why it only outputs "A02-0001,1" and not "Juan Dela Cruz", is because you are printing rWord, which is a pointer to the location in the string where strstr finds a match. If you search for "A02-0001,1", and strstr finds it, there is no reason why you would also have "Juan Dela Cruz" included in rWord.

    And the reason why nothing happens when you search for "A02-0002,2" is because you only read the first 50 characters of the file and I suspect that this will not include the entire second line. This would cause rWord to be NULL and you would be printing NULL. I'm not sure how printf behaves when passing a null pointer to it but in worst cases it could lead to segmentation faults.
    thanks for the explanation mate now how do i do to make "juan Dela Cruz" include in the search

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Wrong output when retrieving string into text file

    You could solve both your issues by reading line by line from the text file (using fgets), and performing the strstr operation on the individual line. If there is a match, simply printf the line and stop searching.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Re: Wrong output when retrieving string into text file

    thanks i found the solution to that..

    i replace the red color text to searchWord

    vb Code:
    1. if (!feof(rf)){
    2.      rWord = strstr(searchWord,getLine);
    3.      printf ([COLOR="Red"]rWord[/COLOR]);
    4.  }

    now my only problem is when i search the "A02-0002" it returns only the "Juan Dela cruz,A02-0001,1" not this "C plusplus,A02-0002,2"

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Wrong output when retrieving string into text file

    you are always printing out the first line of the textfile, no matter if the search fails or not. You need to handle the returnvalue of the strstr function. If it fails you need to read the next line from the file and try again.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Re: Wrong output when retrieving string into text file

    Quote Originally Posted by Atheist View Post
    you are always printing out the first line of the textfile, no matter if the search fails or not. You need to handle the returnvalue of the strstr function. If it fails you need to read the next line from the file and try again.
    how can i do that mate..? sorry new to c++

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Wrong output when retrieving string into text file

    im not by a computer at the moment but you will need to loop until EOF (using feof), read a line from the text file (using fgets), perform the strstr operation. If strstr returns a non-NULL pointer, print the current line and break out of the loop. The search will have completed successfully.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Wrong output when retrieving string into text file

    i never realized that you mention in your first post that you are using c++. The code youve posted is C. Look up iostream for the c++ way ofhandling input and output.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10

    Thread Starter
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Re: Wrong output when retrieving string into text file

    thanks atheist yes i use a Turbo c++ compiler..

    the problem is solved
    i loop while not end of file and get the string in the text file using fgets as you said and perform the strstr..

    thanks mate..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width