Writing to a file, it doesn't seem to be working for me...

This is a discussion on Writing to a file, it doesn't seem to be working for me... within the C++ Programming forums, part of the General Programming Boards category; I'm supposed to write my results to two files, and it doesn't seem to be working at all. With my ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75

    Writing to a file, it doesn't seem to be working for me...

    I'm supposed to write my results to two files, and it doesn't seem to be working at all. With my code below, what's my problem. Please answer ASAP! Thanks for all of the help in advance!

    Code:
    void OpenOutputFile(ofstream&, string);
    int main()
    {
        ifstream indiv_file;
        ofstream ofs_max;
        ofstream ofs_average;
        int array[MAX_SIZE], next, count = 0;
        float average_temp, maximum_temp;
        int array_size;
        string dir, out_file_max = "MaxsOfArrays.txt", out_file_average = "AvgsOfArrays.txt";
        cout.precision(0);
        
        cout << "Enter directory with files available for use: ";  //Lists the files in the current directory you could input .
        cin >> dir;
        stringVector files = stringVector();
        OpenOutputFile (ofs_max, out_file_max);
        OpenOutputFile (ofs_average, out_file_average);
       
        getDir (dir, files);
      
        int fsize = files.size();
        for (unsigned int i = 0; i < fsize; i++)
        {
            count = 0;
          
            if (files[i].at(0)!= '.') // if the file is a valid file
            {
               //cout << endl << dir + files[i];
               OpenInputFile (indiv_file, (dir + files[i]));
               array_size = ReadArray(indiv_file, array);
               average_temp = CalcAverage(array, array_size);
               maximum_temp = CalcMax(array, array_size);
            
               /* for (int j = 0; j < array_size; j++) // If you want to read out the data
               {
                   cout << array[j] << " ";
               } */
               
               files[i].erase(files[i].find("."), 5); //Deletes anything after AND including the 'dot' in the filename.
     
               cout << endl << "The average temperature in " << files[i] << " was: " << average_temp << endl;
               cout << "The maximum temperature in " << files[i] << " was: " << maximum_temp << endl;
               ofs_max << "The maximum temperature in " << files[i] << " was: " << maximum_temp << endl;
               ofs_average << "The average temperature in " << files[i] << " was: " << average_temp << endl;       
            }
            
            indiv_file.close();
            ofs_max.close();
            ofs_average.close();
         }
         cout << endl;
         return 0;
    }
    void OpenOutputFile(ofstream &out_file, string file_name)
    {
         out_file.open("MaxsOfArrays.txt");
         out_file.open("AvgsOfArrays.txt");
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would help if you explained WHAT is not working...

    You do realize that
    Code:
    void OpenOutputFile(ofstream &out_file, string file_name)
    {
         out_file.open("MaxsOfArrays.txt");
         out_file.open("AvgsOfArrays.txt");
    }
    This opens one file, then tries to open another with the same stream, and it also completely ignores the file_name parameter. I don't think that's what you MEANT to do...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    Well, I took out all the irrevelant functions of course, but I'm supposed to write to those two .txt files (in int main() ). I'm not getting any compiling errors, but when I open up those two .txt files, I'm not seeing anything that was written to them.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And do you get any output on the console.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    no i do not.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    well besides what I have cout <<, no

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you do get the average and max value printed by the cout lines.

    And you have fixed the open-file functions so that it opens the file once?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    Yes, everything is correct and acting the way it should be except that those two lines are not writing to the two .txt files.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what does your OpenOutputFile() look like now? Becuase the original one is defiitely wrong - who knows what it will ACTUALLY do, but it's not doing "what it says on the label", that's for sure.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    so, the new OpenOutputFile function is:

    Code:
    void OpenOutputFile(ofstream &out_file, string file_name)
    {
         out_file.open(file_name.c_str());
         if (out_file.fail())
         {
            cout << "Failed to open file." << endl;
            exit(1);
         }
    }
    I mean, the OpenInputFile and OpenOutputFile should be basically the same, so I do not see why my program isn't writing to it...

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see anything wrong in that code. I'm sorry if it sounded like I doubted you, but there's a great difference between that and what you posted first time round.

    And you say that you do get the cout statements next to the output to file?

    Sorry, I don't think there is much I can help with.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    i figured it out. Thanks for the help. you were right, what I did have and what I have now is drastically different. Thanks again,

    ~ Porsche911NFS

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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