cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
aynilian
Explorer
Explorer
793 Views
Registered: ‎07-05-2017

How to read an external file from Petalinux directory structure into C code?

Jump to solution

Hi,

 

I am trying to read and external file from my Petalinux directory structure into my C code.

1. I copied the external file which happens to be a .png image file into Petalinux root directory:

 

..$ scp img.png root@10.0.0.3:

 

I checked the Petalinux root directory and the "img.png" is in the root as shown below:

 

peta_root.png

 

2. I run my compiled ".elf" code from the Petalinux root directory, which should open a file and read the size and print contents. But I get "0" value for the size:

Here is the code snippet:

 ......

Screenshot from 2018-10-24 11-13-50.png

 

.....

Running the above mentioned code, I get this below:

 

Screenshot from 2018-10-24 11-12-16.png

 

3. I tried to change the location of the img.png file and open it like this: file = fopen("/bin/img.png","r");

still reads zero for size.

 

What could be wrong, Why can't the C code launched from the root directory not seeing the referenced file ?

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
rfs613
Scholar
Scholar
757 Views
Registered: ‎05-28-2013

One possible reason is that fopen() might be failing. You should really check its return code, if it is NULL, the open failed...

 

However a more likely reason is that you are calling ftell() immediately after fopen(). The file position of a newly opened file will always be zero (unless you open it in "append" mode).

 

One way to get the size is to seek to the end. It looks like you have a line to do that, but it is commented out. If you put that line back in, and move it before the call to ftell(), you should see the correct size reported.

 

Note that once you do that, you will not be able to get any of the data with fgetc(). This is because the first seek took you to the end-of-file, so fgetc() will return an error each time. To fix this, you would need to do another fseek(), to go back to the beginning of the file, before reading the bytes.

 

As an alternative approach, instead of seeking to the end, and then seeking back to the beginning, you could just read the bytes until you reach the end. Here is how you would typically see it written:

 

FILE *file;
char ch;

file = fopen("img.png", "r");
if (file == NULL)
{
    perror("open failed");
    return;
}

while ((ch = fgetc(file)) != EOF)
{
    printf("%x", ch);
}

fclose(file);

By the way, the above is not specific to Petalinux. Any Unix/Linux/BSD will behave the same way.

View solution in original post

2 Replies
rfs613
Scholar
Scholar
758 Views
Registered: ‎05-28-2013

One possible reason is that fopen() might be failing. You should really check its return code, if it is NULL, the open failed...

 

However a more likely reason is that you are calling ftell() immediately after fopen(). The file position of a newly opened file will always be zero (unless you open it in "append" mode).

 

One way to get the size is to seek to the end. It looks like you have a line to do that, but it is commented out. If you put that line back in, and move it before the call to ftell(), you should see the correct size reported.

 

Note that once you do that, you will not be able to get any of the data with fgetc(). This is because the first seek took you to the end-of-file, so fgetc() will return an error each time. To fix this, you would need to do another fseek(), to go back to the beginning of the file, before reading the bytes.

 

As an alternative approach, instead of seeking to the end, and then seeking back to the beginning, you could just read the bytes until you reach the end. Here is how you would typically see it written:

 

FILE *file;
char ch;

file = fopen("img.png", "r");
if (file == NULL)
{
    perror("open failed");
    return;
}

while ((ch = fgetc(file)) != EOF)
{
    printf("%x", ch);
}

fclose(file);

By the way, the above is not specific to Petalinux. Any Unix/Linux/BSD will behave the same way.

View solution in original post

aynilian
Explorer
Explorer
748 Views
Registered: ‎07-05-2017

Hi rfs613

 

Works!

 

I tried your suggestion(s) moving the fseek() before the ftell() and I did see the size of the file.

 

Then I did fseek(file,0,SEEK_SET);

Now I can see the ch = fgetc(file) printfs.

 

Thanks for sharing.

 

 

0 Kudos