无法从文本文件中替换空格 [英] can't able to replace blanks from text file

查看:229
本文介绍了无法从文本文件中替换空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我试图通过单个空格删除连续的空白区域但是通过这条小线条得到错误 -

ch =''

这是我到目前为止所做的事情 -

Hi all
I am trying to remove consecutive blank space by single blank space but getting error by this small line-
ch=''
This is what I have done till now-

#include <iostream.h>
    #include <ctype.h>
    #include <conio.h>
    void main()
    {
        char ch;
        int count=0;
        ifstream in_stream;
        ofstream out_stream;
        clrscr();
        in_stream.open("A.txt");
        out_stream.open("B.txt");
        while (!in_stream.eof())
        {
            if(isspace(ch))
                   count++;
              
              if(count >= 2)
              {
                   ch='';
                   count = 0;
              }          
              else
              {
                  out_stream <<ch;
              }
              out_stream << ch;
        }
    }





错误是 - 字符常量必须是一个或两个字符长

根据我们的教学大纲要求,我正在学习Turbo C ++中的C ++。



提前谢谢你



Error is - Character constant must be one or two characters long
I am learning C++ in Turbo C++ as per our syllabus requirement.

Thank you in advance

推荐答案

请参阅我的评论;它解释了为什么你的代码错了,它的整个想法。因为这是C ++,为什么不使用 std :: string

请参阅:

http://www.cplusplus.com/reference/string/string/find/ [ ^ ],

http://www.cplusplus.com/reference/string/string/replace/ [ ^ ]。



你明白了吗?







请参阅我对该问题的第二条评论解释如何处理它。使用 std :: string.find 查找双空格,并将其替换为 std :: string.replace



-SA
Please see my comment; it explains why your code is wrong, the whole idea of it. As this is C++, why not using std::string?
Please see:
http://www.cplusplus.com/reference/string/string/find/[^],
http://www.cplusplus.com/reference/string/string/replace/[^].

Are you getting the idea?



Please see my second comment to the question explaining what to do with it. Find double blank space with std::string.find and replace it with std::string.replace.

—SA


好的,谢谢大家考虑我的问题。我从第15号问题 http://cbsecsnip.in 获得了解决方案

编写一个读取文本的程序文件并创建另一个相同的文件,期望每个连续的空格序列被一个空格替换。

这里是最终的代码



OK Thank you every one for considering my problem. I got the solution from http://cbsecsnip.in under question No. 15
Write a program that reads a text file and creates another file that is identical expect that every sequence of consecutive blank space is replaced by a single space.
here is the final code

#include <fstream.h>
#include <iostream.h>
#include <ctype.h>
#include <conio.h>
void main()
{
    char ch;
    int count=0;
    ifstream in_stream;
    ofstream out_stream;
    clrscr();
    in_stream.open("A.txt");
    out_stream.open("B.txt");
    while (!in_stream.eof())
    {
       ch = (char)in_stream.get( );
        if(isspace(ch))
               count++;
          
          if(count >= 2)
          {
               ch=' ';
               count = 0;
          }          
          else
          {
              out_stream <<ch;
          }
    }
}</conio.h></ctype.h></iostream.h></fstream.h>





工作完美



Working perfect


这篇关于无法从文本文件中替换空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆