Go Down

Topic: Writing to text file (Read 425 times) previous topic - next topic

SixtoD

Code: [Select]
  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




adwsystems

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

?

SixtoD


Lucario448

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

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

SixtoD

#4
Sep 09, 2018, 08:52 am Last Edit: Sep 09, 2018, 08:53 am by SixtoD
when I do this... no output... even the Serial.write(c) 
Code: [Select]

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();

}

SixtoD

#5
Sep 09, 2018, 10:39 am Last Edit: Sep 09, 2018, 10:40 am by SixtoD
but if I do this, i got tthis




Code: [Select]

  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

adwsystems

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)

Lucario448

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:
Code: [Select]
tempFile = SD.open("temp.txt", O_WRITE | O_CREATE | O_TRUNC);

SixtoD

hello sir @Lucario448
when i use this code:
I bet it's because temp.txt already existed in the second test run. Try opening it this way:
Code: [Select]
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
Code: [Select]

#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
}



adwsystems


Lucario448

#10
Sep 13, 2018, 06:26 pm Last Edit: Sep 13, 2018, 06:27 pm by Lucario448
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]

SixtoD

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

Go Up