2016-12-06 62 views
0

使用一個處理文件流的程序。我想編輯存儲在文本文件中的一行文本。根據我的理解,最好的方法是從原始文件中複製所有內容並將其覆蓋在一個新文件中,並在編輯過程中進行編輯。我一直無法在網上找到一個清晰的例子,並想知道我能否看到一個例子。用C++重寫一個文本文件

例如文本文件包含:

user1 pass1 55 

user2 pass2 56 

user3 pass3 57 

我想編輯對地方有:

user1 pass1 55 

user2 pass2 44 

user3 pass3 57 

這裏是我的情況下刪節代碼:

#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(); 
} 
+1

,一定不要調用主。 http://stackoverflow.com/questions/2128321/can-main-function-call-itself-in-c – drescherjm

+0

爲什麼你聲明主?它已經被聲明和超載。這個災難就是如果你在你的函數中調用main函數,並且通常從main函數調用該函數是一個像無限循環或無限遞歸的無盡調用 – Raindrop7

+1

@ Raindrop7瞭解。我將在稍後介紹其他功能。目前,該計劃正在運行,正在做我需要的事情。感謝你爲什麼稱主要是一件壞事,並可能導致災難和無限循環的事情信息 – cparks10

回答

2

下面是一些僞代碼,這可能有助於:

方法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 

方法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 
+0

@ 0x543謝謝!僞代碼真的幫助可視化它,它解決了我的問題。但是,我仍然堅持將新文件移動到現有文件上,是否有某處可以指出我會有示例?謝謝! – cparks10

+0

我相信你可以在Mac/Linux上使用'rename'(包括''),在Windows上使用'MoveFile'(包括'')。 – 0x5453

2

呼叫

main(); 

以遞歸方式給你留下未定義的行爲。所以你不能指望任何可靠的東西。

+0

Gotcha。絕對不應該叫main。這是否阻止我做我想做的事情? – cparks10

+3

@ cparks10使用循環代替可能是? –

+0

@ cparks10:「gotcha」是什麼?只要已經聲明瞭多個版本(重載),比如'int main',''void main','int main(argc,char ** argv)'......所以我們不推薦原型爲main。是多餘的 – Raindrop7