C++ 将数据存储到阵列中的问题
我的程序允许用户在输入用户名时删除指定记录。当它们传入名称时,它将调用一个方法,该方法将它们存储到一个数组中,并将它们写回文件而不追加,以便重新写入文件。 但是在存储部分出现了一些问题,我的文本文件的最后一行没有正确存储,而是从最后一行的第二行复制到最后一行并包含名称。 希望1号会感到困惑:/。下面是存储在我的数组中的文本文件和数据的示例,我用粗体和斜体表示更清晰的图片,还有deleteRec的方法 这是我的文本文件包含的内容C++ 将数据存储到阵列中的问题,c++,arrays,fstream,C++,Arrays,Fstream,我的程序允许用户在输入用户名时删除指定记录。当它们传入名称时,它将调用一个方法,该方法将它们存储到一个数组中,并将它们写回文件而不追加,以便重新写入文件。 但是在存储部分出现了一些问题,我的文本文件的最后一行没有正确存储,而是从最后一行的第二行复制到最后一行并包含名称。 希望1号会感到困惑:/。下面是存储在我的数组中的文本文件和数据的示例,我用粗体和斜体表示更清晰的图片,还有deleteRec的方法 这是我的文本文件包含的内容 user;pass;1234;John;1111 user1;pas
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
这是我运行要删除的程序时的输出
Data stored in store[] array: user1;pass1;2345;May;2222
Data stored in store[] array: user2;pass2;3456;Mary;3333
Data stored in store[] array: user3;pass3;4567;Andy;4444
Data stored in store[] array: hr;hr;5678;Jonathan;5555
Data stored in store[] array: admin;admin;6789;Aili;6666
Data stored in store[] array: user10;pass10;7890;eggy;9999
***Data stored in store[] array: ;pass10;7890;eggy;9999***
Data stored in store[] array:
bool Employee::deleteRec(string nm)
{
int count;
int i=0;//for looping
ifstream file("login1.txt");
string fusername,empty;
string store[100];//initialize a array to store textfile contents
while (!file.fail())
{
getline(file,fusername,';');// use ; as delimiter
getline(file,empty);// use line end as delimiter, and to skip the rest of the information
string add="";//initialize add string to nothing when it loops
add += fusername+';'+empty; //adds back the username and rest of the line together back
if(fusername!=nm)//to check if the username in textfile do not match the user input name
{
store[i]=add; //store into an array
cout<<"i is: "<<i<<endl;
cout<<"store array[] = "<<store[i]<<endl;
i++;
}
else{}
}
//ofstream pwd2_file ("login1.txt", ios::app); //suppose to user this if im writing to file
for(int x=0;x<i+1;x++)
{
cout<<"Data stored in store[] array: "<<store[x]<<endl;
}
return false;
}
bool Employee::deleteRec(字符串nm)
{
整数计数;
int i=0;//用于循环
ifstream文件(“login1.txt”);
串熔丝名称,空;
字符串存储[100];//初始化数组以存储文本文件内容
而(!file.fail())
{
getline(文件,fusername,“;”);//使用;作为分隔符
getline(file,empty);//使用行尾作为分隔符,并跳过其余信息
string add=“”;//在循环时将add string初始化为nothing
add+=fusername+';'+empty;//将用户名和行的其余部分一起添加回
if(fusername!=nm)//检查textfile中的用户名是否与用户输入的名称不匹配
{
store[i]=add;//存储到数组中
cout循环的问题是,当您到达文件末尾时,您的流还没有失败。它只在下一次读取时失败,但您没有验证这一点
因此,数组包含最后一条记录两次
您有一个空字符串作为第一个字段,可能是因为它将此字符串设置为空以便读入(流尚未处于失败状态),或者是因为您的输入文件末尾有一个空行被读入
为您的用户及其数据创建一个结构,并从流中读入。如果整个读取成功,您可以附加到数据集
我建议您对此使用std::vector
和push_back()
正确的循环方式如下所示:
struct EmployeeData
{
std::string name;
// fill in members
;
std::istream& operator>>( std::istream& is, EmployeeData& emp )
{
std::getline( is, emp.name(), ';' );
// etc.
return is;
}
std::vector< EmployeeData > emps;
EmployeeData emp;
while( is >> emp )
{
emps.push_back( emp );
}
struct EmployeeData
{
std::字符串名;
//填写成员
;
std::istream&operator>>(std::istream&is、员工数据和emp)
{
std::getline(is,emp.name(),“;”);
//等等。
回报是;
}
std::vectorEMP;
EmployeeData emp;
while(is>>emp)
{
电磁脉冲推回(emp);
}
循环的问题是,当您到达文件末尾时,您的流还没有失败。它只在下次读取时失败,但您没有验证这一点
因此,数组包含最后一条记录两次
您有一个空字符串作为第一个字段,可能是因为它将此字符串设置为空以便读入(流尚未处于失败状态),或者是因为您的输入文件末尾有一个空行被读入
为您的用户及其数据创建一个结构,并从流中读入。如果整个读取成功,您可以附加到数据集
我建议您对此使用std::vector
和push_back()
正确的循环方式如下所示:
struct EmployeeData
{
std::string name;
// fill in members
;
std::istream& operator>>( std::istream& is, EmployeeData& emp )
{
std::getline( is, emp.name(), ';' );
// etc.
return is;
}
std::vector< EmployeeData > emps;
EmployeeData emp;
while( is >> emp )
{
emps.push_back( emp );
}
struct EmployeeData
{
std::字符串名;
//填写成员
;
std::istream&operator>>(std::istream&is、员工数据和emp)
{
std::getline(is,emp.name(),“;”);
//等等。
回报是;
}
std::vectorEMP;
EmployeeData emp;
while(is>>emp)
{
电磁脉冲推回(emp);
}
我建议使用调试器,只需找出传递的内容。在输出存储在数组中的位置或将其重写到文件时,看起来像是一个逐个错误。这可以解释您缺少边界用户的原因。我建议使用调试器,只需找出传递的内容。看起来像在输出存储在数组中的位置或将其重写到文件中时出现一个off-by-one错误。这可以解释您缺少边界用户的原因。有一件事,不要使用例如while(!file.fail())
。改用while(getline(…)
。@joachim pileborg所以在((getline(cin,nm))时可以这样做吗?有一件事,不要使用while(!file.fail())
。而是使用while(getline(…)
。@joachim pileborg那么在((getline(cin,nm))时这个方法会起作用吗?在你的例子中,有没有一种方法可以代替?因为你在()while(getline(…)&&getline(…)时,每次在循环中做两个getline(){
。我只是很惊讶,我真的很惊讶。@cashcow实际上我已经有了一个字符串名在里面的类,但我认为用class我仍然可以做和你现在做的一样的事情?我使用struct和public成员使它更简单。你可以使用class和private成员,但必须让operator>>成为朋友。实际上我更喜欢使用s如果我想添加功能,请将结构的一个成员作为私有数据成员放入类中。在您的例子中,有没有方法代替?因为您每次在循环中执行两个getline(getline(…)&&getline(…){
。我只是很惊讶,我真的很惊讶。@cashcow实际上我已经有了一个字符串名在里面的类,但我认为用class我仍然可以做和你现在做的一样的事情?我使用struct和public成员使它更简单。你可以使用class和private成员,但必须让operator>>成为朋友。实际上我更喜欢使用s如果我想添加功能,那么就将结构的一个成员作为私有数据成员放入类中。