Displaying the output file??

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.
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
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
	ifstream infile("input.txt");
	ofstream outfile("output.txt");
	string word;
	char c;
		
	while(infile >> word)
	{
		if (word[0] >= 'a' && word[0] <= 'a')
			word[0] += 'A' - 'a';
			outfile << word;
			infile.get(c);
			outfile.put(c);
	}
		string getinputcontent;
	ifstream open_infile("input.txt");
	if(open_infile.is_open())
		while(getline(open_infile, getinputcontent))
		{
			cout << "Contents of infile:" << endl;
			cout << getinputcontent << endl;
		}
	return 0;
}
Last edited on
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
Last edited on


if (word[0] >= 'a' && word[0] <= 'a') This will make all words beginning with a be capitalized from input.txt

so if the file contains

apple banana aardvark cat anteater dog

it'll only make the first letter of each word starting with a capitalized
(probably a better way to do this though)

By changing the 2nd a to e
if (word[0] >= 'a' && word[0] <= 'e')

it would then capitalize all of the first letters that are in those words

Here's modified code, yet the console window doesn't display the actual contents of output.txt (but there is content if I open the text file)

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
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
	ifstream infile("input.txt");
	ofstream outfile("output.txt");
	string word;
	char c;
		
	if( !infile ) {
    cerr << "Input file could not be opened or file doesn't exist!" << endl;
    exit(1);
  }	
	while(infile >> word)
	{
		if (word[0] >= 'a' && word[0] <= 'a')
			word[0] += 'A' - 'a';
			outfile << word;
			infile.get(c);
			outfile.put(c);
	}
		string getinputcontent;
	ifstream open_infile("input.txt");
		getline(open_infile, getinputcontent);
			cout << "Contents of infile:" << endl;
			cout << getinputcontent << endl;			
		
	    string getinputcontent2;
	ifstream open_outfile("output.txt");
	    getline(open_outfile, getinputcontent2);
	        cout << "Contents of outfile:" << endl;
	        cout << getinputcontent2 << endl;	
	return 0;
}


… make all words beginning with a be capitalized from input.txt

so if the file contains 

apple banana aardvark cat anteater dog


restore the file pointer of the output file to its beginning before you can read from it:
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 <iostream>
# include <fstream>
# include <sstream>
# include <string>

int  main()
{
    std::fstream inFile{"D:\\input.txt"};
    std::fstream outFile{"D:\\output.txt"};

    std::string word{};

    while (inFile >> word)
    {
        if(word[0] == 'a')
        {
            word = 'A' + word.substr(1);
        }
        if(inFile)
        {
            outFile << word << " ";
        }
    }
    outFile.seekg(0, outFile.beg);
    //restore file pointer to beginning
    //http://www.cplusplus.com/reference/istream/istream/seekg/
    while (outFile >> word)
    {
            std::cout << word << " ";
    }
    //std::cout << outFile.rdbuf();alternative version
}
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
41
42
43
44
45
46
47
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
	fstream infile("input.txt");
	fstream outfile("output.txt");
	string word;
	char c;
	
	// Error checking to see if Input file exists
	if( !infile ) {
    cerr << "Input file could not be opened or file doesn't exist!" << endl;
    exit(1);
  }
		
	while(infile >> word)
	{
		if(word[0] == 'a')
		{
			word = 'A' + word.substr(1);
		}
		if(infile)
		{
			outfile << word << " ";
		}
		outfile.seekg(0, outfile.beg);
		cout << "Contents of output file:" << endl;
		while (outfile >> word)
		{
			cout << word << " ";
		}
		
		string getinputcontent;
	ifstream open_infile("input.txt");
	if(open_infile.is_open())
		while(getline(open_infile, getinputcontent))
	{
			cout << endl << endl << "Contents of input file:" << endl;
			cout << getinputcontent << endl;
		}
	return 0;
}
}


Following your modified code, I added some modifications and was able to get the results I was looking for! Thanks!

Contents of output file:
Anteater Antelope Apple Aardvark balding bobblehead

Contents of input file:
anteater antelope apple aardvark balding bobblehead

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
41
42
43
#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() ;
}
JLBorges,

The only issue that I come across (and could be the compiler I am using) is your code will not display the contents of output.txt
When I try it, it does display the contents of output.txt
http://coliru.stacked-crooked.com/a/f048ba02447e3ab9


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.

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
#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
            static const 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() ;
}

http://coliru.stacked-crooked.com/a/adafaf7e2fb649a4
Interesting. It must just be my C++ compiler that is causing the issues. Thanks for the information about regex, that tutorial was excellent.
Topic archived. No new replies allowed.