Writing to text file

  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    
// skipped/blocked the first Line OPEN
      bool firstLine = true;
while (myFile.available()) {
  int c = myFile.read();
  if (firstLine) {
    if (c == '\n') {
      firstLine = false;
    }
  } else {
    Serial.write(c);
    
 }

how can I write the "c" in my other text file which is temp.txt

Open myOtherFile = SD.open("temp.txt") and use myOtherFile.write(c);

?

Can I open two File?

You can: open as many files as your RAM allows.

You can't: open the same file in multiple instances.

when I do this... no output... even the Serial.write(c)

myFile = SD.open("test.txt");
  myFile = SD.open("temp.txt", FILE_WRITE);
  if (myFile) {
    Serial.println("test.txt:");
    
// skipped/blocked the first Line OPEN
      bool firstLine = true;
while (myFile.available()) {
  int c = myFile.read();
  if (firstLine) {
    if (c == '\n') {
      firstLine = false;
    }
  } else {
   Serial.write(c);
   myFile.write(c); 
 }
 
 myFile.close();

}

but if I do this, i got tthis

  myFile = SD.open("test.txt");
 // myFile = SD.open("temp.txt", FILE_WRITE);
 tempFile = SD.open("temp.txt", FILE_WRITE); 
  if (myFile) {
    Serial.println("test.txt:");
    
// skipped/blocked the first Line OPEN
      bool firstLine = true;
while (myFile.available()) {
  int c = myFile.read();
  if (firstLine) {
    if (c == '\n') {
      firstLine = false;
    }
  } else {
    Serial.write(c);
    tempFile.write(c);
  }
  
}
myFile.close();
tempFile.close();

output:
22222
33333
44444
55555
22222
33333
44444
55555

example expected output:
22222
33333
44444
55555

It is quite hard to say what is wrong.

Bad formatting in the code? (P.S. try using the auto format function in the IDE)

Mismatched braces (there are more open than closed)? (P.S. please post the complete program, or at least the complete function in question)

SixtoD:
output:
22222
33333
44444
55555
22222
33333
44444
55555

example expected output:
22222
33333
44444
55555

I bet it's because temp.txt already existed in the second test run. Try opening it this way:

tempFile = SD.open("temp.txt", O_WRITE | O_CREATE | O_TRUNC);

hello sir @Lucario448
when i use this code:

Lucario448:
I bet it's because temp.txt already existed in the second test run. Try opening it this way:

tempFile = SD.open("temp.txt", O_WRITE | O_CREATE | O_TRUNC);

im getting this error msg: 'O_CREATE' was not declared in this scope
what library should i use to use O_CREATE?

here's my whole code

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

File myFile;
File tempFile;
char cr;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  tempFile = SD.open("temp.txt", O_WRITE | O_CREATE | O_TRUNC);
 //tempFile = SD.open("temp.txt", FILE_WRITE);
 
 
  if (myFile) {
    Serial.println("test.txt:");
    
// skipped/blocked the first Line OPEN
      bool firstLine = true;
while (myFile.available()) {
  int c = myFile.read();
  if (firstLine) {
    if (c == '\n') {
      firstLine = false;
    }
  } else {
 
  //Serial.write(c);
  tempFile.write(c);
  }

}
myFile.close();
tempFile.close();
 

    
// skipped/blocked the first Line CLOSED

  } 
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

try O_CREAT?

Yeah thanks!

I didn't remember how that definition is named; obviously if I recalled it wrong, a compilation error happens.

Now I'm pretty sure it's O_CREAT, because how the other options are named: O_[the first five characters of the word in uppercase]

thank you very much Sirs...
big help for me...