Still learning and doing some basic file handling exercise and was wondering if I wrote this code correct and did I do all the error checking and if statements properly?
int main()
{
std::wofstream outFile;
outFile.open(L”savelist.txt”, std::ios::out);
if (outFile.is_open())
{
wchar_t buffer[100];
std::wcout << L”Please Enter Address \n”;
std::wcin.getline(buffer, sizeof(buffer));
if (outFile << buffer)
{
std::wcout << “File has been written \n”;
outFile.close();
}
else
{
std::wcout << “Write to file failed \n”;
}
}
else
{
std::wcout << “File could not be opened \n”;
}
}
#include <iostream>
#include <fstream>
int main()
{
const std::size_t N = 100 ;
wchar_t address[N] {} ;
std::wcout << L"enter address: " ;
std::wcin.getline( address, N ) ; // note: N == sizeof(address) / sizeof(wchar_t)
// note: constructor of wofstream would accept a const wchar_t* only if std::filesystem::path::value_type is wchar_t
if( std::wofstream out_file{ "savelist.txt" } ) // if file was opened for output
{
if( out_file << address ) std::wcout << L"address was written into file\n" ;
else std::wcerr << L"write to file failed\n" ;
} // at end of scope, out_file is automagically closed
else std::wcerr << L"failed to open file\n" ;
}
1. Post a complete code example by including headers when possible, it makes it easier for others to paste the code and try to compile it. A short, self-contained & compileable example.
2. Proper formatting, indentation and restrained use of whitespace help make looking at code easier to spot minor errors and follow logical blocks of code.
#include <iostream>
#include <fstream>
#include <cstdlib>
int main()
{
const size_t size { 100 };
std::wofstream outFile("savelist.txt");
if (!outFile)
{
std::wcerr << L"File could not be opened \n";
return EXIT_FAILURE;
}
wchar_t buffer[size];
std::wcout << L"Please Enter Address \n";
std::wcin.getline(buffer, size);
if (outFile << buffer)
{
std::wcout << L"File has been written \n";
// not actually needed, the file will close when the program ends
outFile.close();
}
else
{
std::wcerr << L"Write to file failed \n";
}
}
Personally for something this trivial I'd use std::string instead of a C string and not use wide characters.
This is only correct when the size of the underlying type used by buffer is 1 byte (ie when it's char). In this case buffer has an underlying type of wchar_t (2 bytes). Hence you need:
#include <fstream>
#include <iostream>
int main() {
std::wofstream outFile;
outFile.open(L"savelist.txt");
if (outFile.is_open()) {
wchar_t buffer[100] {};
std::wcout << L"Please Enter Address \n";
std::wcin.getline(buffer, sizeof(buffer)/sizeof(buffer[0]));
if (outFile << buffer)
std::wcout << L"File has been written \n";
else
std::wcout << L"Write to file failed \n";
} else
std::wcout << L"File could not be opened \n";
}
>> // note: constructor of wofstream would accept a const wchar_t* only if std::filesystem::path::value_type is wchar_t
>> if( std::wofstream out_file{ "savelist.txt" } ) // if file was opened for output
>>> outFile.open(L"savelist.txt");
Quite unsurprisingly, the same holds for std::basic_ofstream<>::open()
I am still kind of confused on this: note: constructor of wofstream would accept a const wchar_t* only if std::filesystem::path::value_type is wchar_t.
What does this really mean? My buffer array type is a wchar_t.
I had a look on MSDN which is quite intimating for me and found the constructor takes a wchar_t*. But what is std::filesystem::path::value_type?:
On a non-windows platform (where std::filesystem::path::value_type is not wchar_t):
1 2 3 4 5 6 7 8 9
#include <iostream>
#include <fstream>
int main()
{
// *** error *** : no matching function for call to 'std::basic_ofstream<char>::basic_ofstream(const wchar_t [9])'
// *** error *** : no matching constructor for initialization of 'std::ofstream'
std::ofstream test( L"xyz" ) ;
}
An overload of the file stream constructor accepts std::filesystem::path for the file name. std::filesystem::path can be constructed from a wide variety of character sequences.
This would work on all conforming C++ implementations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <filesystem>
#include <fstream>
int main()
{
constwchar_t file_name[] = L"savelist.txt" ;
if( std::wofstream out_file{ std::filesystem::path(file_name) } ) // if file was opened for output
{
// use out_file
out_file << L"abcd\nefgh\nijkl\n" ;
}
else { /* error: failed to open output file */ }
}