Question

Okay guys, I've tried everything I can think of. I'm passing in a file name into this function. A little context: hash_table is an already initialized and filled vector with key pairs, and the 'value' part of the pair is a Linked List that has the field "bucket_size". When I use cout to check if these fields are actually being accessed, they are; even the debugger lists them as being filed into the output stream. I have flush() and close() in there, but it doesn't write anything to the file. Returns true, indicating no errors in the stream. Anyone have nay ideas?

 string line;
 std::ofstream ofs;
 if(ofs.is_open())
     ofs.close();
 ofs.open(filename);
 if (ofs.is_open())
 {
     cout << "File Opened" << endl;
     for (double i = 0; i < hash_table.capacity(); ++i)
     {
         ofs << "Bucket Number " << i;
         if (hash_table[i].value != NULL)
             ofs << " Bucket Size: " << hash_table[i].value->bucket_size << endl;
         else
             ofs << " Bucket Size: 0" << endl;
         ofs.flush();
     }
     cout << "closing file stream" << endl;
     ofs.flush();
 ofs.close();
 if (ofs.good())
         return true;
     else
         return false;
 }
 else
 {
 cout << "File not opened" << endl;
 return false;
 }

}

Was it helpful?

Solution

You're almost certainly examining the wrong file. Remember that relative paths are relative to the working directory of the process, which is not necessarily the same as where the executable lives on disk.

OTHER TIPS

I compiled and ran it by the console and now it works, with no edits made. It seems my IDE doesn't like something in the code. Regardless, thank you everyone for the response.s

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top