SD card read/write

Hey there friends! I need your help in SD card!

I have a text file named party_a.txt in my sdcard. read/write scketch goes fine.

My objective: Need to read the value stored in party_a.txt then increment it by value one and store it back on the same text file.

I tried all sorts of things but no luck!

These are what i tried:

#include <SPI.h>
#include <SD.h>

File myFile;
char a;

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ; 
  }
  Serial.print("Initializing SD card...");
  pinMode(53, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  myFile = SD.open("party_a.txt");
  if (myFile) {
    Serial.print("party_a.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      a=myFile.read();
   // Serial.print(a);
    }
    Serial.println(a);
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening part_a.txt");
  }
}

void loop()
{
}

The output:

Initializing SD card…initialization done.
party_a.txt:

And for this code:

myFile = SD.open("party_a.txt");
  if (myFile) {
    Serial.print("party_a.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      a=myFile.read();
    Serial.print(a);
    }
 //   Serial.println(a);
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening part_a.txt");
  }

The output is

Initializing SD card…initialization done.
party_a.txt:12

which is fine…
I just want to increment the value stored in a text file… Please help!

In this snippet,

while (myFile.available()) { a=myFile.read(); }

How to use the variable 'a' after reading the value stored in sd card text file??? Inside while loop, it works fine... But not outside that loop!

Use .parseInt() to read a number. Use .position(0) to go back to the beginning of the file. Use .print(number+1) to write the number back.

Something like:

  if (myFile) {
    Serial.print("party_a.txt: ");
    int number = myFile.parseInt();
    Serial.print(number);
    myFile.position(0);
    myFile.print(number+1);
    myFile.close();
    }

For this program below

#include <SPI.h>
#include <SD.h>

File myFile;

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ; 
  }
  Serial.print("Initializing SD card...");
  pinMode(53, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  myFile = SD.open("party_a.txt");
  if (myFile) {
  Serial.print("party_a.txt:");
  int number=myFile.parseInt();
  Serial.print(number);
  myFile.position(0);
  myFile.print(number+1);
  myFile.close();
  } else {
    Serial.println("error opening part_a.txt");
  }
}

void loop()
{
}

This is what i get:

Arduino: 1.6.1 (Windows 8.1), Board: “Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)”

ReadWrite.ino: In function ‘void setup()’:

ReadWrite.ino:45:20: error: no matching function for call to ‘File::position(int)’

ReadWrite.ino:45:20: note: candidate is:

In file included from ReadWrite.ino:22:0:

C:\Program Files (x86)\Arduino\libraries\SD\src/SD.h:42:12: note: uint32_t File::position()

uint32_t position();

^

C:\Program Files (x86)\Arduino\libraries\SD\src/SD.h:42:12: note: candidate expects 0 arguments, 1 provided

Error compiling.

Definitely no idea on what this error is all about!!!

To go back to the beginning of the file, I think the command you want is file.seek(0). http://arduino.cc/en/Reference/FileSeek

The error you are seeing is is that file.position() takes no input parameter, but returns the current location.

candidate expects 0 arguments, 1 provided

cattledog: To go back to the beginning of the file, I think the command you want is file.seek(0). http://arduino.cc/en/Reference/FileSeek

The error you are seeing is is that file.position() takes no input parameter, but returns the current location.

Just now tried this!!! Doesn't work and no change! Why is Johnwasser' code not working? Haven't anyone tried this yet?

Show what you did and let's not worry about John's snippet, okay?

As now, does what you have compile?

GoForSmoke:
Show what you did and let’s not worry about John’s snippet, okay?

As now, does what you have compile?

Alright, i am repeating again.
This is the Code:

#include <SPI.h>
#include <SD.h>

File myFile;

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ;
  }


  Serial.print("Initializing SD card...");
  pinMode(53, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  myFile = SD.open("party_a.txt");
  if (myFile) {
    Serial.println("party_a.txt:");
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
  } else {
    Serial.println("error opening party_a.txt");
  }
}

void loop()
{
}

with its’ output

Initializing SD card…initialization done.
party_a.txt:
12

After John’ suggestion, i changed the code as below:

  if (myFile) {
  Serial.print("party_a.txt:");
  int number = myFile.parseInt();
  Serial.print(number);
  myFile.position(0);
  myFile.print(number+1);
  myFile.close();

and i got strange errors as follows:

Arduino: 1.6.1 (Windows 8.1), Board: “Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)”

ReadWrite1.ino: In function ‘void setup()’:

ReadWrite1.ino:45:20: error: no matching function for call to ‘File::position(int)’

ReadWrite1.ino:45:20: note: candidate is:

In file included from ReadWrite1.ino:22:0:

C:\Program Files (x86)\Arduino\libraries\SD\src/SD.h:42:12: note: uint32_t File::position()

uint32_t position();

^

C:\Program Files (x86)\Arduino\libraries\SD\src/SD.h:42:12: note: candidate expects 0 arguments, 1 provided

Error compiling.

After that, as cattledog suggested, i changed the code again:

 myFile = SD.open("party_a.txt");
  if (myFile) {
  Serial.print("party_a.txt:");
  long number = myFile.parseInt();
  Serial.print(number);
  myFile.seek(0);
  myFile.print(number+1);
  myFile.close();
  } else {
    Serial.println("error opening part_a.txt");
  }

and i got same result as before:

Initializing SD card…initialization done.
party_a.txt:12

Now i’m out of ideas!!! :disappointed_relieved: :disappointed_relieved: :disappointed_relieved:

No worries, a simple mistake. If i am understanding your post correctly you changed

if (myFile) { Serial.println("party_a.txt:"); while (myFile.available()) { Serial.write(myFile.read()); } myFile.close();

to

if (myFile) { Serial.print("party_a.txt:"); int number = myFile.parseInt(); Serial.print(number); myFile.position(0); myFile.print(number+1); myFile.close();

The obvious problem here is when you made the change you took out the part that reads the information from the sdwhile (myFile.available()) { Serial.write(myFile.read());

Look at the documentation for file.open - it defaults to read only.

wildbill: Look at the documentation for file.open - it defaults to read only.

I also tried this myFile = SD.open("party_a.txt",FILE_WRITE); but no difference!!!

I changed some pin numbers to run this sketch on a uno, but it should provide a template for how to read a numerical value stored as a character string beginning in file byte 0, convert it to a number, increment by 1, and store it back as a character string starting at byte 0.

#include <SPI.h>
#include <SD.h>

File myFile;

void setup()
{
  Serial.begin(9600);

  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }

  Serial.println("initialization done.");
  SD.remove("party_a.txt");//get rid of old example
}

void loop()
{
  myFile = SD.open("party_a.txt", FILE_WRITE);//open for reading and writing
  if (myFile) {
    myFile.seek(0);//go to beginning instead of end of file
    long number = myFile.parseInt();//read a number from the file
    Serial.println("stored index for party_a.txt:"); 
    Serial.println(number);
    myFile.seek(0);//go back to first position
    Serial.println("incrementing index by 1");
    myFile.print(number + 1);//overwrite begining of file
    myFile.seek(0);//go back to first position, verify increment
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    Serial.println();
    myFile.close();
  } 
  else {
    Serial.println("error opening part_a.txt");
  }
  delay(2000);
}

You are the one!!! :sunglasses: Thanks Sir... And sorry for late reply as i was busy with report preparation... Thanks for everything...

This is the output which is the one i expected...

Initializing SD card...stored index for party_a.txt: 1 incrementing index by 1 2 Initializing SD card...stored index for party_a.txt: 2 incrementing index by 1 3 Initializing SD card...stored index for party_a.txt: 3 incrementing index by 1 4