Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   ofstream (standard C++ file output) not working (http://www.chiefdelphi.com/forums/showthread.php?t=79548)

byteit101 01-01-2010 12:08 PM

ofstream (standard C++ file output) not working
 
I was testing a logging system, where you would be able to log data to a logfile on the cRIO, and then FTP to it to see what happened. I used a modified simpleRobot, and added a streambuf* to the variable list, a ofstream, initialized to "cRIOUserLog.log", and then rdbuf to clog. but clog<<"Logging Test"<<endl; (which should also flush) does not appear to do anything. I FTP'd to the cRIO, and looked in most of the directories, but I cannot find cRIOUserLog.log. Is there something I need to do? I remember someone somewhere in some thread somewhile ago saying something about a write flag, but I was able to use IMAQ_write_png (don't think this is the actual name) to write PNG's to the cRIO.
Relevant code:
PHP Code:

class SuperSimpleRobot
{
streambufofsTclog;
ofstream UserLog;
//...
public:
SuperSimpleRobot():
UserLog("cRIOUserLog.log")//...
{
    
ofsTclog=UserLog.rdbuf();
    
clog.rdbuf(ofTclog);
    
//...
    
clog<<"Logging Test"<<endl;
}
//...
}; 


Pat Fairbank 01-01-2010 12:55 PM

Re: ofstream (standard C++ file output) not working
 
Try calling Priv_SetWriteFileAllowed(1) in the constructor.

I've never used ofstream on the cRIO, but I've gotten C-style file I/O to work (FILE*, fopen(), fwrite(), etc.).

slavik262 01-01-2010 05:38 PM

Re: ofstream (standard C++ file output) not working
 
Ditto. C-style has always run fine with me. If you know what you're doing it's not that hard to make a class similar to ofstream with c-style calls.

byteit101 01-01-2010 06:26 PM

Re: ofstream (standard C++ file output) not working
 
Quote:

Originally Posted by Pat Fairbank (Post 892017)
Try calling Priv_SetWriteFileAllowed(1) in the constructor.

I've never used ofstream on the cRIO, but I've gotten C-style file I/O to work (FILE*, fopen(), fwrite(), etc.).

Thanks! I will try that next time I get a chance.

Quote:

Originally Posted by slavik262 (Post 892041)
Ditto. C-style has always run fine with me. If you know what you're doing it's not that hard to make a class similar to ofstream with c-style calls.

I know about C IO, but like C++ much better due to its simplicity, and ease of use by semi-incompetent people

slavik262 01-02-2010 11:40 AM

Re: ofstream (standard C++ file output) not working
 
What I was saying is that if you can't get ofstream to work, write your own class that copies its behavior and use the C-style I/O in the member functions.

byteit101 01-02-2010 12:37 PM

Re: ofstream (standard C++ file output) not working
 
Quote:

Originally Posted by slavik262 (Post 892203)
What I was saying is that if you can't get ofstream to work, write your own class that copies its behavior and use the C-style I/O in the member functions.

if C style works, why would ofstream not work?

Jared Russell 01-02-2010 06:01 PM

Re: ofstream (standard C++ file output) not working
 
The cRIO requires that you call Priv_SetWriteFileAllowed(1) before you can create files on the cRIO file system. You seem to be instantiating your ofstream object in the initialization list of your main robot class; in this case, the ofstream would attempt to create the specified file BEFORE you have allowed such an activity to take place (even if you call Priv_SetWriteFileAllowed(1) in your robot class's constructor - the initialization list gets executed first).

Also, ofstream has several methods that can be useful in diagnosing what has gone wrong. In particular, look at is_open(), good(), fail(), bad(), rdstate() here: http://www.cplusplus.com/reference/iostream/ofstream/

Hope this helps!

byteit101 01-02-2010 08:16 PM

Re: ofstream (standard C++ file output) not working
 
any files?
when we saved camera png's we did not have to call Priv_SetWriteFileAllowed(1)

slavik262 01-04-2010 07:39 AM

Re: ofstream (standard C++ file output) not working
 
Quote:

Originally Posted by byteit101 (Post 892211)
if C style works, why would ofstream not work?

It's a great question, but I've never had any issues reading/writing to the robot using C file I/O, and I've never called Priv_SetWriteFileAllowed(1). I know my suggestion is more of a workaround than a real answer, but I just thought it might help.

karatekid 01-10-2010 07:47 PM

Re: ofstream (standard C++ file output) not working
 
I got this from cprogramming.com:
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
char str[10];

//Creates an instance of ofstream, and opens example.txt
ofstream a_file ( "example.txt" );
// Outputs to example.txt through a_file
a_file<<"This text will now be inside of example.txt";
// Close the file stream explicitly
a_file.close();
//Opens for reading the file
ifstream b_file ( "example.txt" );
//Reads one string from the file
b_file>> str;
//Should output 'this'
cout<< str <<"\n";
// b_file is closed implicitly here
}
This is how it is done in c++, for both input and output-this may not help, because I do not know how it will work with the robot, put it works for regular programs. Also, this only works for text files to my knowledge...

byteit101 01-11-2010 03:24 PM

Re: ofstream (standard C++ file output) not working
 
karatekid, I don't think you know what this thread is about. I know how to use o/i/fstream, but cannot find the file it (supposedly)creates. This format (should) work for the robot, as well as any other target supporting C++ io. And it works for binary fills also

Another thought: would I have to use /myfile instead of just myfile?

karatekid 01-11-2010 08:21 PM

Re: ofstream (standard C++ file output) not working
 
Sorry?

slavik262 01-13-2010 11:03 AM

Re: ofstream (standard C++ file output) not working
 
Any luck so far or is it still not working?

Also, I don't remember needing a forward slash in front of the filename when saving.

Jared Russell 01-13-2010 11:28 AM

Re: ofstream (standard C++ file output) not working
 
In Java, I recall I had to use a "file://" prefix on my filename (for example: "file://mylog.txt") in order to get file creation to work. Perhaps you have to do the same thing here?

byteit101 01-13-2010 04:49 PM

Re: ofstream (standard C++ file output) not working
 
Haven't tried to do it yet, yesterday we we only able to get LV/WR/NB installed. I think I might be able to test it Thursday, but definitely by Saturday.

Interesting Jared341, I will try this.


All times are GMT -5. The time now is 09:30 AM.

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