Not complete Output

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)

by the way here's my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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;
}



in line 17 change rWord to getLine.
thanks but it not work

i done working with it i change the rWord to searchWord in line 17

now my problem is it only retrieve the first line of the text file.

when i enter "A02-0002" it does not return "C plusplus,A02-0002,2" instead it only return the first line..
Your code only checks the first line. Put it in a loop
1
2
3
4
5
6
7
while(!feof(rf)){
   fgets(searchWord, 50, rf);
   if(strstr(searchWord, getLine)){
      printf(searchWord);
      break;
   }
}
(I rarely use c functions, so I might have gotten something wrong)

Your names are kind of weird. One could expect searchWord to be the word you are searching for and getLine the line you read with gets, but in fact, it is the other way around.
Last edited on
not solve the problem mate... it returns nothing.. :(
Works fine for me..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <string.h>

int main(){
 FILE *rf;
 char getLine[50];
 char searchWord[50];
 char *rWord;
 printf("Enter word to search for : ");//entered "of"
 scanf("%s",getLine);
 rf = fopen("hello.txt","r");//file contained "hello world\nhello\n cup of tea"
 while(!feof(rf)){
   fgets(searchWord, 50, rf);
   if(strstr(searchWord, getLine)){
      printf(searchWord);//I got "cup of tea"
      break;
   }
 }
 getchar();
 getchar();
 return 0;
}
thanks mate... works fine thanks..

now when i search a last line of the text file.. for ex "A02-0003" it returns 2 "Pedro Dela Cruz,A02-0003,3" like this

output
Pedro Dela Cruz,A02-0003,3
Pedro Dela Cruz,A02-0003,3

why this happen mate and also when i put else in the if statement where if not found the specific string it output " cannot find Emp ID" and it returns " cannot find Emp ID" but 4 times

i put else after the } of the if statement like this..

else {
printf("cannot find emp ID");
}
Is the break there? If it is, I have no clue why you would get it twice.
If it isn't, it's because fgets fails, reads nothing and you search the same string twice. This is the same reason why you get 4 errors for 3 lines. Try changing the loop to
1
2
3
while(fgets(/*same arguments*/)){
   //same searching and printing
}
Last edited on
thanks all done i use break after i print out the "cannot find string" and it returns only 1..

thanks mate..

how can i make all letters all caps after the user input a string..?
because if i type.. "a02-0001" it doesnt find a string.. can i do that..

sorry mate new to c++

EDIT : lol i thought i have resolved my problem when the search is not found.. when i search "A02-0002" it output the else statement.. ?

and also how can i get the string that will ends after the "," character.. for example when i want to search "A02-0003" the output will be

Pedro Dela cruz
A02-0003
3

thanks in advance mate.. :D
Last edited on
make all letters all caps
iterate throught the string and call toupper on every char.

when i search "A02-0002" it output the else statement.. ?
you mean it doesn't work? post what exactly code you have.

string that will ends after the "," character
see strtok(). http://www.cplusplus.com/reference/clibrary/cstring/strtok/
ok thanks hamster i will work with strtok and the toupper i will post if i have a problem...

by the way this is my code.. but when i put "A02-0002" it output the else statement..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <string.h>
#include <conio.h>

int main(){
 clrscr();
 FILE *rf;
 char getLine[50];
 char searchWord[50];
 char *rWord;
 printf("Enter word to search for : ");
 scanf("%s",getLine);
 rf = fopen("c:\\employee.txt","r");
 while(!feof(rf)){
   fgets(searchWord, 50, rf);
   if(strstr(searchWord, getLine)){
      printf(searchWord);
      break;
   }
   else{
    printf("cannot find String");
    break;
   }
 }
 getch();
 return 0;
}

You have two breaks there, so no matter whether the first line contains what you want or not, the others will not be processed. If you remove the break on line 22 though you'll get several lines of "cannot find string" and only then the line you wanted. Obviously, if you put something in a loop, you should expect it to be repeated. You could solve this with a bool variable, or by puting the loop in a function (that returns a bool) which would be more elegant.. Are you familiar with functions?
Topic archived. No new replies allowed.