|
-
May 7th, 2011, 11:11 AM
#1
Thread Starter
Hyperactive Member
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:
#include <conio.h>
#include <stdio.h>
#include <string.h>
main(){
clrscr();
FILE *rf;
char getLine[50];
char searchWord[50];
char *rWord;
printf("Enter emp ID: ");
scanf("%s",getLine);
rf = fopen("C:\\employee.txt","r");
fgets(searchWord,50,rf);
if (!feof(rf)){
rWord = strstr(searchWord,getLine);
printf (rWord);
}
getch();
return 0;
}
i use the turbo c++ compiler..
thanks in advance
-
May 7th, 2011, 01:28 PM
#2
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.
-
May 8th, 2011, 03:23 AM
#3
Thread Starter
Hyperactive Member
Re: Wrong output when retrieving string into text file
 Originally Posted by Atheist
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
-
May 8th, 2011, 03:57 AM
#4
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.
-
May 8th, 2011, 04:22 AM
#5
Thread Starter
Hyperactive Member
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:
if (!feof(rf)){ rWord = strstr(searchWord,getLine); printf ([COLOR="Red"]rWord[/COLOR]); }
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"
-
May 8th, 2011, 05:31 AM
#6
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.
-
May 8th, 2011, 05:41 AM
#7
Thread Starter
Hyperactive Member
Re: Wrong output when retrieving string into text file
 Originally Posted by Atheist
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++
-
May 8th, 2011, 06:23 AM
#8
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.
-
May 8th, 2011, 07:02 AM
#9
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.
-
May 8th, 2011, 07:15 AM
#10
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|