void 写入文本(string 文件名, string 内容)
{
std::ofstream 文件(文件名, std::ios::app);
文件 << 内容;
}
void 读取文本(string 文件名, string& 内容)
{
string 读 = ""; int 正数 = 0, 负数 = 0;
std::ifstream 文件(文件名);
if (!文件.eof())
{
while (getline(文件, 读))
if (atoi(读.c_str())>0)正数 += atoi(读.c_str());
else 负数 += atoi(读.c_str());
//cout << 正数 << "正负" << 负数 << endl; //不包括每行的换行符
内容 = "正数" + to_string(正数) + "负数" + to_string(负数);
文件.close();
}
else
cout << "温馨小提示,麻油此文件" << endl;
}
string 读文本 = "D:\\DW visual studio 2017\\Projects\\C++2013";
读取文本("..\\Debug\\回复专用\\数字例子.txt", 读文本);
cout << 读文本 << endl;
string rm[12] = { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" };
string t;//缘由https://bbs.csdn.net/topics/396241391?page=1#post-411092125
int month;
int year;
int day;
ifstream is("..\\Debug\\回复专用\\日期数据.txt", ios_base::in);
if (!is.eof())
{
char temp[11]{};
while (is.getline(temp, 11, ';'))
{
cout << temp << endl;
t = temp;
//cout << t << endl;getline(is, t)
day = stoi(t.substr(0, 2));
month = stoi(t.substr(3, 2));
year = stoi(t.substr(6, 4));
cout << year << " " << rm[month - 1] << " " << day << endl;
}
}
else
{
cerr << "error";
exit(EXIT_FAILURE);
}
string t = "缘由https://bbs.csdn.net/topics/396241399";
char temp[11]{};
vector<string> arr;
ifstream is("..\\Debug\\回复专用\\矩阵数据.txt", ios_base::in);
if (!is.eof())
{
while (is.getline(temp, 11, ','))
{
//cout << temp << ends;
arr.push_back(string(temp));
}
cout << endl;
std::for_each(arr.begin(), arr.end(), [](const string& n) {cout << n << ends; });
}
else
{
cerr << "error";
exit(EXIT_FAILURE);
}
void txt文件修改后输出到另一个txt文件()
{//缘由https://bbs.csdn.net/topics/396342833
string 读 = "", 内容 = "", 临时 = "";
int 位置 = 0;
std::ifstream 读文件("..\\Debug\\回复专用\\原始数据.txt");
std::ofstream 写文件("..\\Debug\\回复专用\\整理原始数据.txt", std::ios::app);
if (!读文件.eof())
{
while (getline(读文件, 读))
{
读.replace(读.find(' '), 1, ",");
while ((位置 = 读.find(',')) != string::npos)
{
临时 = 读.substr(0, 位置);
内容 += 临时.substr(2);
读 = 读.substr(++位置);
}
while ((位置 = 读.find('"')) != string::npos)读.replace(位置, 1, "");
内容 += "\n" + 读 + "\n";
写文件 << 内容;
内容 = "";
}
读文件.close();
cout << "数据转换完成!" << endl;
}
else
cout << "温馨小提示,麻油此文件" << endl;
}
std::ifstream 读文件("..\\Debug\\回复专用\\捐款数据.txt");
int n;//缘由https://bbs.csdn.net/topics/396510443
(读文件 >> n).get();
vector<donor> ar(n);
for (int i = 0; i<n; ++i)//文本文件能成功打开,但是这里为啥读不到里面的数据????
{
getline(读文件, ar[i].name);
(读文件 >> ar[i].money).get();
}
cout << "Grand Patrons:" << endl;
int m = 0;
for (int i = 0; i<n; ++i)
if (ar[i].money >= 10000)
{
cout << ar[i].name << ends << ar[i].money << endl;
++m;
}
if (m == 0)
cout << "None!" << endl;
m = 0;
cout << "Patrons:" << endl;
for (int i = 0; i<n; ++i)
if (ar[i].money<10000)
{
cout << ar[i].name << ends << ar[i].money << endl;
++m;
}
if (m == 0)
cout << "None!" << endl;
读文件.close();
