I want to put my text file content into a text file.
String readFileToString(const char *path)
{
File file = SD_MMC.open(path);
if (!file)
return "Failed to open file for reading";
char r = file.read();
String s;
while (file.available())
s += r;
return s;
}
I made a logging system, and I want to upload my log file's content to my webserver. I tried the method above, but there is no output to it. Can you put me in the right direction to make my text file content to a String?
UPDATE: Found the solution to the problem! Here it is!
String readFileToString(const char *path)
{
File file = SD_MMC.open(path);
if (!file)
return "Failed to open file for reading";
String s;
while (file.available())
{
char charRead = file.read();
s += charRead;
}
return s;
}
Sorry for the inconvenience I caused to people!
char r = file.read(); //get a character from the file
String s; //declare a String variable
while (file.available()) //until there are no more characters to read from the file
s += r; //append the same character to the String over and over again but don't read another one
return s;
You are not reading characters from the file in the while loop
And how would I do that?
I thought r would be the read while the loop is going.
I thought r would be the read while the loop is going.
Why would it ? It is not inside the while loop
And how would I do that?
initialise the String (or even better an array of chars, aka a C string)
open the file
while there is data available
read a character
add it to the String
end of while
close the file
do something with the String
I get ya guys, but now I have another problem. I use the following code:
String readFileToString(const char *path)
{
File file = SD_MMC.open(path);
if (!file)
return "Failed to open file for reading";
String s;
while (file.available())
s += file.read();
return s;
}
... and I get...
9165117103117115116324955325048504832495258495158534893326611197114100321141011151011164610
Is there a way to turn this into String?
and I get...
Get it from where ?
Please post a complete sketch that shows the problem