将行中的字段从textfile中更改为存储数组,而不是重新编写文件
change a field in a line from textfile, storing into array than re-writing into a file
我的程序分为两个参数,第一个参数是要更改的名称,第二个参数是更改它的名称。
执行后,我的textfile缺少第二行,这是从5月到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;
您正在阅读empty3
两次,而将empty4
空置为空:
getline(file, empty, ';');
getline(file, empty2, ';');
getline(file, empty3, ';'); // <--- here
getline(file, name, ';'); //
getline(file, empty3); // <--- and here (this is supposed to be empty4, right?)
然后您有一个没有效果的声明(乍得·坎贝尔已经指出):
store[i];
最后,一个不正确的条件: while(!file.fail())
-当流还不错时,您输入最后一次迭代 - 您已经阅读了所有数据,但尚未尝试读取末端,这将使流处于不良状态 - 下一步读取失败,但您不检查它,并默默地重新使用上一个迭代中的数据。
始终在某些布尔上下文(例如while(getline(...))
)中使用输入操作,或者至少在尝试读取后检查流。在您的情况下,类似的事情:
while (true)
{
getline(file, empty, ';');
getline(file, empty2, ';');
getline(file, empty3, ';');
getline(file, name, ';');
getline(file, empty3);
if (!file) break;
// do stuff with data
}
相关文章:
- 如何将内容数组写入文本文件?
- C++将文本文件中的数据读取到结构数组中
- 将值从二维数组输出到文本文件
- C++数据文件、数组和计算赋值
- 如何拆分文件中.txt字母并使用c ++使用数组进行扑克?
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 如何从txt文件中读取多个不同长度的数组?
- 保存/加载大量短数组到二进制文件
- 从二进制文件中读取整数数组
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- C++编程从外部文本文件定义数组大小
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 如何将文本文件的特定行读取到 int 类型的数组中C++?
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 将系数存储在头文件的数组中("does not name a type"错误)
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 从文件读取,并写入函数C++中的数组
- 如何从具有两列的.txt文件创建并行数组?
- 读取二维数组文件