Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void contexto(char*ficheiro,char*palavra){
  5. FILE *fp;
  6. char ch;
  7. int wordcount=0;
  8. fp = fopen(ficheiro,"r");
  9. int k=1;
  10. while ((ch=getc(fp)) != EOF) {
  11. if (ch == ' ' || ch == '\n') { ++wordcount; }
  12. }
  13. fclose(fp);
  14. char p1[100],p2[100],p3[100];
  15. fp = fopen(ficheiro, "r");
  16. for(int i=0;i<wordcount;i++){
  17. fscanf(fp,"%s",p1);
  18. if(strcmp(p1,palavra)==0){
  19. ch=getc(fp);
  20. if(ch != '\n'){
  21. fscanf(fp,"%s",p2);
  22. printf("Linha %d: %s %s %s\n",k,p3,p1,p2);
  23. }
  24. else{
  25. printf("Linha %d: %s %s\n",k,p3,p1);
  26. ungetc(ch,fp);
  27. }
  28. }
  29. strcpy(p3,p1);
  30. ch=getc(fp);
  31. if(ch == '\n')
  32. k++;
  33. ch=getc(fp);
  34. if(ch == '\n')
  35. k++;
  36. ungetc(ch,fp);
  37. }
  38. }
  39.  
  40. int main(){
  41. contexto("texto.txt","se");
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement