【问题标题】:How can I find a line in a text file and replace it?如何在文本文件中找到一行并替换它?
【发布时间】:2016-04-22 16:50:59
【问题描述】:

我正在为大学做一个项目,该项目要求我在文本文件中找到一个值并将其替换为我存储在浮点变量中的新值,部分问题是我不知道需要替换的项目,仅在其所在的位置(例如,我可以搜索 0.00 的值,但多条记录可能具有该余额)。

文本文件的布局如下:

用户名1
密码1
帐号1
余额1
吉米54
FooBar123
098335
13.37
...

我的代码的相关部分如下所示:

用户输入他们的用户名,我们将其存储在输入的名称中

#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),然后用存储在newBal中的值替换用户名(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();

【问题讨论】:

  • 试着写点东西,如果你遇到困难,问一个具体的问题。
  • @stark 我已经根据我的进度编辑了我的帖子,我找到了需要更改的行,但我不知道如何更改它
  • 编写文件不像在编辑器中操作它,所以你不能“替换”一行,因为如果你增加/缩短行的长度,文件的其余部分不会移动.您可以覆盖固定大小的数据,但最简单的方法是打开一个新的输出文件并复制行。

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


【解决方案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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-28
    • 2015-12-16
    • 1970-01-01
    • 2015-02-24
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    相关资源
    最近更新 更多