Pages: [1]   Go Down
Author Topic: Strange behaviour of sd card read()  (Read 116 times)
0 Members and 1 Guest are viewing this topic.
India
Offline Offline
Full Member
***
Karma: 0
Posts: 172
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote the following piece of code
Code:
#include <SD.h>

File myFile;

void setup()
{  pinMode(53, OUTPUT);char a;
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   SD.begin(53);
// myFile=SD.open("qq.txt",FILE_WRITE);
 //myFile.print("Sreedev.k");
 //myFile.close();
 myFile=SD.open("qq.txt");
 a=myFile.read();
 while((a=myFile.read())!=EOF)
{//to convert char to byte
 Serial.println(a);}
 Serial.println("Reading complete");
 Serial.println(myFile.read());
 myFile.close();
}

The serial monitor reading was somehow strange:

r
e
e
d
e
v
.
k
Reading complete
-1

The text file contains the string "Sreedev.k". Why didn't I get the first character on the Serial monitor?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote the following piece of code

Which doesn't compile:

Code:
core.a(main.cpp.o): In function `main':
/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/arduino/cores/arduino/main.cpp:14: undefined reference to `loop'

http://snippets-r-us.com/
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
myFile=SD.open("qq.txt");
  a=myFile.read();   // <------------ here!
  while((a=myFile.read())!=EOF)
  {//to convert char to byte
    Serial.println(a);
  }

You throw away the first byte, what do you expect?
Logged

Offline Offline
Edison Member
*
Karma: 29
Posts: 2047
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The first time you read 'a',   you don't print it.

and,  why do you read the file again after you already got the EOF ?
Logged

Pages: [1]   Go Up
Jump to: