Chan's FatFS read problem!

OP #680357
Hello my friends,

I'm really confused about the Chan's FatFS read-function!
I'm able to write and do loggings etc... but reading a file greater than 
1 kB doesn't work! I can read up to 1 kB (not including 1 kB).

The problem is I see only 0 (zero) above 1 kB!
My buffer is 2 kB, so it's not the bufferproblem.
1
int main()
2
{
3
  FATFS Fs;
4
  FIL FileObject;
5
  char data[2048];
6
  const char* FileName = "0:Basic.bin";  
7
  unsigned int ByteToRead = sizeof(data);
8
  unsigned int ByteRead;
9
  unsigned char SD = 0;
10
  
11
  memset(data, 0, sizeof(data));
12
  
13
  if( f_mount(SD, &Fs) != FR_OK )
14
    return 0;
15
  
16
  if(f_open(&FileObject, FileName, FA_OPEN_EXISTING|FA_READ) != FR_OK)
17
    return 0;
18

19
  FRESULT res;
20
  int f, e; f = e = 0;
21
  while(1)
22
  {    
23
    res = f_read(&FileObject, data, ByteToRead, &ByteRead);
24
    
25
    if(res == FR_OK)
26
    {
27
      f++;     //just to check number of cycles
28
      if(ByteRead == 0) break;
29
      e++;
30
    }
31
  }
32

33
  return 0;
34
}
Guest #680423
I am missing two things in your code:
disk_initialize ()  How does it work without this ?
f_close()

I would try to read 2048 bytes in four 512 byte steps.
Only to see if f_read() has a problem reading more than
one sector.
OP #680429
holger wrote:
> I am missing two things in your code:
> disk_initialize ()  How does it work without this ?
> f_close()
>
> I would try to read 2048 bytes in four 512 byte steps.
> Only to see if f_read() has a problem reading more than
> one sector.

Holger,

disk_initialize() is done when you call f_open().

f_close? hmmm I thought this was only needed when you write to SD, for 
flushing the data. But maybe it also works for reading! I'll try that.

So f_read() reads only 512 bytes max.?? I thought as long as the buffer 
can be filled.

Thank you.
Guest #680441
>But what if I want to
>know how big the file is, so I know when to stop copying to the buffer?

Ask FileObject.fsize ?

Reply

A question of your own? This thread is over 19 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.