65,211
社区成员
发帖
与我相关
我的任务
分享int a=0;
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in("test.txt");
ofstream out("test1.txt");
char temp[100] = {0};
char temp1[3] = {0};
int count = 0;
in.getline(temp,100);
char * pch;
pch = strtok (temp," ,");
while (pch != NULL)
{
if (atoi(pch)) //如果是数字
{
count += atoi(pch);
}
else //是字母
{
out<<pch;
}
pch = strtok (NULL, " ,");
}
in.close();
out.close();
remove( "test.txt" );
rename("test1.txt","test.txt" );
cout<<count;
system("pause");
return 0;
}
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const char* pszFile = "D:\\test.txt";
ifstream infile;
infile.open(pszFile);
if (!infile)
{
cout << "Open File Error!" << endl;
return 0;
}
vector<string> vecString;
vector<int> vecInt;
string strValue = "";
while (getline(infile, strValue, ','), !infile.eof())
{
//cout << strValue;
if (strValue.at(0) >= 48 && strValue.at(0) <= 57)
{
vecInt.push_back(atoi(strValue.c_str()));
}
else
{
vecString.push_back(strValue);
}
}
infile.clear();
infile.close();
ofstream outfile;
outfile.open(pszFile);
if (!outfile)
{
cout << "Open File Error!" << endl;
return 0;
}
vector<string>::iterator siter;
vector<string>::iterator sbegin = vecString.begin();
vector<string>::iterator send = vecString.end();
for (siter = sbegin; siter != send; ++siter)
{
cout << (*siter) << "\n";
outfile << (*siter) << ",";
}
cout <<endl;
outfile.clear();
outfile.close();
int nSum = 0;
vector<int>::iterator iiter;
vector<int>::iterator ibegin = vecInt.begin();
vector<int>::iterator iend = vecInt.end();
for (iiter = ibegin; iiter != iend; ++iiter)
{
nSum += (*iiter);
}
cout << "nSum = " << nSum << endl;
return 0;
}