Search code examples
c++fileavr-studio6

Files in Atmel Studio


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?


Solution

  • 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.