0

Working on a program dealing with file stream. I am wanting to edit a line of text that is stored in a text file. To my understanding, the best way to do this would be to copy everything from the original file and over-write it in a new file, making my edits along the way. I haven't been able to find a clear example online, and was wondering if I could see one.

example text file contains:

user1 pass1 55

user2 pass2 56

user3 pass3 57

I would like to edit to to where it has:

user1 pass1 55

user2 pass2 44

user3 pass3 57

Here is my abridged code for context:

#include <iostream>
#include <fstream>

using namespace std;
//function prototypes
void addUser();
void part1();
int main();

//global variables
    string username;
    string password;
    int balance;
void addUser(){
    //open a file in write mode
        ofstream myfile;
        myfile.open("userinfo.txt", ios::app);//append the text file
        if(myfile.is_open()){
        cout<<"Please Create a Username: ";
        string inputCheck;//a string to compare name
        cin>>inputCheck;
        cout<<"Please Create a Password: ";
        cin>>password;
        cout<<"Current Balance: ";
        cin>>balance;
        myfile<<inputCheck<<' '<<password<<' '<<balance<<"\n";
        myfile.close();
        cout<<"User account '"<<inputCheck<<"' has been created"<<endl;
        }//ends create a password else
    part1();
}

void part1()
{
    cout<<"1.Add User\n2.Edit Information"<<endl;
    int input;
    cin>>input;
    if(input == 1){
        addUser();
    }else if(input == 2){
    //find the user
    cout<<"Enter the username: ";
    string checkUser;
    cin>>checkUser;
    ifstream infile("userinfo.txt"); //CREATING IFSTREAM
    ofstream outfile("pleasework.txt");
    bool foundUser = false;
    while(infile>>username>>password>>balance){
        if(checkUser==username){
        foundUser = true;
        break;
        }
    }
    if(foundUser){
    cout<<"Current Balance: "<<balance<<endl;
    cout<<"Enter new balance: ";
    int newBalance;
    cin>>newBalance;    
    if(infile.is_open()){
        outfile<<username<<' '<<password<<' '<<newBalance<<endl;
        //How do I get all of the other unedited accounts in here?!?!?!?!
        }
    }//end input 2
        infile.close();
outfile.close();
    }

}//end part 1

int main(){
    part1();
}
cparks10
  • 377
  • 2
  • 14
  • 1
    Never call main. http://stackoverflow.com/questions/2128321/can-main-function-call-itself-in-c – drescherjm Dec 06 '16 at 20:56
  • why you declare main??? it's already declared and overloaded. the catastrophe is if you call main inside your function and normally calling that function from main the result is an endless calling like an infinite loop or endless recursive – Raindrop7 Dec 06 '16 at 21:01
  • 1
    @Raindrop7 Understood. I will put it in a different function later. For now, the program is running and doing what I need it to do. Thank you for the information on why calling main is a bad thing to do and can lead to things like catastrophes and infinite loops – cparks10 Dec 06 '16 at 21:07
  • @cparks10: ok that is it – Raindrop7 Dec 06 '16 at 21:11

2 Answers2

2

Calling

main();

recursively leaves you with undefined behavior. So you cannot expect anything reliably.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
2

Here is some pseudocode that might help:

Method 1:

open existing file in 'read' mode
open new file in 'write' mode
for each line in existing:
    if line doesn't need update:
        write line to new file
    else:
        make changes to values from line
        write updated line to new file
close existing file
close new file
move new file over existing file

Method 2:

open existing file in 'read/write' mode
data = array of lines
for each line in existing:
    read values into data
clear existing file
add/delete rows in data if necessary
for each row in data:
    update values in row
    write updated line to existing file
close existing file
0x5453
  • 12,753
  • 1
  • 32
  • 61
  • @0x543 Thanks! The psuedocode really helped to visualize it and it solved my problem. However, I am still stuck on moving the new file over the existing file, is there somewhere you could point me that would have an example? Thanks! – cparks10 Dec 07 '16 at 05:01
  • I believe you can use `rename` (include ``) on Mac/Linux, and `MoveFile` (include ``) on Windows. – 0x5453 Dec 07 '16 at 13:45