Hi all,
This c code I use it to compute the number of elements in file:
fseek(file, 0, SEEK_END);
size_t inSize = ftell(file);
size_t no_ofelem = inSize/2;
I converted it to work on Arduino ide in this way:
file.seek(file.size());
inSize = file.position();
no_ofelem = inSize/2;
But this gave me not the same result. Can you please tell me how can I use like ftell(file);
with Arduino IDE ?
Please explain what you mean by an element of a file. Your code appears to print the number of bytes in a file divided by 2
1 Like
Yes, the c code return the number of values in txt file. The type of value is (int16).
That relies on each value consisting of exactly one character, thus restricting the value to a range of 0 to 9 and there being no Linefeeds between values
An int will certainly consist of more than a single character
This is the C code:
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdint.h>
int16_t *readInt16(char *srcFilePath, size_t *nbEle)
{
size_t inSize;
FILE *pFile = fopen(srcFilePath, "rb");
if (pFile == NULL)
{
printf("Failed to open input file. 1\n");
return NULL;
}
fseek(pFile, 0, SEEK_END);
inSize = ftell(pFile);
printf("inSize = %d \n",inSize);
nbEle = inSize/2;
printf("no_ofelem = %d \n",nbEle);
fclose(pFile);
if(inSize<=0)
{
printf("Error: input file is wrong!\n");
}
int16_t *daBuf = (int16_t *)malloc(inSize);
pFile = fopen(srcFilePath, "rb");
if (pFile == NULL)
{
printf("Failed to open input file. 2\n");
return NULL;
}
fread(daBuf, 2, *no_ofelem, pFile);
fclose(pFile);
return daBuf;
}
void main(){
// put your setup code here, to run once:
size_t *no_ofelem ;
char srcFilePath[]="test_.txt";
int16_t *daBuf = readInt16(srcFilePath, no_ofelem );
printf("no_ofelem = %d \n",no_ofelem);
}
And this is the Arduino code:
#include <stdio.h>
#include <stdlib.h>
#include <SPI.h>
#include <SD.h>
int16_t *readInt16(char *srcFilePath, size_t *no_ofelem )
{
size_t inSize;
//------------------
if (!SD.begin(BUILTIN_SDCARD)) {
Serial.println("Failed to mount card () ");
while (1);
}
//open file for reading..
File pFile = SD.open(srcFilePath, FILE_READ);
if (!pFile) {
Serial.println("Opening file to read failed 1() ");
return;
}
inSize = pFile.size(); //pFile.position(); //ftell(pFile);
Serial.printf("inSize = %d \n",inSize);
no_ofelem = inSize/sizeof(int16_t); ///2;
Serial.printf("no_ofelem = %d \n",no_ofelem);
pFile.close();
if(inSize<=0)
{
Serial.printf("Error: input file is wrong!\n");
}
int16_t *daBuf = (int16_t *)malloc(inSize);
//open file for reading..
pFile = SD.open(srcFilePath, FILE_READ);
if (!pFile) {
Serial.println("Opening file to read failed ()2 ");
return;
}
pFile.read(daBuf, *no_ofelem );
pFile.close();
return daBuf;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {};
size_t *no_ofelem ;
char srcFilePath[]="test_.txt";
int16_t *daBuf = readInt16(srcFilePath, no_ofelem );
Serial.printf("no_ofelem = %d \n",no_ofelem);
}
void loop() {
// put your main code here, to run repeatedly:
}
The output in C code is inSize = 5004 no_ofelem = 2502
In arduino: inSize = 87714 no_ofelem = 43857
test_.txt (4.9 KB)
Are you sure that the C code that you poted does what you want ?
From https://cplusplus.com/reference/cstdio/ftell/?kw=ftell
long int ftell ( FILE * stream );
Get current position in stream
Returns the current value of the position indicator of the stream .
So, if you fseek() to the end of the file then the ftell() function will return the number of bytes in the file. How does dividing that by 2 tell you how many "elements" there are in the file ?
The test.txt file that you posted is a binary file, not a text file. Where did it originate from ?
In C, the writing in the file :
fwrite(&variable ,sizeof(int16_t),1,file);
Try writing 100 ints to the file using your C code then counting the number of "elements" using your C code
What do you expect the number of "elements" to be ?
What is the number of "elements" returned ?
awneil
September 26, 2023, 1:36pm
10
Indeed:
Here's an online Hex view:
HexEd.it is a free hex editor for Windows, MacOS, Linux and all other modern operating systems, which uses HTML5 and JavaScript (JS) technology to enable hexediting online, directly in your browser.
Please I need help to get on the number of values in txt file. How can do that?
The txt file like this:
16 -1 -1 0 19 1 0 0 4 -4 -7 -4 0 0 0 2 1 2 1 0 1 1 1 -2 4 0 0 0 -1 12 -6 1 0 2 -4 -1 1 1 14 0 1 1 -2 1 0 1 -1 0 0 0 0 -1 16 19 -5 0 7 -6 -2 14 -4 0 0 0 0 0 0 0 -2 4 -5 5 6 -3 -7 -4 0 3 3 0 0 0 0 0 0 0 0 -1 -1 0 0 0 0 0 1 -27 3 15 10 2 2 0 1 -2 6 2 -4 -1 0 0 0 0 0 -2 -2 1 10 2 6 0 0 11 -2 -7 -1 0 0 0 0 0 0 0 1 1 0 21 0 0 -1 4 -5 -8 -4 1 0 0 1 0 0 1 0 0 -1 1 -2 2 0 1 0 0 0 0 -1 15 -8 -2 -4 3 -1 0 -1
Any help please?
One good starting point would be to count the number of ‘spaces’…?
1 Like
No, I need just the number of values.
b707
September 26, 2023, 12:49pm
14
number of values is a number of spaces +1, but counting the spaces will be more easy task than counting the values.
1 Like
b707
September 26, 2023, 1:11pm
15
@mercala_eng
Why did you opened a second topic with the same problem? - It is a violation of the forum rules. Please keep your discussion in the first thread:
Hi all,
This c code I use it to compute the number of elements in file:
fseek(file, 0, SEEK_END);
size_t inSize = ftell(file);
size_t no_ofelem = inSize/2;
I converted it to work on Arduino ide in this way:
file.seek(file.size());
inSize = file.position();
no_ofelem = inSize/2;
But this gave me not the same result. Can you please tell me how can I use like ftell(file); with Arduino IDE ?
1 Like
There are no spaces between the single digit integers.
@mercala_eng
Could you show us the code you have so far please?
gfvalvo
September 26, 2023, 1:35pm
17
There are no spaces between the single digit integers.
Sure looks to me like there are:
If there weren't space, they wouldn't be "single-digit" integers!
1 Like
b707
September 26, 2023, 1:41pm
18
Are you sure?
If there are "single digit integers"
1 2 4 8 9
so they has a spaces between it.
If there are something like
111
there is no spaces between digits, but it is not a "single digit".
You’re both right, sorry, I’ll change my optician.
awneil
September 26, 2023, 1:58pm
20
Maybe it is a different problem? That other thread (despite its title) was about a binary file - not a text file.
@mercala_eng - if you've given up on that other thread, then please add a note there to the people who have been trying to help you with it.