reading and writing to files....

This is a discussion on reading and writing to files.... within the C++ Programming forums, part of the General Programming Boards category; getting lazy with adding up all my bowling scores and finding my average and everything.... Decided while I was at ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    13

    Question reading and writing to files....

    getting lazy with adding up all my bowling scores and finding my average and everything.... Decided while I was at it, I might as well make a program that'll keep scores and averages for the entire league....
    Code:
    {
    			value = 0;
    			MasterlistIn.open("Masterlist.txt");
    			getline(MasterlistIn, Master[value]);
    			value++;
    			while (MasterlistIn)
    			{
    				getline(MasterlistIn, Master[value]);
    				value++;
    			}
    			control = value;
    			MasterlistIn.close();
    			cout << "Create new bowler." << endl << endl
    				 << "Enter name of bowler." << endl << endl;
    			cin >> bowler;
    			cout << endl << endl;
    			Master[value] = bowler;
    			value = 0;
    			MasterlistOut.open("Masterlist.txt");
    			for (value = 0; value < control - 1; value++)
    			{
    				MasterlistOut << Master[value] << endl;
    			}
    			MasterlistOut << bowler << endl;
    			MasterlistOut.close();
    			system("cls");
    here's my first problem.... first, a menu comes up with the choice of entering a new bowler, adding scores, looking at the scores, etc.... this is the part for adding a new bowler.... planning on having the program keep a master list with all the bowler names.... each name will be linked to a file with the scores for that bowler.... this part creates and fills the master list.... my problem is, when I run this part, it opens the file, pulls the names out, adds the new bowler to the bottom of the array I have the list going into, and then puts them back into the file.... It then brings up the option to exit the program or return to the menu.... If I return to the menu and run the section to add another bowler new bowler, it erases the names in the file and adds the new one.... Adding a bowler, exiting, opening the program again and adding another bowler works fine, but that's a waste of time.... I might as well just open the file and type everyone's name in it.... Is there a way around this? I've been out of my c++ class for almost 2 years, always had problems with this kind of thing, though....

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    13
    thinking that changing the while command to another one and adding EOF in there somewhere might fix it, but I'm not completely sure.... there's no point in trying something if I don't know if it'll work and risk messing up what I already have when there is a way around it....

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    If I understand it correctly, the program keeps overwriting the old data, right. Well this how you do it to make sure that what you write will be *appended* at the end of file.
    Code:
    ofstream fout( "filename.txt", ios::app );
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    13
    lol, it only overwrites the old data if I run the same option more than once without closing the program.... I'll give it a try, though.... thanks....

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    13
    looks like it works, thanks....

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    13
    lol, problem.... I'm sure its because I'm pulling everything out and then putting it back inwith the new names, but it copies the original list, adds the new names and puts everything back into the file after the original list....

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    13
    lol, yeah.... fixed that, too.... thanks....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading and writing files
    By jcafaro10 in forum C Programming
    Replies: 8
    Last Post: 04-08-2009, 11:36 PM
  2. writing and reading files
    By oldie in forum C Programming
    Replies: 1
    Last Post: 07-03-2008, 05:54 PM
  3. Reading and Writing from files
    By dutch's finest in forum C Programming
    Replies: 8
    Last Post: 03-18-2004, 04:34 AM
  4. reading and writing to files in windows
    By Dohojar in forum Windows Programming
    Replies: 8
    Last Post: 06-26-2003, 03:07 AM
  5. Reading binary files and writing as text
    By thenrkst in forum C++ Programming
    Replies: 8
    Last Post: 03-13-2003, 10:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21