在 C++ 中重写文本文件
Rewriting a text file in 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();
}
通话中
main();
递归地给你留下未定义的行为。所以你不能指望任何可靠的东西。
下面是一些可能有用的伪代码:
方法一:
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
方法二:
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
正在研究处理文件流的程序。我想编辑存储在文本文件中的一行文本。据我所知,最好的方法是复制原始文件中的所有内容并将其覆盖到一个新文件中,同时进行我的编辑。我一直无法在网上找到一个明确的例子,想知道我是否可以看到一个。
示例文本文件包含:
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();
}
通话中
main();
递归地给你留下未定义的行为。所以你不能指望任何可靠的东西。
下面是一些可能有用的伪代码:
方法一:
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
方法二:
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