Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- void contexto(char*ficheiro,char*palavra){
- FILE *fp;
- char ch;
- int wordcount=0;
- fp = fopen(ficheiro,"r");
- int k=1;
- while ((ch=getc(fp)) != EOF) {
- if (ch == ' ' || ch == '\n') { ++wordcount; }
- }
- fclose(fp);
- char p1[100],p2[100],p3[100];
- fp = fopen(ficheiro, "r");
- for(int i=0;i<wordcount;i++){
- fscanf(fp,"%s",p1);
- if(strcmp(p1,palavra)==0){
- ch=getc(fp);
- if(ch != '\n'){
- fscanf(fp,"%s",p2);
- printf("Linha %d: %s %s %s\n",k,p3,p1,p2);
- }
- else{
- printf("Linha %d: %s %s\n",k,p3,p1);
- ungetc(ch,fp);
- }
- }
- strcpy(p3,p1);
- ch=getc(fp);
- if(ch == '\n')
- k++;
- ch=getc(fp);
- if(ch == '\n')
- k++;
- ungetc(ch,fp);
- }
- }
- int main(){
- contexto("texto.txt","se");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement