![](/img/trans.png)
[英]C++: Strange numbers when outputting result of counting frequency of ASCII characters in a file
[英]Strange result when editing file in C++
几周以来,我一直在为Xbox游戏《使命召唤:现代战争3》开发随机类生成器。在此游戏中,不同的武器具有不同的等级,随着您使用武器的增加,等级会增加。 我将武器及其等级存储在一个文本文件中,该文件以以下格式将数据存储在单独的行中
weapon-weapon_level
因此,武器级别为8的M4A1如下所示:
m4a1-8
(武器全部为小写字母,没有标点符号或空格)。
我已经编写了用于创建文件和读取文件的方法,但是我想要一种用于编辑文件的方法,因此用户输入他们想要更改其级别的武器,然后输入新级别。 到目前为止,这是我所拥有的:(该文件称为“ weaponlevels.txt”)
void WeaponLevelFile::editFile()
{
string line;
string weapon;
string weaponent;
string weaponlevel;
string temp;
cout<<"Please enter the weapon whose level you wish to change. Enter the name in lowercase, with "<<endl;
cout<<"no spaces or punctuation except full stops. E.g. SCAR-L becomes scarl and Barrett .50cal "<<endl;
cout<<"becomes barrett.50cal."<<endl;
cin>>weaponent;
cout<<"Please enter the new weapon level."<<endl;
cin>>temp;
ifstream infile("weaponlevels.txt");
ofstream outfile("weaponlevels.txt");
while (getline(infile, line))
{
istringstream ss(line);
getline(ss,weapon,'-');
if (weapon == weaponent)
{
ss>>weaponlevel;
weaponlevel=temp;
outfile<<weaponlevel<<endl;
infile.close();
outfile.close();
}
}
}
但是,此方法无效。 它所做的就是擦除文件(因此文件为空白)。 为什么这样做,还有什么更好的方法?
编辑:@stardust_的答案效果最好,但仍然没有完全做到这一点。 这是代码:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
string temp;
string line;
string weapon;
string weaponent;
string weaponlevel;
cout<<"enter weapon"<<endl;
cin>>weaponent;
cout<<"enter level"<<endl;
cin>>temp;
ifstream infile("weaponlevels.txt");
std::string in_str((std::istreambuf_iterator<char>(infile)),
std::istreambuf_iterator<char>());
infile.close();
stringstream infile_ss(in_str);
while (getline(infile_ss, line))
{
istringstream ss(line);
getline(ss,weapon,'-');
if (weapon == weaponent)
{
ss>>weaponlevel;
weaponlevel=temp;
infile_ss<<weaponlevel<<endl;
}
}
ofstream outfile("weaponlevels.txt");
outfile << infile_ss.str();
outfile.close();
}
它修改了“ weaponlevels.txt”的右侧部分,但没有完全做到这一点。 如果我输入m4a1
作为武器,输入7
作为武器等级,而不是变成m4a1-7
它将变为:
7
a1-3
经过大量工作之后,以下是可行的方法:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
string temp;
string line;
string weapon;
string weaponent;
string weaponlevel;
cout<<"enter weapon"<<endl;
cin>>weaponent;
cout<<"enter level"<<endl;
cin>>temp;
ifstream infile("weaponlevels.txt");
std::string in_str((std::istreambuf_iterator<char>(infile)),
std::istreambuf_iterator<char>());
infile.close();
stringstream infile_ss(in_str);
ostringstream out;
while (getline(infile_ss, line))
{
istringstream ss(line);
getline(ss,weapon,'-');
out << weapon << '-'; // Write the first part of the line.
if (weapon != weaponent)
{ // Not the correct weapon so just write the original information.
ss >> weaponlevel;
out << weaponlevel << endl;
}
else
{ // Found the desired weapon, change the level.
out << temp << endl;
}
}
我将整个字符串加载到ostringstream
并在字符串中找到了武器。
您是否尝试过使用指针在文件中搜索要替换的特定字符串? 我尚未编写自己使用此程序的程序,但我知道它很常见。 否则,您将只覆盖整个文件。
请注意,我不能完全确定您是否为此目的在代码中使用了指针,因为如上所述,我还没有自己使用过该指针。 但是,从我的观察来看,我认为您没有。 如果我错了,就纠正我。
您的代码有很多问题。 您正在打开文件两次。 您正在读取时在循环中关闭ifstream(?)。 ...
但是,从算法角度来看,您可以打开文件进行读取,将整个内容读取为字符串,关闭文件,修改字符串,打开文件进行写入,写入字符串,关闭文件。
int main()
{
cin>>temp;
ifstream infile("weaponlevels.txt");
std::string in_str((std::istreambuf_iterator<char>(infile)),
std::istreambuf_iterator<char>());
infile.close();
stringstream infile_ss(in_str);
while (getline(infile_ss, line))
{
istringstream ss(line);
getline(ss,weapon,'-');
if (weapon == weaponent)
{
ss>>weaponlevel;
weaponlevel=temp;
infile_ss<<weaponlevel<<endl;
}
}
ofstream outfile("weaponlevels.txt");
outfile << infile_ss.str();
outfile.close();
}
代替
ifstream infile("weaponlevels.txt");
采用
ifstream infile("weaponlevels.txt", ifstream::in)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.