Table of Contents Previous topic
I/O via Host PC->File I/O
fopen(), fread(), fwrite() and fclose() can be used to read and write files on the host PC.
The following code will create a file on the PC containing the data in array szText and then read it back again.
char szText = "asdasdas";
FILE* fh = fopen("C:\\test.dat", "w");
size_t nBytesWritten = fwrite(szText, 1, 20, fh);
int nResult = fclose(fh);
for (int i = 0; i < 20; i++)
szText[i] = 0XAA;
fh = fopen("C:\\test.dat", "r");
size_t nBytesRead = fread(szText, 1, 20, fh);
nResult = fclose(fh);
Note that the pointer fh is a pointer to memory on the PC, not to memory on the ARM board. In particular if you use it to call the library functions fputc() or fgetc() directly, or indirectly via for instance fprintf() or fscanf(), then it will result in unpredictable behaviour.
The debugger keeps track of pointers to files opened across the debug communication channel. Invalid file pointers will be rejected and files that are still open when the debugger is closed will be automatically closed.
To prevent an ARM program unexpectedly opening a file on the host PC, the debugger will only open files if the Allow file I/O on the host PC item has been checked in the projects Debug settings. By default this item is unchecked.