Thread: fstream open method causing me a few headaches

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    fstream open method causing me a few headaches

    Hello Everyone, I haven't posted here in a while.

    The problem I am having seems a simple one and I must be missing something quite basic but I just can't work out what. Below is some snippets of code from a program I am writing at the moment (there is too much source code to post the whole program), but for some reason the fstream 'open' method either fails or crashes at runtime and I can't work out why.

    Neural_network.h File:
    Code:
    /* Some includes and defines here - no problems here */
    
    class CSPNeuralNetwork
    {
          private:
                  /* Neural Network file instance */
                  std::fstream io_file;
                  
                  /* Neural Network */
                  SNeuralNetwork * neural_network;
                  
                  /* Private Methods */
                  bool LayerNumberValid(unsigned int layer_num);
                  bool NeuronNumberValid(unsigned int layer_num, unsigned int neuron_num);
                  
          public:
                 /* Constructors/Destructors */
                 CSPNeuralNetwork();
                 CSPNeuralNetwork(unsigned int n_layers);
                 ~CSPNeuralNetwork();
                 
                 /* Public Methods */
                 SNeuralNetwork * GetNeuralNetwork();
                 
                 /* Dat file output methods - Used for outputting neural network info */
                 bool SaveANN(char * filename);
                 bool SaveANN(char * filename, SNeuralNetwork * net);
                 bool SaveANN(const char * filename);
                 bool SaveANN(const char * filename, SNeuralNetwork * net);
    
                 /* More Methods after this, but they work fine (as far as I know) */
    };
    neural_network.cpp - Problem when I try and save a neural network setup to file
    Code:
    bool CSPNeuralNetwork::SaveANN(char * filename)
    {
         return SaveANN( (const char *)(&filename), neural_network );
    }
    
    bool CSPNeuralNetwork::SaveANN(char * filename, SNeuralNetwork * net)
    {
         return SaveANN( (const char *)(&filename), net );
    }
    
    bool CSPNeuralNetwork::SaveANN(const char * filename)
    {
         return SaveANN( filename, neural_network );
    }
    
    bool CSPNeuralNetwork::SaveANN(const char * filename, SNeuralNetwork * net)
    {
         char temp_buff[DEF_BUFF_SIZE];
         unsigned int pos_x = 0, pos_y = 0;
         
         /* Open the file specified */
         io_file.open( filename, std::fstream::in | std::fstream::out | std::fstream::trunc );
         
         if( !io_file.is_open() )
         {
             return true;
         }
    
         /* More code here ... */
    }
    main.cpp - where the class methods are actually called:
    Code:
        /* Tried it this way to start with */
        sprintf(temp_buff, "ANN SAVE FILE.txt");
        
        if( sp_ann.SaveANN(temp_buff) )
        {
            sp_out.WriteToLog(" - Neural Network was unable to be saved to file...\n");
            exit(1);
        }
    Also Tried it like this:
    Code:
        if( sp_ann.SaveANN("ANN SAVE FILE.txt") )
        {
            sp_out.WriteToLog(" - Neural Network was unable to be saved to file...\n");
            exit(1);
        }
    Whenever I try the code above the SaveANN method always return true, but I also tried opening the file by using the following line of code in the SaveANN method:
    Code:
    io_file.open( "ANN SAVE FILE.txt", std::fstream::in | std::fstream::out | std::fstream::trunc );
    But that causes a runtime error for some reason. Any suggestions?


  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Have you tried changing the file name to something without the spaces like "ANN_SAVE_FILE.txt"??

    In
    Code:
     bool CSPNeuralNetwork::SaveANN(const char * filename, SNeuralNetwork * net)
    do you close the file before leaving?

    Jim

  3. #3
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    I tried removing the spaces in the filename but that didn't do anything.

    If I try replacing the filename argument in the io_file.open() method call with a constant string, e.g.
    Code:
    io_file.open( "testing.txt", std::fstream::in | std::fstream::out | std::fstream::trunc );
    Then the file is created but the runtime error still persists. On the otherhand, if I use the method argument then the file isn't created and the SaveANN class method simply fails.

    Although I didn't show it in my last post, I call io_file.close() at the end of the SaveANN class method.

    This is a bit of an enigma. If I can't solve the problem I might try re-writing the SaveANN and LoadANN (the LoadANN method is still under construction), using C file I/O function calls, completely replacing all C++ file I/O elements with FILE pointers and fprintf() function calls. Far from ideal, given the amount of code I would have to edit, but that is why it is a last resort.


  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Do you get any error messages?

    What is the purpose of all the SaveANN()??

    You might try to call the SaveANN(const char * filename, SNeuralNetwork * net) directly.

  5. #5
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    The other versions of the save 'SaveANN' methods is just to provide alternative ways of calling the same function because it will be useful for later expansions I have in mind for this source code. Although ultimately, all the methods get redirected through the same class method.

    As for the error message I get, I get a runtime error. If you want me to copy and paste the error I get, then I will, but I don't know how useful it will be.


  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Yes please post the entire error message.

    Jim

  7. #7
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Ok, after playing around with the code a bit, I think I've found one of the causes of the runtime error and as such, I no longer get runtime errors. That being said, that doesn't explain why the fstream call to 'open()' always fails. For now (potentially for good), I've commented out the other versions of 'SaveANN()' to keep things simple.

    Although the code isn't 100%, getting rid of the runtime error is definitely a big step in the right direction. Do you think it might be a good idea just to re-code the whole section using only C I/O functions? I.e. Don't bother with C++ classes at all for this part of the program.

    Edit:
    Changing this line of code:
    Code:
    io_file.open( filename, std::fstream::in | std::fstream::out | std::fstream::trunc );
    to this:
    Code:
    io_file.open( filename, std::fstream::trunc );
    Has removed the runtime error but again, the code is still not working as intended.
    Last edited by Finchie_88; 10-16-2010 at 10:34 PM.


  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    C++ classes should be ok. But I would remove std::fstream io_file; from the class and change io_file.open() to
    Code:
    fstream io_file(filename, std::fstream::trunc);
    I find opening and closing the fstream multiple times can cause problems because the error states are not reset on open/close.

    Jim

  9. #9
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Last night, I got my code to run smoothly without any errors and the SaveANN() method worked as it should. I realised that the error I made wasn't even in 'SaveANN()', it was in another method I wrote.

    Basically, I am writing some A.I. code, using neural networks (as many of you have probably already guessed), and basically I've written it in as generic a way as possible so that any network structure can be generated if necessary. I have defined the network using a series of vectors and structures as shown below:
    Code:
    /* Definition of a Neuron */
    struct SNeuron
    {
           unsigned int neuron_location[2];             /* This is a simple ID system for saving and loading ANNs */
           std::vector<float> input_weights;            /* The neuron_location stores the neuron layer number in [0] */
           std::vector<SNeuron *> input_nodes;          /* and the neuron number in that layer in [1]. */
           std::vector<SNeuron *> output_nodes;
           float neuron_bias;
    };
    
    /* Definition of a layer */
    struct SNeuronLayer
    {
           std::vector<SNeuron *> layer_neurons;
    };
    
    /* Definition of Neural Network */
    struct SNeuralNetwork
    {
           std::vector<SNeuronLayer *> network_layers;
           float learning_momentum;
    };
    And a pointer to the Neural Network structure is included in the class 'CSPNeuralNetwork'. All the layers and neurons are dynamically allocated to memory and within each neuron there is a 'coordinate' to help me locate where it goes in the overall structure (i.e. the array neuron_location in the definition of SNeuron). Anyway, when calling another process called 'AppendLayer()' I forgot to specify the 'position' of the neuron in the network structure (by position, I mean a pair of numbers in the format {layer_number, neuron_number_in_that_layer}).

    I thought that the error came about in the 'SaveANN()' method because that was the method which failed but the problem was that I was attempting to save NULL information to the output file. The error has since been corrected. I am sorry for wasting everyone's time as the final problem turned out to have nothing to do with either fstream or my SaveANN() method. Thanks very much to all for attempting to help.

    P.S. If anyone has any past experience in programming neural networks feel free to comment on the structure I have chosen for ANNs. Also, general info about how best to program Neural Networks is more than welcome since this is my first ANN program.

    I am fairly early on in this project and I think this project may well become quite a big challenge. If someone feels like becoming a project partner on this endeavour then feel free to private message me (more details about what I intend if interest is shown). I could do with sharing the workload a bit
    Last edited by Finchie_88; 10-18-2010 at 06:59 PM.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  3. Delete files that are used
    By Yuri in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2005, 01:48 PM
  4. Open default browser and send an HTTP request (POST Method)
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 09-03-2005, 01:31 AM
  5. Replies: 12
    Last Post: 03-10-2005, 07:48 PM