| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1300 人关注过本帖
标题:c++读取文件中的字母并输出
只看楼主 加入收藏
璐璐嘻嘻
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2017-12-26
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:5 
c++读取文件中的字母并输出
图片附件: 游客没有浏览图片的权限,请 登录注册


可以按照这个文件中读取数字的方法  
写一个类似的 文件中读取字母的方法吗
例如:
图片附件: 游客没有浏览图片的权限,请 登录注册


我用isalpha运行不出来  是要改哪些地方求c++大神指教
搜索更多相关主题的帖子: c++ 读取 文件 字母 输出 
2018-01-05 15:37
璐璐嘻嘻
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2017-12-26
收藏
得分:0 
没人理我吗
2018-01-06 14:49
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
不看图,因为我想不通为什么有人要贴图。
难道是想戏弄一下愿意帮忙的人,让他们每个人照着图片浪费时间重敲一遍代码才可以进行调试?
难道是文字看起来太舒服,所以故意弄成图片让别人眼睛看起来累?
既然你这么恨我们,我们当然不敢招惹你啦
2018-01-06 21:32
璐璐嘻嘻
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2017-12-26
收藏
得分:0 
回复 3楼 rjsp
我错了 我没考虑到 抱歉
#include "stdafx.h"
#include<fstream>
#include<iostream>


#include<string>
#include<algorithm>
#include<cctype>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    std::ifstream istr("D:\old.txt");
   
    if(istr.is_open())
    {
       std::string tmp;
        while(getline(istr, tmp))
        {
            std::string::const_iterator p, q = tmp.begin(), end = tmp.end();
            
            while((p = find_if(q, end, isdigit)) != end)
            {//查看该行是否还有数字
                q = find_if_not(p, end, isdigit);//查找最后一个不是数字的位置
                std::cout<<stoul(tmp.substr(distance(tmp.cbegin(), p), distance(p, q)))<<" ";//将p~q的字串转化为数字
            }
            
        }
    }
   
    return 0;
}
2018-01-07 12:49
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
你的代码没法编译通过,此外,"D:\old.txt"肯定是错的,stoul也没考虑数字很大的情况

修改你的源代码为
程序代码:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <cctype>
using namespace std;

void foo( const std::string& filename, int isxxx(int c), std::ostream& out )
{
    std::ifstream istr( filename );
    if( istr )
    {
        for( std::string tmp; getline(istr,tmp); )
        {
            for( std::string::const_iterator p=tmp.cbegin(); p=find_if(p,tmp.cend(),isxxx), p!=tmp.cend(); )
            {
                std::string::const_iterator q = find_if_not( p, tmp.cend(), isxxx );
                std::copy( p, q, std::ostream_iterator<char>(out) ) = ' ';
                p = q;
            }
        }
    }
    out << '\n';
}

int main( void )
{
    foo( R"(D:\old.txt)", ::isdigit, cout );
    foo( R"(D:\old.txt)", ::isalpha, cout );
}

不明白为什么要以“行”为缓冲?
程序代码:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <cctype>

int main( void )
{
    std::ifstream fin( R"(D:\old.txt)");
    std::ofstream fo1( R"(D:\new1.txt)");
    std::ofstream fo2( R"(D:\new2.txt)");
    if( !fin || !fo1 || !fo2 )
        return 1;

    for( char ch, drt=0; fin.get(ch); )
    {
        if( ::isdigit(ch) )
        {
            if( drt != 1 )
                fo1.put( ' ' );
            drt = 1;

            fo1.put( ch );
        }
        else if( ::isalpha(ch) )
        {
            if( drt != 2 )
                fo2.put( ' ' );
            drt = 2;

            fo2.put( ch );
        }
        else
            drt = 0;
    }
}
2018-01-08 09:57
莫大大
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-1-8
收藏
得分:0 
看不懂  你们是用什么写的?什么软件?是控制台书写的吗?
2018-01-09 20:32
快速回复:c++读取文件中的字母并输出
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019602 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved