Writting to file in callback function

This is a discussion on Writting to file in callback function within the C++ Programming forums, part of the General Programming Boards category; Hey everyone! I want to do a basic thing but for some strange reason there is something not working. I ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    34

    Writting to file in callback function

    Hey everyone! I want to do a basic thing but for some strange reason there is something not working.

    I need to write to a file but I'm handling all the code in a callback function, which in turn is declared in a class. Here is the architecture of my code:

    Code:
    int main (int argc, char **argv)
    {
      
      ImageConverter Aclass;
      while(1); //infinite loop
      return 0;
    }
    
    class ImageConverter
    {
    //some variables ..
      
    public:
    
    ImageConverter ():
     
      {
    
       
    
      }
    
      ~ImageConverter ()
      {
    
      }
    
      void imageCb () //this function gets called when I receive an image frm server
      {
    
        .... //some calculations
        
        fstream outfile ("directory/train.txt");
         
        outfile.open ("directory/train.txt", fstream::in | fstream::out | fstream::app);
        
        outfile<<"P,"<<SomeValues<<endl;
        
        outfile.close();
       
       ....
    
    
      }
    
    };
    But this doesn't work, the file gets created but it allways overwrites itself which is not what I need to do.

    Can anyone please tell me what I am doing wrong? Is it because i am constantly declaring outfile?

    Thanks
    Last edited by Median; 03-31-2013 at 03:41 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    From the information you have provided, it appears that ImageConverter()::imageCB() is never called. Which means the file will never be created, let alone "overwrite itself".

    Accordingly, the code will never produce output. All your main() function will do is invoke the constructor for ImageConverter, which does nothing, and then enter an infinite loop.

    Try providing a small sample of compilable code that actually exhibits your problem.

    More generally, read this link to learn how to ask questions that have a chance of eliciting a useful response.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Code:
    fstream outfile ("directory/train.txt");
    What I think may be happening.

    I'm pretty sure if you provide a file name here, you're invoking the constructor using the default file settings, which opens the file without using "ios::app", so there goes your file. Then you do this:

    Code:
    outfile.open ("directory/train.txt", fstream::in | fstream::out | fstream::app);
    I don't know what the consequence of calling open on this object after the constructor has been called in the previous way, might be undefined, might reopen with the new settings, but by that time your file has been blown away.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    >>the file gets created but it allways overwrites itself which is not what I need to do.
    Everytime you get to this line

    fstream outfile ("directory/train.txt");

    you overwrite the file (ie truncate it).
    Perhaps you intend for this to be in main, in order to truncate the file on start, and then subsequently open it for appending?

    Btw, it is better if you open the file once instead of opening and closing it all the time. It might be better to open the file in main then pass it to your imageconverted so it can write to it.
    Also, files close automatically when they go out of scope. No need to manually call close.

    Quote Originally Posted by rags_to_riches View Post
    I don't know what the consequence of calling open on this object after the constructor has been called in the previous way, might be undefined, might reopen with the new settings, but by that time your file has been blown away.
    According to cplusplus.com, it will fail if you reopen it after a file has already been opened.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    fstream's constructor does not open with the app modifier by default. It is in | out, which will truncate an existing file.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    @ grumpy:

    I'm sorry I wasn't to clear in my OP. I'm programming under ROS (Robot operating system) environment, which has it's own funcionalities and I didn't want to put to much information that would distract you from my main problem. I'm creating a software application for visual detection of humans in images of urban setting. The code is kinda complex and uses severel files for compilation. For you to compile it you would need to download ROS, and that is not the point.

    I have two programs running at the same time. One is the server, that is feeding the client with images every x second. Everytime an image arrives the client, ImageCb function gets called and I need to write some information to a file about it.

    @ Elysia

    I thought about creating the file on main and then opening it on the callback function, but I have no clue of how to do that. I tried to declare fstream on the ImageConverter constructor but even then I can't open it in the ImageCb. Keep getting "outfile isn't declared in this scope" error. How do I pass that information from the main to the class and then to the ImageCb?
    Last edited by Median; 04-01-2013 at 09:15 AM.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Ok problem is solved, actually it was easier than I thought, and it was thanks to the tip that Elysia gave

    in main:
    Code:
    int main (int argc, char **argv)
    {
      fstream outfile("directory/train.txt");
      ImageConverter ic;
      while(1);
      return 0;
    }
    and in the callback function

    Code:
    fstream outfile; //could be any other name
        
        outfile.open ("directory/train.txt", fstream::in | fstream::out | fstream::app);
        
        outfile<<"P,"<<features[0]<<endl;
        
        outfile.close();
    Thanks for the help everyone

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    That is actually not what I meant you should do. I meant something like:

    #include <fstream>

    Code:
    class ImageConverter
    {
    private:
    	std::fstream& m_file;
    public:
    	ImageConverter(std::fstream& file): m_file(file) {}
    	void ImageCb()
    	{
    		m_file << whatever;
    	}
    };
    
    int main()
    {
    	std::fstream file("C:\\test.txt");
    	ImageConverter converter(file);
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    That would be possible using some shared pointer (boost) funtionalities, but since this is a file that is phisically in the hard drive, its more straightforward this way.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    It is possible the way I did it, without any smart pointers. Which way is more straight forward is subjective.
    However, I find no information that speculates on whether you can open a file that is already open. This, more than likely, is platform dependent. That is, your code may or may not work, so I will not recommend it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    The way I did works nicely, but it is true that your way seems much more elegant and correct.

    Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read/Write function callback in user space for a file
    By rupamksarma in forum C Programming
    Replies: 4
    Last Post: 02-28-2012, 03:08 PM
  2. writting swap function
    By Pulock2009 in forum C Programming
    Replies: 7
    Last Post: 04-07-2010, 10:36 AM
  3. Writting to a text file
    By Andy123 in forum C Programming
    Replies: 1
    Last Post: 05-24-2006, 06:40 AM
  4. writting data from structure in different function
    By Shadow in forum C++ Programming
    Replies: 2
    Last Post: 08-09-2002, 08:52 PM
  5. What can I ask about problems of writting a .bat file?
    By DramaKing in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-13-2001, 02:53 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21