首页 新闻 会员 周边

文本的里边有换行,大写变小写的时候换行就一起给变了(0-0)!

0
悬赏园豆:20 [已解决问题] 解决于 2016-05-04 22:02
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class chih
{

};
int main()
{
    fstream in("chih.txt", ios::in);
    if (!in)
    {
        cout << "fail to open." << endl;
        exit(1);
    }
    fstream out("chih2.txt", ios::out|ios::app);
    if (!out)
    {
        cout << "fail to open." << endl;
        exit(1);
    }
    char ah;
    while (!in.eof())
    {
        if (!'\r\n')//我想遇到个换行符就跳过去,这样貌似不行-0-??
        {

            in.get(ah);
            out.put(ah - 32);
        }
        else
        {
            in.get(ah);
            out.put(ah);
        }
    }
    in.close();
    out.close();
    return 0;
爱是用心码不要说话的主页 爱是用心码不要说话 | 初学一级 | 园豆:154
提问于:2016-05-03 13:17
< >
分享
最佳答案
0

 

'\r','\n'这是是两个符号,不是一个符号,windows系统下,将这两个符号放在一起作为换行符。

可以使用#include <cctype>下的isalpha()来判断是否为字母。

直接减32也不对,可以使用isupper(ch) 和 islower(ch)来判断字母是大写还是小写

 

收获园豆:20
灵枢 | 菜鸟二级 |园豆:297 | 2016-05-03 13:41

谢谢回答,不过我回去试了那个isapha()老是显示错误,原来是isalpha()-o-,总之还是要谢谢你。

爱是用心码不要说话 | 园豆:154 (初学一级) | 2016-05-04 08:41
#include<iostream>
#include<fstream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
    fstream in("chih.txt", ios::in);
    if (!in)
    {
        cout << "fail to open." << endl;
        exit(1);
    }
    fstream out("chih2.txt", ios::out|ios::app);
    if (!out)
    {
        cout << "fail to open." << endl;
        exit(1);
    }
    char ch[200];
    in.getline(ch, 200, '\0');
    cout << ch << endl;//
    char *a;
    a = ch;
    while (!in.eof())
    {
        if (islower(*a))
        {
            toupper(*a);
            out.put(*a);
            a++;
        }
        else
        {
            out.put(*a);
            a++;
        }
    }

    in.close();
    out.close();
    return 0;
}

现在代码被改成了这样,怎么写入不了文件了?是不是形参那里出了问题?

爱是用心码不要说话 | 园豆:154 (初学一级) | 2016-05-04 16:50

@爱是用心码不要说话: while (!in.eof())这里面,in的文件内指针并没有增加,如果while之前in的文件内指针没有到eof的话,就会变成死循环,如果已经到了eof的话,就会直接跳过

灵枢 | 园豆:297 (菜鸟二级) | 2016-05-04 17:29

@MissingAnObject: 厉害厉害,非常感谢

爱是用心码不要说话 | 园豆:154 (初学一级) | 2016-05-04 20:19

@爱是用心码不要说话: 最后总结:<cctype>,博主无幻总结的很好。数组结尾符号是‘\0’或者NULL,不过话说我用while(!NULL)会出现debug assertion failed 的错误唉-o-\

#include<iostream>
#include<fstream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
    fstream in("chih.txt", ios::in);
    if (!in)
    {
        cout << "fail to open." << endl;
        exit(1);
    }
    fstream out("chih2.txt", ios::out|ios::app);
    if (!out)
    {
        cout << "fail to open." << endl;
        exit(1);
    }
    char ch[200];
    in.getline(ch, 200, '\0');
    cout << ch << endl;
    char *a;
    a = ch;
    for (; *a != NULL; a++)
    {
        if (!isspace(*a))
        {
            out.put(toupper(*a));
        }
        else
        {
            out.put(*a);
        }
    }
    in.close();
    out.close();
    return 0;
}
爱是用心码不要说话 | 园豆:154 (初学一级) | 2016-05-04 21:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册