Can't write data into variable

Hey there! I can't write text from sd card file to string variable. Can you help me, please?

String fileData;
File dataFile = SD.open("settings.txt", FILE_READ);
  while (dataFile.available()){
    fileData = fileData + dataFile.read();

  }

  Serial.print(fileData);

Thanks!

Explain the problem.

We strongly recommend to avoid using Strings with Arduino. Your program will eventually crash due to memory problems.

Test if the file was opened properly (and don’t forget to close it)
Does the program crash or just continues happily ?
Do you actually have something in the file?

Give us a full code exhibiting the issue that compiles - your problem may be elsewhere like too many open file descriptors or no more memory for your String (how big is that file that you try to suck into your String object?)

J-M-L:
Test if the file was opened properly (and don’t forget to close it)
Does the program crash or just continues happily ?
Do you actually have something in the file?

Give us a full code exhibiting the issue that compiles - your problem may be elsewhere like too many open file descriptors or no more memory for your String (how big is that file that you try to suck into your String object?)

It continuous happly

jremington:
Explain the problem.

We strongly recommend to avoid using Strings with Arduino. Your program will eventually crash due to memory problems.

But what can I use instead, to save text, can you recommend me something?

But what can I use instead, to save text, can you recommend me something?

C style strings (Note the lowercase s) which are zero terminated arrays of chars.

You still haven't described the problem. What happens, what error messages, etc.

jremington:
You still haven't described the problem. What happens, what error messages, etc.

Basically nothing happens, but it just doesn't work properlly. And Serial doesn't output the fileData variable, seems like it is empty.

As requested, post all your code.
Make sure you check that the file has opened successfully with:
if (dataFile) { ... } else { /* failed */ }

void setup(){
String fileData;
File dataFile = SD.open("settings.txt", FILE_READ);
while (dataFile.available()){
fileData = fileData + dataFile.read();

}

Serial.print(fileData);
}
void loop(){
}

andagent:
void setup(){
String fileData;
File dataFile = SD.open("settings.txt", FILE_READ);
while (dataFile.available()){
fileData = fileData + dataFile.read();

}

Serial.print(fileData);
}
void loop(){
}

  • You need to call SD.begin().
  • Check that dataFile successfully opens.
  • Print a fixed piece of text (eg. "The data: ") before the fileData so you are sure you can see something happening.

Missing the Serial.begin(xxx) as well

This would have been seen right from the start if you had answered questions in #2...

You wasted everyone’s time.

You STILL haven't answered the question. How big is the file? If you think you can read a 2 terabyte file into the 2K of memory that the Arduino has, while having a file open, and swiss-cheesing memory using the String class, think again.

PaulS:
You STILL haven't answered the question. How big is the file? If you think you can read a 2 terabyte file into the 2K of memory that the Arduino has, while having a file open, and swiss-cheesing memory using the String class, think again.

137 Bytes

So, you KNOW how big the file is. Why do you need to do dynamic memory allocation, then? Just allocate a char array big enough to hold the complete file, and fill it in one call to read().