C++ C++;将字符串写入文本文件中的一行;新行问题\n不起作用
我正在编写一个具有许多功能(读、写、删除、搜索、登录等)的数据库程序,而我的写作功能刚刚停止工作(3天前还在工作),我不知道发生了什么变化。我的写入函数(void savescore)应该写入我的输入(cin用户名和密码),然后移动到下一行,这样我可以在下次决定写入文件时输入更多信息。现在它只是在写我上一次写的东西 test2.txt- 用户名、密码 然后我去编辑并输入“用户,通过”,这就是发生的情况 test2.txt-用户,通过 我想让它在下一行输入,我做了“\n”有人能给我一些帮助吗?谢谢 代码:C++ C++;将字符串写入文本文件中的一行;新行问题\n不起作用,c++,database,file,ifstream,C++,Database,File,Ifstream,我正在编写一个具有许多功能(读、写、删除、搜索、登录等)的数据库程序,而我的写作功能刚刚停止工作(3天前还在工作),我不知道发生了什么变化。我的写入函数(void savescore)应该写入我的输入(cin用户名和密码),然后移动到下一行,这样我可以在下次决定写入文件时输入更多信息。现在它只是在写我上一次写的东西 test2.txt- 用户名、密码 然后我去编辑并输入“用户,通过”,这就是发生的情况 test2.txt-用户,通过 我想让它在下一行输入,我做了“\n”有人能给我一些帮助吗?谢谢
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <fstream>
#include <conio.h>
#include <string>
#include <math.h>
using namespace std;
// Variables
string username;
string password;
//alphabet order functions
// Functions
void SaveScore()
{
ofstream Database;
Database.open("test2.txt");
Database << username << " " << password << "\n";
Database.seekp(0,std::ios::end); //to ensure the put pointer is at the end
Database.close();
}
int main()
{
int db;
char ans;
string save;
string file;
ifstream fin;
ofstream fout;
string searchpar;
char repeat;
bool loop = true;
while (loop == true)
{
cout << "WELCOME TO MY DATABASE\n\n";
cout << "To view the database, press 1\nTo edit the database, press 2\nTo search the database, press 3\nTo log in, press 4\n";
cin >> db;
system("CLS");
if (db == 1)
{
cout << "Here is the database: \n\n";
string line;
ifstream myfile("test2.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << '\n';
}
}
//open while bracket
cout << "\n\nWould you like to return to the menu(y/n)?";
cin >> repeat;
if (repeat == 'y')
{
loop = true;
}
else if (repeat == 'n')
{
loop = false;
}
system("CLS");
}
else if (db == 2)
{
cout << "Please enter your username : ";
cin >> username;
cout << "\nPlease enter your password: ";
cin >> password;
SaveScore();
cout << "\n\nWould you like to return to the menu(y/n)?";
cin >> repeat;
if (repeat == 'y')
{
loop = true;
}
else if (repeat == 'n')
{
loop = false;
}
system("CLS");
}
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//变数
字符串用户名;
字符串密码;
//字母顺序函数
//功能
void SaveScore()
{
流数据库;
Database.open(“test2.txt”);
数据库你说你的程序
每次我尝试向文本文件中写入新内容时,都会替换文本文件的第一行
事实证明,这正是你要求它做的。考虑一下:
Database << username << " " << password << "\n";
Database.seekp(0,std::ios::end); //to ensure the put pointer is at the end
在这种情况下,您可以完全忽略对seekp
的调用,因为所有数据都将自动写入文件末尾。有关此操作的完整文档,请参阅。您说您的程序是
每次我尝试向文本文件中写入新内容时,都会替换文本文件的第一行
事实证明,这正是你要求它做的。考虑一下:
Database << username << " " << password << "\n";
Database.seekp(0,std::ios::end); //to ensure the put pointer is at the end
在这种情况下,您可以完全忽略对seekp
的调用,因为所有数据都将自动写入文件末尾。有关此操作的完整文档,请参阅。数据库.close()
删除此项,太早了。也没有必要。流
,并将在销毁时为您关闭。您希望如何在数据库.close()之后执行数据库.seekp()
?您认为数据库.close()怎么样
does?建议:保留代码的多个版本。如果您喜欢,请使用版本控制系统。这样,当某些内容突然停止工作时,您可以与以前的版本进行比较,看看您更改了什么。“不知道更改了什么”-使用源代码管理!然后您可以返回并查看更改的内容,或者查看以前的工作版本。数据库。关闭();
删除此项,太早了。也没有必要。流的,并将在销毁时为您关闭。您希望如何数据库。请在数据库之后查看kp()
。关闭()
?您认为数据库.close()的作用是什么?建议:保留多个版本的代码。如果您喜欢,请使用版本控制系统。这样,当某些内容突然停止工作时,您可以与以前的版本进行比较,看看您更改了什么。“不知道更改了什么”-使用源代码管理!然后您可以返回并查看更改的内容,或者查看以前的工作版本。非常感谢!我最终使用了数据库。open(“test2.txt”,std::ios::app);它成功了!再次感谢大家的帮助。非常感谢!我最终使用了数据库。open(“test2.txt”,std::ios::app);成功了!再次感谢大家的帮助。