输入输出流的一个题目,给出程序对的就有分

zjk2752 2009-05-02 01:56:11
题目是这样的:现在有一个txt文件,命名为test.txt.我在这里面输入了如下内容:a,23,b,c,4,56,d,123.其中,23,4,56,123是被看作数字的(当然存进去的是字符了)现在要求用C++的输入输出流将文件内容读入,然后删除里面所有的数字后,再将删除后的内容写入test.txt中,要求字母的数字顺序不变。也就是最后test.txt里面的内容是a,b,c,d.
希望大家给我一个完整的程序,刚学到这里,感觉对这些东西很模糊,给个程序我参考分析一下,从别处抄到的也行,一定给高分的,分数不够我追加,谢谢。
...全文
167 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
nwzai 2009-05-03
  • 打赏
  • 举报
回复
int a=0;
lori227 2009-05-02
  • 打赏
  • 举报
回复
建立win32 工程 自动就有那个头文件了~~
zjk2752 2009-05-02
  • 打赏
  • 举报
回复
楼上的,在输入框上面不是有一排快捷键嘛,字体格式控制的,还有插入图片的,那个还#号的,在A的右边就是插入代码的。你把代码放在两个界符之间就行了。
光宇广贞 2009-05-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 baiwei156 的回复:]
C/C++ code

#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)) //如果是数字

[/Quote]

像这么漂亮的代码是怎么贴在这个论坛里面的……
zjk2752 2009-05-02
  • 打赏
  • 举报
回复
5楼的大哥你的程序不完整呀,第一个自己定义的头文件你没弄过来呀
zjk2752 2009-05-02
  • 打赏
  • 举报
回复
谢谢楼上各位,我先来看下你们的程序
baiwei156 2009-05-02
  • 打赏
  • 举报
回复

#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;
}
lori227 2009-05-02
  • 打赏
  • 举报
回复

#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;
}
liao05050075 2009-05-02
  • 打赏
  • 举报
回复
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
ifstream in("test.txt");
string out_string="",tmp="";
int sum=0;
char ch;
while(in.get(ch))
{

if(ch==',')
{
if(tmp.size())
{
if(isdigit(tmp[0]))
sum+=atoi(tmp.c_str());
else out_string+=","+tmp;
}
tmp="";
}
else tmp+=ch;
}
if(tmp.size())
{
if(isdigit(tmp[0]))
sum+=atoi(tmp.c_str());
else out_string+=","+tmp;
}
in.close();
cout<<"The sum is "<<sum<<endl;
out_string.erase(0,1);
ofstream out("test.txt");
out<<out_string;
out.close();
return 0;
}
Sou2012 2009-05-02
  • 打赏
  • 举报
回复
LZ先自己尝试写一个嘛,或者说说你的思路
沙漠里的海豚 2009-05-02
  • 打赏
  • 举报
回复
呵呵 等晚上有时间给你做一下吧
zjk2752 2009-05-02
  • 打赏
  • 举报
回复
再加一点,最后计算里面所有数字的和,然后输出。谢谢。要不然,你直接从左向右扫描遇到数字就舍弃,就不是我的本意了。谢谢。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧