簡體   English   中英

如何在文本文件中找到一行並替換它?

[英]How can I find a line in a text file and replace it?

我正在為一個大學項目工作,該項目要求我在文本文件中找到一個值,然后用存儲在float變量中的新值替換它,部分問題是我不知道所需的項目僅在替換位置(例如,我可以搜索值為0.00,但多條記錄可以具有該余額)。

文本文件的布局如下:

用戶名1
密碼1
帳號1
平衡1
吉米54
FooBar123
098335
13.37
...

我的代碼的相關部分如下所示:

用戶輸入用戶名,我們將其存儲在enterName中

#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";

        } 
}

因此,從這里開始需要做的是打開文件,在文件中搜索enterName(在這種情況下,我們稱Jimmy54),然后用存儲在newBal中的值替換用戶名(13.37)下方的三行值。

提前致謝! 抱歉,如果這聽起來像是一個愚蠢的問題,我對編程還是相當陌生的,對於c ++來說還是比較新的

編輯#1:這是我到目前為止的代碼,它找到entryName,然后獲取下面的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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM