Here is my code, and it displays the content of input.txt to the screen. I want it to do the same thing to read the output.txt file, I've tried making a new ifstream for output.txt, but it still will not display.
if (word[0] >= 'a' && word[0] <= 'a')
what does this mean?
you can also read the file a line at a time using getline(infile, line) where line is a variable of type std::string
to read output.txt you need to initialize it with a std::ifstream object, std::ofstream will not read files
#include <iostream>
#include <string>
#include <fstream>
int main()
{
const std::string in_file_name = "input.txt" ;
const std::string out_file_name = "output.txt" ;
{
std::ifstream infile(in_file_name) ; // open for nput
if( !infile.is_open() )
{
std::cerr << "failed to open file '" << in_file_name << "' for input\n" ;
return 1 ; // just return from main; avoid std::exit
}
std::ofstream outfile(out_file_name) ; // open for nput
if( !outfile.is_open() )
{
std::cerr << "failed to open file '" << out_file_name << "' for output\n" ;
return 1 ;
}
std::string word ;
while( infile >> word ) // note: this does not preserve line endings in the input file
// (the input file may have than one line of text)
{
// avoid the clumsy: word = 'A' + word.substr(1);
if( word[0] == 'a' ) word[0] = 'A' ;
outfile << word << ' ' ;
}
} // both files are now closed
// as a general principle, avoid seeking on files opened in text mode
// display contents of the input file
std::cout << "contents of input file:\n" << std::ifstream(in_file_name).rdbuf() ;
// display contents of the output file
std::cout << "\n\ncontents of output file:\n" << std::ifstream(out_file_name).rdbuf() ;
}
Here's a version that preserves the format (spacing and line endings) of the input file.
It uses the regular expressions library; if you are unfamiliar with regular expressions, it is very worthwhile to make an effort to learn it. Start with a regex tutorial first; for example this one https://www.regexone.com/ before you look at the standard C++ regular expressions library.
#include <iostream>
#include <string>
#include <fstream>
#include <regex>
int main()
{
const std::string in_file_name = "input.txt" ;
const std::string out_file_name = "output.txt" ;
{
// for testing: create input file
std::ofstream(in_file_name) << "Aah! - alliteration is almost always\n""an amusing \t artifact of articulation.\n""(note:\tthe\tabove sentence is not alliterative.)\n" ;
}
{
std::ifstream infile(in_file_name) ; // open for input
std::ofstream outfile(out_file_name) ; // open for output
std::string line ;
while( std::getline( infile, line ) ) // for each line in the input file
{
// regex: (^|\s)a - either beginning of line or white space (captured),
// followed by lower case 'a'
// ie. a lower case a at the beginning of a word
staticconst std::regex word_re( "(^|\\s)a" ) ;
// replace: make all lower case a at word beginning with upper case A
// leave everything else (including white space) unchanged
// replace with: $1A - the same captured white space $1, followed by upper case A
outfile << std::regex_replace( line, word_re, "$1A" ) << '\n' ;
}
}
std::cout << "contents of input file:\n" << std::ifstream(in_file_name).rdbuf()
<< "\n\ncontents of output file:\n" << std::ifstream(out_file_name).rdbuf() ;
}