I do not know (
nor really care) what C++ standard you are building your source to or your compiler....
C++17 added the
<filesystem> library that can make dealing with files much easier.
C++20 added the
<format> library, making it easier to format data to be displayed to the console/written to a file.
When opening files it is best to check the file actually opened before reading/writing data. Trying to read data from a file that didn't properly open is gonna cause problems.
There are newer and safer ways to do for loops that reduce the possibility of accessing your container's elements going out of bounds.
While this particular problem is easily identifiable, sending your output to the console instead of a file, giving us a self-contained and compilable example makes it easier for us to help you.
The following example doesn't use
<filesystem>, requires C++20 or later:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <fstream>
#include <iostream>
#include <vector>
#include <format>
int main( )
{
std::vector v { 1.2, 2.3, 4.5, 5.6, 7.8, 9.0 };
std::ofstream myFile;
myFile.open( "DataFile.txt", std::ios_base::out );
if ( myFile.is_open( ) )
{
std::cout << "File open successful\n";
for ( size_t count { }; const auto& itr: v )
{
// duplicate the output to the console and the file to see what it looks like.
// easier to format the output that way. Remove the std::cout when
// no longer needed.
std::cout << std::format( "({:>10} ) {:.3f} feet\n", count, itr );
myFile << std::format( "({:>10} ) {:.3f} feet\n", count, itr );
count++;
}
std::cout << '\n';
std::cout << "Finished writing to file, will close now\n";
myFile.close( );
}
else { std::cerr << "Unable to open file, exiting!\n"; }
}
| |
File open successful
( 0 ) 1.200 feet
( 1 ) 2.300 feet
( 2 ) 4.500 feet
( 3 ) 5.600 feet
( 4 ) 7.800 feet
( 5 ) 9.000 feet
Finished writing to file, will close now |
An example using
<filesystem>, creating a sub-directory and a file for output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include <filesystem>
#include <fstream>
#include <iostream>
// for simplicity
using namespace std::filesystem;
int main( )
{
// Define the path to create directory
path directorypath = "mydirectory";
// To check if the directory exist or not, create it if
// doesn't exist
if ( !exists( directorypath ) )
{
create_directory( directorypath );
std::cout << "Directory created: " << directorypath << '\n';
}
// Define the file path within the directory and
// combining the directory
path filepath = directorypath / "myFile.txt";
// Create and open the file for writing using
// std::ofstream
std::ofstream file( filepath );
if ( file.is_open( ) )
{
// Write data to the file
file << "Hello, FileSystem!";
file.close( );
std::cout << "File created: " << filepath << '\n';
}
else
{
// Handle the case if any error occurred
std::cerr << "Failed to create file: " << filepath << '\n';
}
}
| |
Checking the open status of a file for reading is super-critical.