Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define exit_on_null(s,m) if(s == NULL) { perror(m); exit(1); }
- //print f
- char procurarstring(char *a, char *b)
- {
- if(!b[0] || !a[0])
- return 0;
- char *c;
- for(;;)
- {
- if(!*a)
- break;
- c=a;
- for(;;)
- {
- if(!*b)
- return 1;
- if(*c++ != *b++)
- break;
- }
- b-=(int)c;
- b+=(int)a;
- a++;
- }
- return 0;
- }
- int linhas(FILE *f, char *str)
- {
- char *line = NULL;
- ssize_t n=0;
- int l=0;
- while((n = getline(&line, &n, f)) != -1)
- if(procurarstring(line,str))
- l++;
- if(line) free(line);
- return l;
- }
- int main (int argc, char *argv[])
- {
- FILE *fp;
- int c;
- char *value;
- /* Usage */
- if (argc<=1)
- {
- printf("Usage: %s <file name> <flags>\n",argv[0]);
- exit(1);
- }
- fp=fopen(argv[1],"r");/*Abre ficheiro para leitura*/
- exit_on_null(fp,"Erro na abertura");
- /*Leitura linha a linha*/
- while((c=getopt(argc,argv,":l:"))!=-1)
- {
- fseek(fp,0,SEEK_SET);
- switch(c)
- {
- case 'l': value = optarg;
- break;
- }
- }
- printf("%d linhas (com a palavra %s)\n",linhas(fp,value), value);
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement