c++ - 从文本文件中更改一行中的字段,存储到数组中,然后重新写入文件

标签 c++ arrays stream

我的程序有两个参数,第一个参数是要更改的名称,第二个参数是要更改的名称。

执行后,我的文本文件缺少第二行,假设从 May 更改为 MayMay。我不确定是我的 if else 语句有问题还是 while(!file.fail) 有问题。

这是我的原始文本文件。

user;   pass;   1234; John;     1111
user1;  pass1;  2345; May;      2222
user2;  pass2;  3456; Mary;     3333
user3;  pass3;  4567; Andy;     4444
hr;     hr;     5678; Jonathan; 5555
admin;  admin;  6789; Aili;     6666
user10; pass10; 7890; eggy;     9999
user11; pass11; 9807; Mary;     7777

这是程序执行后我的输出文本文件。

user;   pass;   1111;  John;

user2;  pass2;  3333;  Mary;
user3;  pass3;  4444;  Andy;
hr;     hr;     5555;  Jonathan;
admin;  admin;  6666;  Aili;
user10; pass10; 9999;  eggy;
user11; pass11; 7777;  Mary;

        pass11; 7777;  Mary;

这是我的代码:

bool Employee::changeName(string nnn, string mmm)
{
    int i = 0;
    ifstream file("login1.txt"); // to open the file
    string name, empty, empty2, empty3, empty4; // fusername is use to store the first parameter in textfile,empty is use to store the rest of the line after the ';'
    string store[100]; // initialize a array to store textfile contents

    while (!file.fail()) // loop if file didn't fail
    {   
        getline(file, empty, ';'); // use ; as delimiter
        getline(file, empty2, ';'); // use ; as delimiter
        getline(file, empty3, ';'); // use ; as delimiter
        getline(file, name, ';'); // use ; as delimiter
        getline(file, empty3); // use line end as delimiter, and to skip the rest of the information
        string add = ""; //initialize add string to nothing when it loops

        if(name != nnn) // to check if the username in textfile do not match the user input name
        {
            add += empty + ';' + empty2 + ';' + empty3 + ';' + name + ';' + empty4; // adds back the username and rest of the line together back    
            store[i] = add; // store into an array
            cout << "i is: " << i << endl; // debugging.
            cout << "store array[] = " << store[i] << endl; // debugging..
            i++;
        }
        else if(name == nnn)
        {
            add += empty + ';' + empty2 + ';' + empty3 + ';' + mmm + ';' + empty4; // adds back the name and rest of the line together back 
            store[i];
            cout << "i is: " << i <<endl; // debugging.
            cout << "store array[] = " << store[i] << endl; // debugging..
            i++;
        }
        else{}  
    }

    remove("login1.txt"); //remove the textfile
    ofstream pwd2_file ; // initilize a outputstream textfile
    pwd2_file.open("login1.txt"); // create a new file call login1.txt

    for (int x = 0; x < i; x++)//for loop to store store[] array into login1.txt
    {
        pwd2_file << store[x] << endl; // storing into textfile
    }
    pwd2_file.close(); // close the output stream
}

最佳答案

导致空行的问题是,当您替换 else-if 语句中的名称时,您有这些行

add += empty+';'+empty2+';'...
store[i];

我猜应该是

store[i] = add;

关于c++ - 从文本文件中更改一行中的字段,存储到数组中,然后重新写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13055344/

相关文章:

swift - audioStreamer 快速错误 cocoa.h

python - Python 中有 FileIO 吗?

c++ - 为什么类的析构函数在只包含类的头文件的cpp中是 "inlined"

c++ - 从另一个 constexpr std::array 初始化没有默认构造函数的对象的 std::array

python - 如何将矩阵中具有不等长度的项的值分组

java - 无法从 long[] 转换为 long

arrays - 如何创建带有随机数的数组?

c# - 字节数组的缓冲流

c++ - LOBYTE(nValue) 和 LOBYTE(LOWORD(nValue)) 区别?

c++ - 标准库中的双向链接循环列表