[General] Determining a location of text in file ?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Visit site
Activity points
9,442
Guys,

I want to try finding a text in file :
Code:
             char line[512];
             i=512;
	  if (strncmp("TEXT", (line[0]+i), 4))

After I find the text, how can I determine the location of "TEXT" ?

For example it's located on 0x100, how can I know that ?
f_read and then fseek ? or something else, I haven't caught the idea.....

Any thoughts ?

Thanks
 

I don't see why you would use strncmp() here. I suggest strstr(line, "TEXT").

line[0]+i isn't a char pointer, by the way. It's the sum of the character code line[0] and i. Using it in the function call causes an address error exception. You would write line + i or &line.
 


Code C - [expand]
1
2
3
int location = 0;
 
location = strstr(line, "TEXT");



location contains the address of first 'T' of string "TEXT".
 

int rc = 0;
.
.
.
.
main
{
rc = find_text(512); // rc is the location of TEXT in the file, if rc < 0 then no such TEXT
.
.
.
}

int find_text(int nbr_bytes)
{
int location = 0, line_counter = 0;

read nbr_bytes bytes from file into line

while (something was read)
{
offset = strstr(line, "TEXT")
if (offset is NOT null)
return (line_counter * nbr_bytes + difference of pointers offset and line);
++line_counter;
if ( EOF ) return -1; // not really needed
read next nbr_bytes bytes into line;

}
return -1;
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…