(C++)如何判断读入的是不是空格或换行?

来源:百度知道 编辑:UC知道 时间:2024/05/26 09:22:34
(C++)如何判断读入文本里面的数据时哪个是不是空格或换行?

我无数次实验都失败了,结果都是判断不出的,
回 ccchu0: 这个早试过了,失败的

确实是不行啊,例如读入

Friendship is a quiet walk in the park with the one you trust love is when you feel like

you are the two around Friendship is when they gaze into you eyes and you know they care

love is when they gaze into your eyes and it warms your heart

Friendship is being close even when you are apart love is when you can still feel their

hand on your heart when they are not near Friendship is hoping that they experience the

very best love is when you bring them the very best Friendship occupies your mind love

occupies your soul Friendship is knowing that you will alwarys try to be there when in

need love is a warming touch that sends a pulse through your heart Friendship can

survive without love love cannot live without friendship

根本无法判断哪个是换行哪个是空格,注:希望的是在算法里入手,不用cin_ge

你是怎么读入数据的嘛,不知道这个行不行if(c==32){}

我发现上面的答案可行。你把你的代码贴出来看哈嘛,有可能是其他地方出了问题。比如文件读入的时候。
#include<iostream>
#include<fstream>
using namespace std;

void main()
{
char ch;
ifstream in("fname.txt",ios_base::in);
if(!in)exit(0);

int i=500;
while(i--){
ch=in.get();
if(ch==' ')
cout.put(' ');
else if(ch==EOF)
cout<<endl;
else cout.put(ch);
}

}
文本文件要和C++源文件在同一个文件夹中,且文件名必须是"fname.txt"。缺一不可。

不用函数辅助?那你用什么?
用>>?这个一样是函数好吧,重载的运算符一样是函数。

而且!!!
>>这个运算符是吃空格并且吃换行符的,有多说它吃多少。
你不会是用了>>导致没有空格以及换行吧?

//以下是随便写的一个函数,
//你在源文件同目录下建一个test.txt就可以读里面的东西。

#include <fstream>
#include <iostream>

using namespace std;

void main()
{
char c;
ifstream fin;
fin.open("test.txt");
while(!f