如何在文本文件中找到一行并替换它?

时间:2016-04-22 16:50:59

标签: c++ c++11 file-handling

我正在研究一个大学项目,它要求我在文本文件中找到一个值并将其替换为我存储在float变量中的新值,部分问题是我不知道需要替换的项目,只有它的位置(例如,我可以搜索0.00的值,但多个记录可以有这种平衡)。

文本文件的布局如下:

  

USERNAME1
  密码1
  AccountNo1
  余额1
  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行替换为newBal中存储的值

提前致谢!对不起,如果这听起来像一个愚蠢的问题,我对编程很新,甚至更新的c ++

编辑#1: 这是我到目前为止的代码,它找到了incomingName,然后得到以下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();

1 个答案:

答案 0 :(得分:1)

这是解决我的问题的代码,它有点草率但它有效!

它只是读入一个新文件并进行任何更改,然后再次回写。

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();