We have an Iterative robot working using the WPIlib in C++ and would like to store floating point values to the cRio for later use. We created a test program that reads and writes a file in windows using standard fopen\fscanf\fprintf\fclose and then ported it to the cRIO.
We’ve successfully copied a test.txt to the file system using FTP but the fopen for the read routine is failing.
We’ve tried “c:\”, " /test.txt" or the “file://” and “text.txt” but none have worked. We found a post that mentioned something about a SetFileWriteAllowed but not sure if that is required for reading. I’ve searched the other forums with some success but haven’t found a working sample using the WPIlib. Let me know and I can post the actual code but here is the idea:
//sample …
FILE *pFile = fopen(“text.txt”, “rt”);
if(pFile == NULL )
{printf(“failed to open”); return false;}
fscanf(pFile,“Offset=%f”,&floatTest);
//and later we have something similar in order to write using fprintf
fprintf(pOutFile,"
Label_1=%d",floatTest);
slightly off topic, but why are you using C style IO when C++ IO is available? In 09 we successfully used fstream IO with just “file.ext” style and “existingdir/file.ext” style open args
fstream file;
file.open("test.txt");
if (!file.is_open())
{printf("failed to open"); return false;}
file>>floatTest;
//and later we have something similar in order to write using fprintf
file<<floatTest;
Here is our routine but it sounds like the “rt” - read text should not be a problem but we’ll try the “r+” - read write and see if it helps.
The ‘C’ functions were quick and easy way to implement reading and parsing values from a file. Preference I guess, but we’ll also try the C++ stream routines as well.
I thought maybe the way we referred to the file system using Windows “c:” or Unix “/” would be a problem.
/* Reads values for potentiometer offset, etc.
note: home_path is a char* path to the file in the cRIO root directory
where the values should be stored. (“C:\some_file.txt”)
*/
void read_vals(void)
{
pInFile = fopen(home_path, “rt”);
if(pInFile == NULL)
{
printToDisplay(“FILEPATH INCORRECT”, 1, 1, LCD);
load_defaults();
return;
}
fscanf(pInFile, "potentiometer=%f
", &test_var);
fclose(pInFile);
}
To check the proper file path format, one way is to write some test code to enumerate files for the target file system, it should return you file paths and you can print them out.