我在程序中遇到问题,我想删除所有内容并重新输入,但是我的文本文件先前的内容不会删除。我想知道如何解决。
我的文本文件格式:
jam:A7D579BA76398070EAE654C30FF153A4C273272A:9BB8F747A1C0DEA66C6FAF43391FEEAB:7F273F63581093D2:08102018: kelly:7C222FB2927D828AF22F592134E8932480637C0D:06FE625E35A95669B866B914ABE818DE:7F273F63581093D2:08102018:
我的代码:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <ctime>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
string SHA_1(const string& input_data)
{
string digest;
SHA1 hash;
StringSource s(input_data, true, new HashFilter(hash, new HexEncoder(new StringSink(digest))));
return digest;
}
void outfile (string filename, string u, string p , string k, string iv, string d)
{
ofstream outfile, outfile_2;
outfile_2.open ("Account.txt", ios::out | ios::app);
outfile_2 << u + ":" + p + ":" + ":" + k + ":" + iv + ":" + d + ":";
outfile_2.close();
}
int main()
{
ifstream infile_Account;
infile_Account.open("Account.txt", ios::in);
string UserAccount;
string KEY_2;
string IV_2;
string Username = "kelly";
string Password = "12345678";
string Hashed_Password = SHA_1(Password);
int i = 0;
int j = 0;
if (!infile_Account)
{
cout << "File " << endl;
return 0;
}
while(infile_Account >> UserAccount)
{
//cout << UserAccount << endl;
i++;
}
infile_Account.close();
string u, p , k , iv ,d;
cout << i << endl;
ifstream infile_Account_2;
infile_Account_2.open("Account.txt", ios::in);
ofstream outfile1;
for (int j =0; j<i ; j++)
{
getline(infile_Account_2,u, ':');
getline(infile_Account_2,p, ':');
getline(infile_Account_2,k, ':');
getline(infile_Account_2,iv, ':');
getline(infile_Account_2,d, ':');
/*cout << u << endl;
cout << p << endl;
cout << k << endl;
cout << iv << endl;
cout << d << endl;*/
if (u == Username && p == Hashed_Password )
{
p = "78965478";
}
outfile("Account.txt",u,p,k,iv,d);
}
}
执行程序后文件的结果:
kelly:7C222FB2927D828AF22F592134E8932480637C0D:06FE625E35A95669B866B914ABE818DE:7F273F63581093D2:08102018:
jam:A7D579BA76398070EAE654C30FF153A4C273272A:9BB8F747A1C0DEA66C6FAF43391FEEAB:7F273F63581093D2:08102018:
kelly:78965478:06FE625E35A95669B866B914ABE818DE:7F273F63581093D2:08102018:
jam:A7D579BA76398070EAE654C30FF153A4C273272A:9BB8F747A1C0DEA66C6FAF43391FEEAB:7F273F63581093D2:08102018:
答案 0 :(得分:3)
您正在使用ios::app
打开文件。因此,您要追加数据。 Juste删除标志...并阅读文档。