Finding text in file ?

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Guys,

I want to try finding a text in file :

             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

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You already have the position here:

line[0]+i

except of course that this is meaningless. The address of the i'th position of line is:

line + i

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One day you'll get this. A file is a stream. You keep reading "next character" until you see 'T' you then read the next one and see if it is 'E' and so on until you have seen the whole of "TEXT" in which case you have found what you were looking for.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
One day you'll get this. A file is a stream. You keep reading "next character" until you see 'T' you then read the next one and see if it is 'E' and so on until you have seen the whole of "TEXT" in which case you have found what you were looking for.

after I get "TEXT", how can I retrieve the address of "TEXT" ?

It could be somewhere inside 512 bytes ? any functions ?
thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what about counting how many chars you have read before you get your 'T' ;)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Exactly as Sparrow says.

Or in real Posix/stdio filing you could also call ftell() to get the file pointer position (set it with fseek()). I can't remember if FatFs has an equivalent.

I'll probably say this until I'm blue in the face but the way to learn about filing is to do it in C test programs on the PC, not using FatFs on an AVR. When you understand the concepts THEN see how that relates to the use of FatFs. Don't try to learn the basics while also learning FatFs.

As your question is really a general one about filing in C I'll bet you find tons of useful questions / answers about things like finding text in files on Stack Overflow.

EDIT: as I guess one might have predicted, FatFs does, indeed, have its own equivalent of ftell():

http://elm-chan.org/fsw/ff/en/te...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Again, if you are doing it the way you imply above you have something like:

char line[512];
int i = 0;
for (; i < 512; i++)
{
    if (strncmp("TEXT", (line+i), 4))
    {
        break;
    }
}

Then after this, the start of the string is at element "i", so its address is (line + i), since that is the address that you told strncmp to start at when it found the string. But it would be easier to use strstr() which returns a pointer to the first occurrence of the search string.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

probably :

i=512;
if (strncmp("TEXT", (line[0]+i), 4))
				{
usart_pstr("TEXT FOUND WITH other method");NEXT_LINE;
usart_pstr("TEXT LOCATION:");NEXT_LINE;
usart_transmit(f_tell(&Fil));
}

thanks
?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

probably :

No, not "probably" at all. What on earth do you think:

(line[0]+i)

is going to deliver when i=512?

I'm assuming (though you didn't bother to show it) that you are fread()'ing 512 bytes at a time into line[]. So say the data you just read starts "Hello\nWorld" then line[0] is 'H' which has an ASCII value 72. So (line[0]+i) is going to be 72+512 = 584. So the thing you are strcmp()ing against is memory location 584 which in part is an address constructed form the letter 'H' that happens to be in the data.

This does not make any sense in any known computing language!

I'm guessing you possibly meant:

if (strncmp("TEXT", (&line[0]+i), 4)) 

the use of & (address-of) here is very important. The &line[0] now delivers the address of 'H' in memory not the value 'H' itself. But why use "&line[0]" when, in C, "line" alone will give you the address of the start of the array. So I'm guessing you perhaps meant:

if (strncmp("TEXT", line + i, 4)) 

But again this makes no sense either. You defines line[] as:

char line[512]; 

So if you use line + i where i=512 as the address to compare against you are actually addressing the byte just beyond the END of the line[] array. What you probably really want is to vary i between 0 and 511 and compare each location in line with the text. Something like:

for (i=0; i<512; i++) {
  if (strncmp("TEXT", line+i, 4)) {
    ...

Well, lookey here, step back to the post immediately above yours. What did Steve tell you?!?

Is there some reason why you deliberately ignore the advice people are giving you and instead persevere with your own meaningless nonsense?

Just one thing though. I fear Steve may have made a small error. The fact is that strncmp() will return 0 when the strings match. So you need to use:

  if (!strncmp("TEXT", (line+i), 4)) 

or

  if (strncmp("TEXT", (line+i), 4) == 0) 

so it enters the conditional block on a 0 result.

But again this makes no real sense. Why use fread() to read 512 bytes at a time if the file you are looking at is a text file. The chances are it contains something like

Mary had a little lamb\n
Its fleece was white as snow\n
And everywhere that Mary went\n
...

And you have an array called line[]. So why not read a LINE at a time into that array (unless you are really looking for things across line end boundaries??). As such I would use fgets() not fread(). When you use fgets() you use something like:

char line[512];
while(!f_eof(&fil) {
  f_gets(line, 512, &fil);
  for (i=0; i < strlen(line); i++) {
    if (strncmp("TEXT", line + i, 4) == 0) {
       //found
    }
  }
}

But I'll come back to my previous point that it is MUCh easier to learn this stuff on a PC...

uid23021@lxl0060u:~$ cat poem.txt
Mary had a little lamb
Its fleece was white as snow
And everywhere that Mary went
The lamb was sure to go

uid23021@lxl0060u:~$ cat poem.c
#include 
#include 

FILE * fin;
char line[512];

int main(void) {
  int i;
  fin = fopen("poem.txt", "rt");
  if (fin) {
    while(!feof(fin)) {
       fgets(line, 512, fin);
       for(i = 0; i < strlen(line); i++) {
         if (strncmp("Mary", line+i, 4) == 0) {
            printf("Mary found at offset %d in line %s", i, line);
         }
       }
    }
    fclose(fin);
  }
}
uid23021@lxl0060u:~$ gcc poem.c -g -o poem
uid23021@lxl0060u:~$ ./poem
Mary found at offset 0 in line Mary had a little lamb
Mary found at offset 20 in line And everywhere that Mary went

That took about 3-4 minutes on a PC - how long is it going to take you to do the same on an AVR?

As you can see the filing functions in are very like FatFs. Or to put it the correct way, the filing functions in FatFs are very like the functions in stdio. So use stdio on a PC (where things like printf() for the test output are trivial) and when you have the algorithm/structure worked out then transfer the same to AVR and the FatFs dialect of the same thing.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Clawson, I tried your method :

void read_512()
{
	 /*TESTING READING 512 bytes begin*/
	 
	 UINT i,bw,size;
	 FRESULT fr;    /* FatFs return code */
	 BYTE line[512],size_char; /* Line buffer */
	 
	 if (f_open(&Fil, "poem.txt", FA_READ) == FR_OK)
	 {
		 f_read(&Fil, line, sizeof line, &bw);
		 usart_pstr("\n\nReading file 512 bytes \n\n");
		
		 	if (strncmp("ID3", line, 3) == 0) 
			 {
				 char fil_buffer[20];
				 usart_pstr("ID3 FOUND!");
				 NEXT_LINE;
			//if (strncmp("TIT2", (line[0]+i), 4))
			for(i = 0; i < strlen(line); i++)
			  {	
				if (strncmp("Mary", line+i, 4))
				{
					usart_pstr("Mary FOUND WITH other method");NEXT_LINE;
					usart_pstr("Mary offset:");NEXT_LINE;
					
					
					sprintf_P( fil_buffer, PSTR("offset : u "), i );
					usart_pstr(fil_buffer);NEXT_LINE;
					
					usart_pstr("Mary line:");NEXT_LINE;
					sprintf_P( fil_buffer, PSTR("line : u "), line );
					usart_pstr(fil_buffer);NEXT_LINE;

					 					
					 NEXT_LINE;
				}
			  }
				 
				NEXT_LINE;
				 
				
			 }
			 
		 usart_pstr(line);
		
		 f_close(&Fil);
		 usart_pstr("\n\nFinished reading 512 bytes\n\n");
	 }
	 /*TESTING READING 512 bytes end*/	  

}

The result on UARTis different, what do you reckon? Thanks
Reading file 512 bytes

ID3 FOUND!

Mary FOUND WITH other method

Mary offset:

offset : 0

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 1

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 2

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 3

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 5

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 6

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 7

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 8

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 9

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 10

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 11

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 12

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 13

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 14

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 15

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 16

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 17

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 18

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 19

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 20

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 21

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 22

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 23

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 24

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 25

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 26

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 27

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 28

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 29

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 30

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 31

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 32

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 33

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 34

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 35

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 36

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 37

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 38

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 39

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 40

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 41

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 42

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 43

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 44

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 45

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 46

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 47

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 48

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 49

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 50

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 51

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 52

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 53

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 54

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 55

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 56

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 57

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 58

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 59

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 60

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 61

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 62

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 63

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 64

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 65

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 66

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 67

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 68

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 69

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 70

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 71

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 72

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 73

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 74

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 75

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 76

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 77

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 79

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 80

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 81

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 82

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 83

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 84

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 85

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 86

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 87

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 88

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 89

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 90

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 91

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 92

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 93

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 94

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 95

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 96

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 97

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 98

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 99

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 100

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 101

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 102

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 103

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 104

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 105

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 106

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 107

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 108

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 109

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 110

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 111

Mary line:

line : 2776

Mary FOUND WITH other method

Mary offset:

offset : 112

Mary line:

line : 2776

ID3 Mary had a little lamb
Its fleece was white as snow
And everywhere that Mary went
The lamb was sure to go

Finished reading 512 bytes

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I nearly wore out my finger scrolling down that post. I dare say anyone else on a tablet or phone will feel the same.

Surely you need to do some debugging.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
         for(i = 0; i < strlen(line); i++) 
           {  

Do investigate strstr() as an alternative. Don't write anything yourself that already is available in the standard library.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Fri. Mar 28, 2014 - 07:39 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
            if (strncmp("Mary", line+i, 4)) 

You need to read the documentation of strncmp().

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
You need to read the documentation of strncmp().

Sometimes I despair!

There's clearly a very basic issue with an ability to read plain English here.

For one thing I suggested doing this on a PC: ignored. Then I suggested using fgets not fread: ignored. Then I pointed out that strncmp returns 0 on a match so the conditional needs inversion: ignored.

Truly I despair.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Having followed this thread (as well as others from the OP), i must confess that for some people, planting roses might be a much more appropriate way to spend their leisure time....

And i do not even try to imagine that the OP actually is in the process of designing a product that one day might hit the markets.

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sorry,
I missed this line :

if (strncmp("Mary", line+i, 4) == 0)
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, I have not been following this thread.

However, surely you can just use strstr() to determine the location of any string. This is a single C statement!

If you want every occurrence, it is simply put inside a loop.

@Bianchi,
Most tasks can be solved with a handful of C statements. Just choose the appropriate library functions.

Explain what you want to do. If it takes you more than 10 lines, ask for advice.

Most importantly, read any replies to your questions very carefully.

Consider your 'potential' readers. If you irritate them, you are less likely to receive help.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You define the variable line as an array of char

    BYTE line[512],size_char; /* Line buffer */

But then you try to print is as if it is an unsigned integer:

               sprintf_P( fil_buffer, PSTR("line : u "), line );

Sit down. Read through your code very carefully. Scrutinize everything, double-check that all stuf that should match actually matches. This will save you a lot of frustration, large amounts of time and things might be learned.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]