Hi everyone,
check my code below.
when reading number 4 on position 50 of the file groups i get -25536 while it should be 4
if itry it with less it works
can someone pls help me explain why?
Thanks to all of you!
my code:
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(53)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
SD.remove("groups.txt");
myFile = SD.open("groups.txt",FILE_WRITE);
//myFile.seek(0);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to groups.txt...");
myFile.print(",160158000000000000000000000000000000000000000000:40000!");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening groups.txt");
}
// re-open the file for reading:
myFile = SD.open("groups.txt");
if (myFile) {
Serial.println("groups.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
SD.remove("members.txt");
myFile = SD.open("members.txt",FILE_WRITE);
//myFile.seek(0);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to members.txt...");
myFile.print("!");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening members.txt");
}
// re-open the file for reading:
myFile = SD.open("members.txt");
if (myFile) {
Serial.println("members.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
File myFile;
myFile = SD.open("groups.txt", O_RDWR);
myFile.seek( 50);
Serial.print(myFile.position());
long credit = 0;
credit = (myFile.read() - '0' ) * 10000; Serial.println(credit);
credit += (myFile.read() - '0') * 1000; Serial.println(credit);
credit += (myFile.read() - '0') * 100; Serial.println(credit);
credit += (myFile.read() - '0') * 10; Serial.println(credit);
credit += myFile.read() - '0'; Serial.println(credit);
myFile.close();
myFile = SD.open("groups.txt");
if (myFile) {
Serial.println("groups.txt");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
Serial.println(myFile.position());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening groups.txt");
}
Serial.println(credit);
}
void loop() {
// nothing happens after setup
}
and here the serial for 3 and 4:
Initializing SD card...initialization done.
Initializing SD card...initialization done.
Writing to groups.txt...done.
groups.txt:
,160158000000000000000000000000000000000000000000:30000!Writing to members.txt...done.
members.txt:
!5030000
30000
30000
30000
30000
groups.txt
,1
12
63
04
15
56
87
08
09
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
:50
351
052
053
054
055
!56
30000
Initializing SD card...initialization done.
Initializing SD card...initialization done.
Writing to groups.txt...done.
groups.txt:
,160158000000000000000000000000000000000000000000:40000!Writing to members.txt...done.
members.txt:
!50-25536
-25536
-25536
-25536
-25536
groups.txt
,1
12
63
04
15
56
87
08
09
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
:50
451
052
053
054
055
!56
-25536