Go to Post Happy 6 days till FIRSTmas! (and Happy New Year, but that pales in comparison =D) - VKP [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 10 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 28-02-2011, 17:03
Carmello Carmello is offline
Registered User
no team
 
Join Date: Jan 2010
Location: Connecticut
Posts: 5
Carmello is an unknown quantity at this point
help with file I/O on Crio ... unable to read file when fopen fails

Hi

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,"\nLabel_1=%d",floatTest);

Any ideas would be appreciated.
Thanks
Reply With Quote
  #2   Spotlight this post!  
Unread 28-02-2011, 19:14
masoug's Avatar
masoug masoug is offline
Food Consumer
FRC #0114
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: Planet Earth
Posts: 78
masoug is an unknown quantity at this point
Re: help with file I/O on Crio ... unable to read file when fopen fails

Can you show us your code?

fopen should be "fopen("test.txt", "r+");"
__________________

JabbaScript
Reply With Quote
  #3   Spotlight this post!  
Unread 28-02-2011, 20:54
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Re: help with file I/O on Crio ... unable to read file when fopen fails

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
Code:
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;
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
Reply With Quote
  #4   Spotlight this post!  
Unread 28-02-2011, 23:18
masoug's Avatar
masoug masoug is offline
Food Consumer
FRC #0114
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: Planet Earth
Posts: 78
masoug is an unknown quantity at this point
Re: help with file I/O on Crio ... unable to read file when fopen fails

Quote:
Originally Posted by byteit101 View Post
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
Code:
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;
Both ways are perfectly okay, depends on preference. We (114) like to use C-style I/O because it seems easier to use (for us).

Also, don't forget to call fclose or file.close() function before you exit.
__________________

JabbaScript
Reply With Quote
  #5   Spotlight this post!  
Unread 01-03-2011, 16:47
Carmello Carmello is offline
Registered User
no team
 
Join Date: Jan 2010
Location: Connecticut
Posts: 5
Carmello is an unknown quantity at this point
Re: help with file I/O on Crio ... unable to read file when fopen fails

Hi ... Thanks for the replies.

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\n", &test_var);
fclose(pInFile);
}
Reply With Quote
  #6   Spotlight this post!  
Unread 01-03-2011, 17:57
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: help with file I/O on Crio ... unable to read file when fopen fails

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.
__________________
Reply With Quote
  #7   Spotlight this post!  
Unread 06-03-2011, 10:38
Carmello Carmello is offline
Registered User
no team
 
Join Date: Jan 2010
Location: Connecticut
Posts: 5
Carmello is an unknown quantity at this point
Re: help with file I/O on Crio ... unable to read file when fopen fails

In case it helps, the C++ stream IO does work for us. We don't know why the C fopen routine did not.
Thanks to all for your assistance.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 02:36.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi