Go Down

Topic: Error in opening existing file in SdFat library? (Read 5901 times) previous topic - next topic

yatin

Guys I am doing one data logging project where user can delete some file and i want to rename files after he delete one to keep pointer of my program on track!
I am using arduino IDE SD library!
I didnt find any rename function in it?
Is it available in it? I do saw a thread on this but it was of 2011 so I thought I should make a new one!
Can any one could guide?
Yatin
Hard work is only solution if you are not smart!!

PaulS

Quote
I didnt find any rename function in it?
Is this a question? It looks more like a statement.

You can open a file, read the contents, and write to a new file. Then, delete the old file.

yatin

I basically want to ask as we can do in sd fat library we can rename it and SD library is cover to sd fat so can we do this in it to?
Yatin
Hard work is only solution if you are not smart!!

PaulS

Quote
as we can do in sd fat library we can rename it
So, use the sd fat (or SdFat) library.

Quote
SD library is cover to sd fat
What evidence do you base this claim on? It is not true.

yatin

Quote
SD.h will not have rename anytime soon.  In Arduino 1.0 SD.h will be about two KB larger than the new SdFat and use more RAM.

You can do anything SD.h does with SdFat since SD.h is a simple wrapper for an old version of SdFat.  The SD.h wrapper mainly changes the default behavior for open(), and uses a different syntax for the API.

SD.h opens files so sync/flush is called after every write()/print().  This is very slow but insures data is written to the SD.  Also SD.h positions files opened for write at EOF.
This is post by fat16lib over this link
Yatin
Hard work is only solution if you are not smart!!

PaulS

Perhaps you missed this part:
Quote
for an old version of SdFat.
Or this:
Quote
SD.h will not have rename anytime soon.

yatin

Didn't got you but i think may be i was wrong as I don't know as much as you! but problem stays there can we do edit in lib or something to include rename function?
Yatin
Hard work is only solution if you are not smart!!

MrAl

Hi,

I've never used this lib but if you trust the file integrity enough you can write a little program that stores your 'new' filename and redirects you to the old filename.  The new names and old names would be stored in at least one file, preferably two files one as a backup.

The file might look something like this:
MyNewName1.txt, MyOldName1.txt
MyNewName2.txt, MyOldName2.txt

when you want to change a name, you just change the first entry on each line such as:
MyNewName3.txt, MyOldName1.txt

The main program would always goes to the same file name but your main program can look up the new name instead.

You could even just number the old names:
MyNewName1.txt, Old_00000001.xxx

or whatever you like best.

A little more work yes, but doable.
If you have a huge number of files then you need to work out a complete name hashing system.

fat16lib

The six plus year old version of SdFat used in SD.h does not have rename so there is no easy way to add rename to SD.h.

I added rename to SdFat a few months after the Arduino company did their SD.h wrapper for SdFat.  It's not likely the Arduino company will upgrade SD.h.

yatin

Actually I have 400 file backup storage in sd card to store reading record by user. which will have name as 1.txt to 400.txt!
 Now if user has store 352 readings untill now and if he want to delete 102th reading the thing I want is delete 102 and rename 103 as 102 , 104 as 103....352 as 351!
Will it be difficult a lot? and is SdFat is quite different than sd.h in normal operations?
Yatin
Hard work is only solution if you are not smart!!

MrAl

Actually I have 400 file backup storage in sd card to store reading record by user. which will have name as 1.txt to 400.txt!
 Now if user has store 352 readings untill now and if he want to delete 102th reading the thing I want is delete 102 and rename 103 as 102 , 104 as 103....352 as 351!
Will it be difficult a lot? and is SdFat is quite different than sd.h in normal operations?
Well, if you cant find any other way to do it that works for you then try the file storage idea.  That's like writing your own file manager.

If you already have file names 1 to 400 then use names 00000001 to 00000400 for example.  The file then start out like:
00000001,001
00000002,002
00000003,003
00000004,004
00000005,005
down to:
00000399,399
00000400,400

Now say they want to delete file with indirect file name 003, you look for that file name 003 and then rename the others which then makes the list become:
00000001,001
00000002,002
00000003,004
00000004,005
00000005,006
down to:
00000399,400
00000400,000 //empty file here

Now you want to create and insert file 003, the list becomes:
00000001,001
00000002,002
00000003,004
00000004,005
00000005,006
down to:
00000399,400
00000400,300

This is just a brief example of one way you can do it, although you may want to think about it and do it another way.

I had to do something like this with a very old computer i had a long long time ago, in the 1980's, because the file system would not allow a file with a date above 1988!  Thus i had to start storing all the dates using a program every time a file was stored after 1988.





yatin

I switched to SdFat library as it has rename option!
But I got stucked now i need help on it!
I am able to read different files, but when i remove one file and try to read next one so that I could rename it I fail to open it here is few part of my codes related to it!
Before Setup part!
Code: [Select]
#include <SdFat.h>
const uint8_t chipSelect = SS;
SdFat SD;
SdFile myFile;

In setup
Code: [Select]
if (!SD.begin(chipSelect, SPI_HALF_SPEED)) {
    Serial.println("SD initialization failed!");
    // return;
  }
  else {
    Serial.println("SD initialization done.");
}

Problem part at void loop!
Code: [Select]
if (key == 'A') {
              sprintf(filename,"%d.txt",sd_recall_pointer);
              temppointer=sd_recall_pointer;
              if(SD.remove(filename)){
             
                Serial.print("Removing file:");
                Serial.print(filename);
                 Serial.println(" Successful!!");
                sd_recall_pointer++;
                myFile.close();
              }else{
                 Serial.print("Fail Removing file:");
                Serial.println(filename);
              }
    upif0:          if(sd_recall_pointer<=sd_file_pointer){
                 
               sprintf(filename,"%d.txt",sd_recall_pointer);
               Serial.println(filename);   
               if(!myFile.open(filename, O_READ ))
               Serial.println("error in opening next file in rename");
                sprintf(filename,"%d.txt",(sd_recall_pointer-1));
                if (!myFile.rename(SD.vwd(), filename)) {
                Serial.println("error renaming file");
                sd_recall_pointer++;
                 }
                 goto upif0;
              }
              else{
                sd_recall_pointer=temppointer;
                Serial.print("sd_recall_pointer after delte");
                 Serial.println(sd_recall_pointer);
              }
            }

And here is my Serial output on this part!
Code: [Select]
Removing file:24.txt Successful!!
25.txt
error in opening next file in rename
25.txt
error in opening next file in rename
error renaming file
26.txt
error in opening next file in rename
error renaming file
sd_recall_pointer after delte24

Can any one could point out where I am doing wrong and what shold i do?
Yatin
Hard work is only solution if you are not smart!!

PaulS

Quote
here is few part of my codes related to it!
Here is part of the answer.

You need to ...

yatin

I posted part of code because my full code is too lengthy, but still if you want it I will attach it!
Yatin
Hard work is only solution if you are not smart!!

yatin

My actual code is hard to debug as it is not written with all standards and hard to understand here is a small code representing my problem! I am attaching it below!

In theattached code char keypad() is for taking input from 4X4 keypad matrix, now the problem is I can open and close files regularly but when I delete one file I wasnt able to open next one!
Here is my serial output!
Code: [Select]
SD initialization done.
I am in memory recall mode
26
filename:26.txt
File open:26.txt
z=1
I am in myfile available
no mode found
no mode found
no mode found
no mode found
no mode found
no mode found
 I am in IR
I am good to come back
i=15
key=<
I am out of while
25
filename:25.txt
File open:25.txt
z=1
I am in myfile available
no mode found
no mode found
no mode found
no mode found
no mode found
no mode found
 I am in IR
I am good to come back
i=15
key=<
I am out of while
24
filename:24.txt
File open:24.txt
z=1
I am in myfile available
no mode found
no mode found
no mode found
no mode found
no mode found
no mode found
 I am in SV
i=3
key=A
I am out of while
Removing file:24.txt Successful!!
25.txt
25.txt
error in opening next file in rename
error renaming file
26.txt
error in opening next file in rename
26.txt
error in opening next file in rename
error renaming file
sd_recall_pointer after delte24
I am good to come back to close file
Yatin
Hard work is only solution if you are not smart!!

Go Up