Go Down

Topic: file.available() and file.read()  (Read 2429 times) previous topic - next topic

pic2pic

Look at the fallowing code:

String readLine()
{
  String received = "";
  char ch;
  while (file.available())
  {
       
    ch = file.read();
   
    if (ch == '\n')
    {
      return String(received);
   
    }
    else
    {
     
      );
    received += ch;
    }
  }
  return "";
}


The code works just fine. Still, I do not understand 2 things !!!

First, inside while loop there is file.available(). While loop executes code as long as file.available() is not 0. I know that file.available() returns the number of bytes available (int) on SD card. But I saw nowhere in books that the number is decremented !!! Is it true that file.available() decrements  ???  When is it decremented ?

Second, I know that file.read() reads the next byte (or character), or -1 if none is available. But in this while loop, file.read()  reads next character or byte and it is like there is some internal pointer that increments after reading one byte or character !  Is there a way to see what is the current value of that pointer. Can I set that pointer to some wanted value ?

Thank you

BulldogLowell

When is it decremented ?

Second, I know that file.read() reads the next byte (or character), or -1 if none is available. But in this while loop, file.read()  reads next character or byte and it is like there is some internal pointer that increments after reading one byte or character !  Is there a way to see what is the current value of that pointer. Can I set that pointer to some wanted value ?

Thank you
there is a Serial buffer, containing room for a fixed number of characters.

the bytes available in that buffer is decremented as chars are removed by receiver.

as characters are removed, they are destroyed (read()) . if more characters are coming from the sender, the sender will send another byte.

available() returns true as long as there are any characters in the buffer.

PaulS

Quote
there is a Serial buffer, containing room for a fixed number of characters.

the bytes available in that buffer is decremented as chars are removed by receiver.
While that is true, that has nothing to do with reading from a file.

OP: The available() method you are using tells you how many bytes between where you last read from the file and the end of the file. Each time you read, the last read position is updated, while the end of the file stays fixed, so the number of characters available to be read goes down by however many characters you read.

Quote
But in this while loop, file.read()  reads next character or byte and it is like there is some internal pointer that increments after reading one byte or character !
Exactly.

Quote
Is there a way to see what is the current value of that pointer.
Yes. There is a position() method that returns that value. I'm not sure what good that will do you, but there it is.

Quote
Can I set that pointer to some wanted value ?
No. You can use the seek() method to make the File class do it, though.
The art of getting good answers lies in asking good questions.

pic2pic

Thank you all :)

So I understand that .read() has a pointer that .available() uses to calculate. So does not mater if I call .available() as many times as I do, what only mater is how many times I call .read() from file. Also available() is doing like subtraction every time it is called.

How come there is no explanation in the reference ? This is pretty important to know. I thought that available() and read() does not change at all.  I thought that available() just show the number of bytes in  file, and read() just reads next byte, and that all the job has to be done by the programmer to adjust program to read consecutive bytes or to decrement value of available() in the while loop.

PaulS, thank you very much for your easy to understand reply :)

PaulS

Quote
I thought that available() just show the number of bytes in  file
No. size() returns the number of bytes in the file. available() returns the number that haven't been read.
The art of getting good answers lies in asking good questions.

pic2pic


Go Up