Hi everyone,
not quite familiar with embedded programming, pointers etc. Trying my best during lockdown.
I am trying to use the function readFile from the examples but changing it a bit to "parse" the file and create objects that will run in RAM and access them more quicker(does that even make sense?).
I want to create an object from the text file. Text file was written by writeFile function of the examples on the format:
30charactersmaximum&30charactersmaximum&0&0
I was thinking initially to parse it as string and split it by & (lazy .net framework hobbyist i am ) but I am trying to change my logic now and learn something new.
Im lost in the middle of nowhere.. I need to "construct" something like
MyClass mc("30charactersmaximum","30charactersmaximum",0,1);
from the data of the text file.
im lost here at the moment if you could please help me:
void readFile2(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if(!file || file.isDirectory()){
Serial.println("- failed to open file for reading");
return;
}
int i=0;
int myvar[100];
Serial.println("- read from file:");
while(file.available()){
myvar[i]=file.read();
//Serial.println(myvar[i]); //prints numbers, Decimal (bytes) of the text in the txt
//Serial.write(myvar[i]); //prints exactly whats i want
if(myvar[i]=='&'){
//split ??
}
i=i+1;
}
file.close();
}
chris700:
I am trying to use the function readFile from the examples but changing it a bit to "parse" the file and create objects that will run in RAM and access them more quicker(does that even make sense?).
Honestly, not by itself. What processor? The AVR can't run code from RAM.
aarg:
Honestly, not by itself. What processor? The AVR can't run code from RAM.
im so sorry, edited title. ESP32
edited: dont mean to do some fancy stuff with ram etc. just to have an instance of an object so i can access it imediatelly and not start parsing when i need it. i believe it will be running in RAM thats why i wrote to RAM .if its not RAM i dont mind. just to have the instance done
int numBytes = strlen(c)+1 + strlen(d)+1 + 2; // string lengths + 2 For e & t
uint8_t myBuff = malloc(numBytes); // Now we have the RAM. Time to fill it.
strcpy((char*)myBuff,c); // We stuff in the first string.
char* trace; // A temporary pointer..
trace = (char*)(&(myBuff[strlen(c)+1])); // Point it to the byte past the last string.
strcpy(trace,d); // Stuff in the next string.
myBuff[numBytes-2] = e; // Stuff in the e byte.
myBuff[numBytes-1] = t; // Stuff in the t byte.
Now go write this buffer to the SD drive.
To read it back?
char* c;
char* d;
int numChars = strlen((char*)readBuff); // We got a buffer from the SD drive. Find the string length of c.
c = malloc(numChars +1); // Allocate a new c.
strcpy(c,(char*)readBuff); // FIll the new c.
char* trace = &(readBuff[numChars+1]); // Point at the next string.
int traceChars = strlen(trace); // How long is the next string..
d = malloc(traceChars +1); // Allocate a new d.
strcpy(d,trace); // Fill the new d.
uint8_t e = (uint8_t)trace[traceChars]; // Grab the e.
uint8_t t = (uint8_t)trace[traceChars+1]; // Grab the t.
myClass anObj = new myClass(c,d,e,t);
free(c);
free(d);
Will this work? Donno' I just typed it out on the screen here. 99.9% chance there will be compile errors. But it'll hopefully give you an idea of a possible approach.
Thanks for your reply however i do not understand anything. since im hobbyist and too noob i would appreciate if you could help me handle it with my way so i can keep my project going even on a not so "correct" way and then i can learn all those intresting stuff that you posted. thank you
dont laugh at my code lol, im trying to merge two char arrays with seperator & and i tried the commands of merging etc and i was getting random errors of char * and char and this and the other. Im trying to send this complete array to LittleFS to write the file.(before i was doing everything string and then c.str() to send it)
what worked is my random code:
enabled, type and write_file are declared as int . so when i print them in Serial i get rectangulars instead of numbers..
How could i fix that? Easy fix would be to make everything char since char worked for cryptoid and dname but any other fix? thank you
EDIT:
it worked after i added +48 in every integer. for example
lala[j]=write_file+48;
I thought in chars we write bytes, not ascii... isnt that correct?
chris700:
I thought in chars we write bytes, not ascii... isnt that correct?
Not really sure what you mean here, but the rectangles were because you tried to print character one (ascii) and character zero. They're not printable characters but when you added 48, that brought them up to character '0' and character '1' that are printable.
wildbill:
Not really sure what you mean here, but the rectangles were because you tried to print character one (ascii) and character zero. They're not printable characters but when you added 48, that brought them up to character '0' and character '1' that are printable.
If you could please help me understand some basics so i can continue without flooding here the forum:
We give to LittleFS input char for filename and char for the message to write to the text file
Those chars are not bits bytes (i mean they are not numbers that represent letters) but actual letters. A B C D etc
The text that its written by LittleFS is actual text A B C D that people can read and not 2314 234 443 345 663 345
Dont get me wrong, i always used to use just Strings in .net framework so im really struggling here..
I think you're confusing yourself. A byte is 8 bits and can hold whatever you like from 0 to 255. Ascii is just a convention that we will use a byte containing 48 to represent '0' and 65 to mean 'A'.
Serial.print knows when you pass it a char or an array of char that you want the Ascii representation, just as it knows if you pass it an int that you want the number it represents instead.
wildbill:
I think you're confusing yourself. A byte is 8 bits and can hold whatever you like from 0 to 255. Ascii is just a convention that we will use a byte containing 48 to represent '0' and 65 to mean 'A'.
Serial.print knows when you pass it a char or an array of char that you want the Ascii representation, just as it knows if you pass it an int that you want the number it represents instead.
When LittleFS is writing something how does it write it?
Also, what shall I put to lala array to give it as input to LittleFS if i want a 0 in char array, 0 or 48 for example?
A char array holds in every position the DEC or the CHAR of this table?
chris700:
When LittleFS is writing something how does it write it?
It does nothing special, just takes whatever bytes you pass it and writes them to the file system - it doesn't care what they are.
Also, what shall I put to lala array to give it as input to LittleFS if i want a 0 in char array, 0 or 48 for example?
Doesn't matter as long as you remember what you did when it comes time to read it. Since you probably want it to be human readable though, add the 48.
There is a caveat though which I don't know is necessarily applicable to LittleFS. It you're writing a text file character 26 may have a special meaning - end of file, so best avoid the non-printable characters.
A char array holds in every position the DEC or the CHAR of this table?
It holds eight bits in every position. By convention, if you print them you will get the Ascii character shown in that table, but there's no magic telling the computer I'm holding Ascii data in this byte, it's just raw data. If you want to check what's in a particular char you can compare it to 'A' or you can compare it to 65. It's just a pattern of bits.
This means that you can treat the data as whatever you want. It's also why C++ has casting.
i appreciate your help, bit too advanced for me.ill test it when i finally understand what is going on with those pointers etc! much appriaciated your help.
wildbill:
It holds eight bits in every position. By convention, if you print them you will get the Ascii character shown in that table, but there's no magic telling the computer I'm holding Ascii data in this byte, it's just raw data. If you want to check what's in a particular char you can compare it to 'A' or you can compare it to 65. It's just a pattern of bits.
This means that you can treat the data as whatever you want. It's also why C++ has casting.
Ah so pretty much since i was used to strings i used to compare if mystring ="teststring", now i have to create an array of the string that i want(teststring) to compare it with the array. right? or compare character per character with ''?
I tested a line:
char mypath[60]="/mydir/" "hohoho"
and it actually prints /mydir/hohoho
I was wondering how to do it(used to use + in Strings)..
My question is, is there any "easy" way without the loops i used before to put a char array inside?
like:
char mypath[60]="/mydir/" cryptoid ".txt"