Problems appending to Text File

Can not append words to a text file

Page 1 of 1

1 Replies - 1728 Views - Last Post: 06 March 2009 - 09:28 PM Rate Topic: -----

#1 Scorpiobuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-February 09

Problems appending to Text File

Posted 06 March 2009 - 08:55 PM

Been working on this most of the week. Still can't get the funtion to append the user input words to the text file. Below is the latest attempt and it's not working either.
{
		string line;
		fstream File;  
		File.open("caps.txt");
		cout << "Please enter a priming word." << endl;
		 cin >> line;  //a priming read,use these with files as conditions for while loops, or a do while
		while(File.peek() != EOF)  //the peek is so we are not actually at EOF, rather we can append words at this location
		{
			cin >> line;
		}
		cout << "Please enter the first word and hit enter." << endl;
		File << endl;  //start a new line in the output file to put word on	   
		cin >> line;  //get priming read
		while (line != "end")
		{
			File << line << endl;
			cout << "Please enter another word to add to the dictionary file, enter end to exit" << endl;
			 cin >> line;  //get more words, or the "end"
		}
		cout << endl;
		File.close();  //changed from close();
	}



Notorion came up with this one, which I both appreciated and really liked for its simplicity, but still no dice. This thing has to be finished by Sunday night, so it's crunch time.

Is This A Good Question/Topic? 0
  • +

Replies To: Problems appending to Text File

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: Problems appending to Text File

Posted 06 March 2009 - 09:28 PM

OK. here's some code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void create_file()
{
    string line;
    fstream File;
    // Open file for writing, appending to the end of the file.
    File.open("caps.txt", ios::out | ios::app);
    if (!File.good())
    {
        cerr << "Unable to open file for writing" << endl;
        return;
    }

    cout << "Please enter a word and hit enter." << endl;
    cin >> line;
    while (line != "end")
    {
        File << line << endl;
        cout << "Please enter another word to add to the dictionary file, enter end to exit" << endl;
        cin >> line; //get more words, or the "end"
    }
    cout << endl;
    File.close(); //changed from close();
 
}

void print_file()
{
    fstream File;
    File.open("caps.txt", ios::in);

    if (!File.good())
    {
        cerr << "Unable to open file for reading" << endl;
        return;
    }
    
    string line;
    while (getline(File, line))
        cout << line << endl;
    File.close();
}

int main()
{
    create_file();
    print_file();
    return 0;
}


Here is some output from this code:
neon:meh jack$ ./test
Please enter a word and hit enter.
cat
Please enter another word to add to the dictionary file, enter end to exit
dog
Please enter another word to add to the dictionary file, enter end to exit
bird
Please enter another word to add to the dictionary file, enter end to exit
rat
Please enter another word to add to the dictionary file, enter end to exit
giraffe
Please enter another word to add to the dictionary file, enter end to exit
end

cat
dog
bird
rat
giraffe
neon:meh jack$ ./test
Please enter a word and hit enter.
rhino
Please enter another word to add to the dictionary file, enter end to exit
dinosaur
Please enter another word to add to the dictionary file, enter end to exit
hippopotamus 
Please enter another word to add to the dictionary file, enter end to exit
platypus
Please enter another word to add to the dictionary file, enter end to exit
end

cat
dog
bird
rat
giraffe
rhino
dinosaur
hippopotamus
platypus
neon:meh jack$

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1