如何在文本文件中找到一行并替换它?
How can I find a line in a text file and replace it?
我正在为一个大学项目工作,它要求我在文本文件中找到一个值并将其替换为我存储在浮点变量中的新值,部分问题是我没有知道需要更换的物品,只知道它在哪里(例如,我可以搜索值 0.00,但多个记录可能有该余额)。
文本文件布局如下:
Username1
Password1
AccountNo1
Balance1
Jimmy54
FooBar123
098335
13.37
...
我的代码的相关部分如下所示:
用户输入他们的用户名,我们将其存储在 enteredName
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Please enter your username:\n";
cin >> enteredName;
在代码中大约 100 行,我们到达了对这个问题很重要的部分:
if (c2 == 1)
{
//irrelevant to problem
}
else if (c2 == 2)
{
float convBal;
float deposit;
string newBal;
convBal = ::atof(ball.c_str());
/* Ball is declared higher up in the code and just contains a
value pulled from the text file for the sake of the question
we'll say it has a value of 0.00 */
cout << "How much would you like to deposit?:\n";
cin >> deposit;
newBal= convBal + deposit;
cout << "Your total balance is now: "<< newBal << "\n\n";
}
}
所以从这里开始需要做的是打开文件,在文件中搜索 enteredName(在本例中我们会说 Jimmy54),然后用存储的值替换用户名 (13.37) 下面 3 行的值在新巴尔
提前致谢!抱歉,如果这听起来像一个愚蠢的问题,我是编程新手,甚至是 c++ 新手
编辑#1:
这是我到目前为止的代码,它找到 enteredName 然后得到下面的 3 行:
if (myfile.is_open())
{
while (getline(myfile, line))
{
if (line.compare(enteredName) == 0)
{
getline(myfile, line); //Password
getline(myfile, line); //Account Number
getline(myfile, line); //Balance
}
}
}
myfile.close();
这是解决我的问题的代码,它有点草率但它有效!
它只是读入一个新文件并进行任何更改,然后再次写回。
if (myfile.is_open())
{
while (getline(myfile, line))
{
if (line.compare(enteredName) == 0)
{
tempFile << enteredName << "\n"; //Username
getline(myfile, line);
tempFile << line << "\n"; //Password
getline(myfile, line);
tempFile << line << "\n"; //Account Number
getline(myfile, line);
tempFile << newBal << "\n"; //Balance
}
else
{
tempFile << line << "\n"; //Username
getline(myfile, line);
tempFile << line << "\n"; //Password
getline(myfile, line);
tempFile << line << "\n"; //Account Number
getline(myfile, line);
tempFile << line << "\n"; //Balance
}
}
}
myfile.close();
tempFile.close();
/* Had to declare these again beacuse the compiler was throwing some bizarre error when using the old declarations */
ifstream tempF("TempStore.csv");
ofstream mainF("DataStore.csv", ios::trunc);
//Writes back from TempStore to DataStore with changes made correctly
if (tempF.is_open())
{
while (getline(tempF, line))
{
mainF << line << "\n"; //Username
getline(tempF, line);
mainF << line << "\n"; //Password
getline(tempF, line);
mainF << line << "\n"; //Account Number
getline(tempF, line);
mainF << line << "\n"; //Balance
}
}
tempF.close();
mainF.close();
我正在为一个大学项目工作,它要求我在文本文件中找到一个值并将其替换为我存储在浮点变量中的新值,部分问题是我没有知道需要更换的物品,只知道它在哪里(例如,我可以搜索值 0.00,但多个记录可能有该余额)。
文本文件布局如下:
Username1
Password1
AccountNo1
Balance1
Jimmy54
FooBar123
098335
13.37
...
我的代码的相关部分如下所示:
用户输入他们的用户名,我们将其存储在 enteredName
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Please enter your username:\n";
cin >> enteredName;
在代码中大约 100 行,我们到达了对这个问题很重要的部分:
if (c2 == 1)
{
//irrelevant to problem
}
else if (c2 == 2)
{
float convBal;
float deposit;
string newBal;
convBal = ::atof(ball.c_str());
/* Ball is declared higher up in the code and just contains a
value pulled from the text file for the sake of the question
we'll say it has a value of 0.00 */
cout << "How much would you like to deposit?:\n";
cin >> deposit;
newBal= convBal + deposit;
cout << "Your total balance is now: "<< newBal << "\n\n";
}
}
所以从这里开始需要做的是打开文件,在文件中搜索 enteredName(在本例中我们会说 Jimmy54),然后用存储的值替换用户名 (13.37) 下面 3 行的值在新巴尔
提前致谢!抱歉,如果这听起来像一个愚蠢的问题,我是编程新手,甚至是 c++ 新手
编辑#1: 这是我到目前为止的代码,它找到 enteredName 然后得到下面的 3 行:
if (myfile.is_open())
{
while (getline(myfile, line))
{
if (line.compare(enteredName) == 0)
{
getline(myfile, line); //Password
getline(myfile, line); //Account Number
getline(myfile, line); //Balance
}
}
}
myfile.close();
这是解决我的问题的代码,它有点草率但它有效!
它只是读入一个新文件并进行任何更改,然后再次写回。
if (myfile.is_open())
{
while (getline(myfile, line))
{
if (line.compare(enteredName) == 0)
{
tempFile << enteredName << "\n"; //Username
getline(myfile, line);
tempFile << line << "\n"; //Password
getline(myfile, line);
tempFile << line << "\n"; //Account Number
getline(myfile, line);
tempFile << newBal << "\n"; //Balance
}
else
{
tempFile << line << "\n"; //Username
getline(myfile, line);
tempFile << line << "\n"; //Password
getline(myfile, line);
tempFile << line << "\n"; //Account Number
getline(myfile, line);
tempFile << line << "\n"; //Balance
}
}
}
myfile.close();
tempFile.close();
/* Had to declare these again beacuse the compiler was throwing some bizarre error when using the old declarations */
ifstream tempF("TempStore.csv");
ofstream mainF("DataStore.csv", ios::trunc);
//Writes back from TempStore to DataStore with changes made correctly
if (tempF.is_open())
{
while (getline(tempF, line))
{
mainF << line << "\n"; //Username
getline(tempF, line);
mainF << line << "\n"; //Password
getline(tempF, line);
mainF << line << "\n"; //Account Number
getline(tempF, line);
mainF << line << "\n"; //Balance
}
}
tempF.close();
mainF.close();