0

First let me make this clear, I have read alot about this issue and i dont want similar answers.

I want to open a file from my pc stored on my HDD into atmel studio. Reading files is not part of my program, but I need to read this file because it contains example data. I can fill the arrays in my program manually but that would be exhausting.

I only need to read the file contents to the array, so that I can test my algorithm. I know on avr there is no filesystem and reading files makes no sense, but as I said reading files is not part of my algorithm.

Is there any work around to read files in Atmel Studio 6?

phadaphunk
  • 12,785
  • 15
  • 73
  • 107
Maher Assi
  • 75
  • 3
  • 10

2 Answers2

1

search for some tools bin2h bin2c or bin2hex or similar. that convert binary data to files with c-uint8_t-arrays that you can include. Iam quite sure that the avr-gcc or winavr does include such a tool but i cannot remember the name.

vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
  • I have found a tool that converts the file to binary data, that tool is named xxd which comes with the VIM text editor. I was able to generate .c file containing an array in binary data, but the problem is that how do I get my real data when i read that array? I mean how to convert that array from binary in c or c++? – Maher Assi Jun 17 '13 at 13:00
  • i do not think that anybody knows that you want. you have a file (binary? text? it doesn't really matter) that you converted to a c-Array. this array contains the exact same data as your file. It is totally up to you that you do with this data and how to interpret it. – vlad_tepesch Jun 17 '13 at 13:05
1

For anyone else who wants a solution to this, here is my solution. I didnt use any tool, instead I made a c++ function to read a file and write it in the form of an array, this function is to be used outside atmel.

    void writevectorf(const char *filename, vector<float> &myvector){
    FILE * pFile = fopen(filename,"w");
    if (pFile!=NULL){
        unsigned int size = myvector.size()-1;
        fprintf(pFile,"%s","#include <vector>\n\n");
        fprintf(pFile,"%s","float myfloats[] = {");
        for (unsigned int i=0; i<size; ++i)
            fprintf(pFile, "%0.7g, ",myvector[i]);
        fprintf(pFile, "%0.7g};\n",myvector[size]);
        fprintf(pFile, "%s = %d;\n","int datasize",myvector.size());
        fprintf(pFile, "%s %d %s","std::vector<float> input (myfloats, myfloats +",myvector.size(),");");
        fclose(pFile);
    }
    else
        cout << "Unable to open file: "  << filename << endl;
}

this function writes the file to a vector, it also writes to an array, feel free to modify it to suit your needs.

Maher Assi
  • 75
  • 3
  • 10