Go Down

Topic: file.print not writing to file (Read 276 times) previous topic - next topic

dandim

Jul 28, 2017, 02:55 pm Last Edit: Jul 28, 2017, 04:20 pm by dandim
I have the following code:

Code: [Select]

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

File file;  // actually use SD to open the Card and assign the file to f

void setup() {
  Serial.begin (9600);
 
  initializeSD();
 
  openFile ("test.txt");
  Serial.println (readLine());
  // close the file:
  closeFile();

  openFile("test.txt");
  delLine ();
  closeFile();

}

void loop() {
  // put your main code here, to run repeatedly:

}

void initializeSD()
{
  Serial.println("Initializing SD card...");
  pinMode(10, OUTPUT);

  if (SD.begin(10))
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
}

int writeToFile(char text[])
{
  if (file)
  {
    file.println(text);
    Serial.println("Writing to file: ");
    Serial.println(text);
    return 1;
  } else
  {
    Serial.println("Couldn't write to file");
    return 0;
  }
}

void closeFile()
{
  if (file)
  {
    file.close();
    Serial.println("File closed");
  }
}

int openFile(char filename[])
{
  file = SD.open(filename);
  if (file)
  {
    Serial.println("File opened with success!");
    return 1;
  } else
  {
    Serial.println("Error opening file...");
    return 0;
  }
}


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

void delLine()
{

uint32_t S = 0;
char ch;

  while (file.available())
  {
    ch = file.read();
   
    if (ch == '/')
    {
      S = S + 13;  // add 13 to include cr and lf on end of each line 11(+2)
      file.seek(S); //position the pointer' to where I want to overwrite   
    }
  }
 
      Serial.print ("File pointer is at: ");
      Serial.println (S);

      char del[14];
     
      // building the overwrite line
      for(uint8_t i=0;i<11;i++) del[i]='/';
      del[11]='\r'; //  (cr)
      del[12]='\n'; // newline (lf)
      del[13]='\0'; // end of string marker;

      // over write the existing data
      file.print(del); // overwrite the data
      Serial.print(del);   
  }





the test.txt file has the following lines (11 chars per line)

///////////
THISISLINE2
THISISLINE3
THISISLINE4
THISISLINE5


my issue is that the file.print command in the delLine function is not overwriting  "THISISLINE2" to /////////// which is the expected behavior.  I want this functionality so that the next time the code is ran, readLine() will return THISISLINE3 since "THISISLINE2" is now "///////////".

Read somewhere that it is much easier to do this as a means of line deletion rather than having to write to a new file all the data that you want, deleting the old file and renaming the new file to match the old file. 

I know it seems pretty basic but i can't get my head around it (Just started on c programming two days ago :(  )

any help will be greatly appreciated.  thank you!

PaulS

What is the default behavior of the File::open() function? Specifically, given just a file name, how is the file opened? What can you do with that file?

The term "read-only" should appear somewhere in your answer.

How can you expect to write to a read-only file?
The art of getting good answers lies in asking good questions.

Go Up