File output problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jewel87
    New Member
    • Jan 2007
    • 62

    File output problem

    Hi,
    I have something strange going on with my file output function, everything seems correct, i've checked with all possible references on the topic, but it is not working, i.e. not doing anything, not even creating the file for output.
    I'm using C++ Builder 6, and here is the code:
    Code:
    void Product :: OutputToFile()
    {
            fstream outputfile("data.txt", ios::app);
    
            outputfile<<ProductName.c_str() << "\n"
                      <<ProductID<< "\n"
                    << Weight<< "\n"
                    << PriceWithoutVAT<< "\n"
                    << PriceWithVAT<< "\n"
                    << ExpiryDate.c_str()<< "\n"
                    << AmountInStock<< "\n"
                    << ReorderLimit<< "\n"
                    << ProductCategory.c_str()<< "\n"
                    << InputDate.c_str()<< "\n"
                    << DueDate.c_str()<< "\n"
                    << Manufacturer.c_str()<< "\n"
                    << ManufacturerAddress.c_str()<< "\n";
    
            outputfile.close();
    
    }
    and the function is called from the form, like this:

    Code:
    void __fastcall TForm1::SaveToFileClick(TObject *Sender)
    {
              for(int i=0;i<size;i++)
       {
            myProductList.getProduct(i)->OutputToFile();
       }
    }
    Any ideas why this could be?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    So your program is compiling correctly, not giving you any runtime errors, but failing to output?

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      Try checking if the file opens correctly before you output to it, something like:

      [CODE=cpp]
      if (!outputfile){ \\this checks if the state of the stream is OK
      cout << "File error." << endl;
      exit(1); \\Builtin function that tells the program to exit and calling it with 1 rather than 0 implies an error.
      }
      [/CODE]

      This way, if the file can't open or it can't write to it, it'll stop and give you a message saying as much.

      Comment

      • jewel87
        New Member
        • Jan 2007
        • 62

        #4
        The program is compiling fine and not giving any errors, but it is not creating the file for output, and i also tried to create it manually, it still doesn't output the data.
        I've also tried to put something like advised
        Code:
               if (!outputfile)
                {
                        flag =1;
                        exit(1);
                }
        the flag is then returned by the function and in the main form I put a ShowMessage() function with its value, but it's not displaying anything, just closes the program.

        Comment

        • mohaakilla51
          New Member
          • Jul 2007
          • 39

          #5
          have you tried using an ofstream instead of just an fstream? Just a thought.

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            Originally posted by jewel87
            The program is compiling fine and not giving any errors, but it is not creating the file for output, and i also tried to create it manually, it still doesn't output the data.
            I've also tried to put something like advised
            Code:
                   if (!outputfile)
                    {
                            flag =1;
                            exit(1);
                    }
            the flag is then returned by the function and in the main form I put a ShowMessage() function with its value, but it's not displaying anything, just closes the program.
            exit(1) immediately closes the program, that's why the cout was inside that if in my example code.

            Is the folder you're trying to write to write-able on non-root permission levels? That's about all I can still think of...

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              If it's an fstream, since fstreams can also be used for input, it probably won't create the folder. Try using an ofstream variable instead, ofstreams are for output only, no input, so there should be no problem creating a new, empty file.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                You need to create your fstream and specify whether it is an input or outpout file. Then you need to say whether you want to start wrting and the begininng or the end of the file.
                [code=cpp]
                fstream file("data", ios_base::out | ios_base::app);
                [/code]

                This opens file as an output file and the write will start is the end of the file.

                Be sure to call file.fail() to be sure the file opened correctly.

                Comment

                • jewel87
                  New Member
                  • Jan 2007
                  • 62

                  #9
                  Thank you all guys very very much! Using an ofstream variable resolved the problem, i just didn't imagine this could be the cause. ;) Now i'll know. Thank you!!!

                  Comment

                  Working...