|
-
Jul 30th, 2002, 11:59 PM
#1
Thread Starter
Hyperactive Member
Find location of string in a file
I opened a file like this:
//open file in binary read mode
ifstream file (Filepath, ios::in|ios::binary);
I need to open in in binary mode to be able to read all bytes (including null chars)
My question is. How do you find the location of a string in a file? Similar to instr in VB. I am reading an MP3 file. They have ascii tags embedded through out the file (there are null chars between the tags). If I try to put the tag from the MP3 into a *char, it will truncate after the first null char.
example: I need to find the location of "TRCK" in the file, then the next few bytes (when math is applied) will tell me how long the track tag is (in bytes). Once I know how long the tag is, I can grab it. The problem is finding where "TRCK" is.
I hope I am not being unclear.......hard to explain.
thanks
-
Jul 31st, 2002, 09:59 AM
#2
Ya ya Baby!!!Me is Back
What I whould do it to put all in a String and use the ".find" or something like that. String have some feature who search inside the string.
-
Aug 2nd, 2002, 05:08 AM
#3
Fanatic Member
make a struct or a class to handle this:
struct data {
DWORD dwLength;
BYTE * lpData;
}
then allocate the data using new or malloc or globalAlloc or something, then, use a for loop to find what ur looking for.
-
Aug 2nd, 2002, 10:00 AM
#4
Monday Morning Lunatic
Why not just keep reading until you match "TRCK"?
Hint: read until you match T, then get subsequent characters separately and attempt to match that way...if it fails, go back to looking for T.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 2nd, 2002, 11:10 AM
#5
Frenzied Member
try:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
static struct stat st;
static long position;
static char *base;
long howbig(char*); // file size
void bomb(const char*); // error-out
long find(char *,const char *); // find char string
int main(int argc,char *argv[]){
FILE *in;
char *buf;
int count=0;
if(argc!=3) bomb("Wrong number of arguments");
in=fopen(argv[1],"rb");
if (in==NULL) bomb("Error opening input file:" );
buf=calloc(1, (size_t)howbig(argv[1])+2 ); // to allow us slop at end
if(buf==NULL) bomb("Memory allocation failure");
base=buf;
if (!fread(buf,1,(size_t)st.st_size,in) )
bomb("Input file read failure:");
for(position=0; ; ){
position=find(buf,argv[2]);
if(!position) break;
printf("%s found at:%d\n",argv[2],position);
count++;
}
fclose(in);
printf("found:%d \n",count);
return 0;
}
long howbig(char *s){
if(!stat(s,&st)) return st.st_size;
bomb("Error on stat() for file:");
}
long find(char *s, const char *lookup){
char *buf;
long limit;
if(position) position++;
buf=s;
buf+=position; // start in new territory
limit=st.st_size - strlen(lookup); // max position
while(1){
while(*buf!=*lookup && position<=limit) buf++,position++;
if(position<=limit) {
if (!memcmp(buf,lookup,strlen(lookup)) ) break; // found one
else buf++,position++;
}
else return 0; // end of file
}
return position;
}
void bomb(const char *msg){
perror(msg);
exit(EXIT_FAILURE);
}
Last edited by jim mcnamara; Aug 2nd, 2002 at 04:14 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|