fgets in c don't return a portion of an string
Posted
by Marc
on Stack Overflow
See other posts from Stack Overflow
or by Marc
Published on 2010-06-14T19:25:01Z
Indexed on
2010/06/14
19:32 UTC
Read the original article
Hit count: 296
Hi!
I'm totally new in C, and I'm trying to do a little application that searches a string into a file, my problem is that I need to open a big file (more than 1GB) with just one line inside and fgets return me the entire file (I'm doing test with a 10KB file).
actually this is my code:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *search = argv[argc-1];
int retro = strlen(search);
int pun = 0;
int sortida;
int limit = 10;
char ara[20];
FILE *fp;
if ((fp = fopen ("SEARCHFILE", "r")) == NULL){
sortida = -1;
exit (1);
}
while(!feof(fp)){
if (fgets(ara, 20, fp) == NULL){
break;
}
//this must be a 20 bytes line, but it gets the entyre 10Kb file
printf("%s",ara);
}
sortida = 1;
if(fclose(fp) != 0){
sortida = -2;
exit (1);
}
return 0;
}
What can I do to find an string into a file?
I'v tried with GREP but it don't helps, because it returns the position:ENTIRE_STRING.
I'm open to ideas.
Thanks in advance!
© Stack Overflow or respective owner