怎么从文件中读取换行符

elegant87 2008-08-04 12:54:03
程序与下,有一个feng.txt的文件,里面有些字符,且有换行。
现在有从文件中读取字符,显示输出,如果读到换行符,就输出换行。
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
const char* fing ="feng.txt";
ifstream inFile(fing);
if(!inFile)
{
cout<<"找不到所需的文本文件";
return 1;
}
char ar;
while(!inFile.eof())
{
inFile>>ar;
if(ar=='\n')
cout<<endl;
else
cout<<ar;
}
return 0;
}
...全文
1421 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
npuhuxl 2008-08-04
  • 打赏
  • 举报
回复
inFile.get()
xkyx_cn 2008-08-04
  • 打赏
  • 举报
回复
晕倒,你直接输出\n不就换行了吗
nmglzlong 2008-08-04
  • 打赏
  • 举报
回复
mark
bluejing0 2008-08-04
  • 打赏
  • 举报
回复
这个跟OS有关系的。
win下是\r\n
linux是\n
不一样的
chenhjcs 2008-08-04
  • 打赏
  • 举报
回复
看看所获取的字符是否为\n,当然\n\t也行就看你怎么处理了
ykx_yeer 2008-08-04
  • 打赏
  • 举报
回复
一行结尾是 \n\r
wangdeqie 2008-08-04
  • 打赏
  • 举报
回复
对文件操作还是用C吧,像操作系统,老字号,值得信赖^_^
veloting 2008-08-04
  • 打赏
  • 举报
回复
呵呵,学习了!
wangdeqie 2008-08-04
  • 打赏
  • 举报
回复
我9楼贴的代码,可以读出换行!
比如一个.txt文件里:
345353


一行数字,两行空行(通过点击Enter产生的)
vc6下运行结果:
2
Press any key to continue
matrixdwy 2008-08-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xkyx_cn 的回复:]
晕倒,你直接输出\n不就换行了吗
[/Quote]
同感。。。。。


#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
const char* fing ="feng.txt";
ifstream inFile(fing);
if(!inFile)
{
cout<<"找不到所需的文本文件";
return 1;
}
istreambuf_iterator<char>isbf(inFile), end;
copy(isbf, end, ostream_iterator<char>(cout, ""));
/* 另外一个“方法”
char c;
while(isbf != end) {
c = *isbf++;
cout << c;
}
*/
return 0;
}
elegant87 2008-08-04
  • 打赏
  • 举报
回复
get
语法:
int get();
istream &get( char &ch );
istream &get( char *buffer, streamsize num );
istream &get( char *buffer, streamsize num, char delim );
istream &get( streambuf &buffer );
istream &get( streambuf &buffer, char delim );



get()函数被用于输入流,和以下这些:

读入一个字符并返回它的值,
读入一个字符并把它存储在ch,
读取字符到buffer直到num - 1个字符被读入, 或者碰到EOF或换行标志,
读取字符到buffer直到已读入num - 1 个字符,或者碰到EOF或delim(delim直到下一次不会被读取),
读取字符到buffer中,直到碰到换行或EOF,
或是读取字符到buffer中,直到碰到换行,EOF或delim。(相反, delim直到下一个get()不会被读取 ).
例如,下面的代码一个字符一个字符的显示文件temp.txt中的内容:

char ch;
ifstream fin( "temp.txt" );
while( fin.get(ch) )
cout << ch;
fin.close();
wangdeqie 2008-08-04
  • 打赏
  • 举报
回复

//这么写
#include <stdio.h>
#include <process.h>
#include <iostream.h>
#include <memory.h>
#include <string.h>
#include <iostream.h>
#include <fstream.h>

void main()
{
FILE *fp;
int count=0;
char *str="\n";
char buff[256];
fp=fopen("1.txt","r");
while(fgets(buff,256,fp)!=NULL)
{
if (strcmp(str,buff)==0)
{
count++;
}
}
cout<<count<<endl;
fclose(fp);

}
elegant87 2008-08-04
  • 打赏
  • 举报
回复
为什么inFile>>ar;就不行呢?

lily604 2008-08-04
  • 打赏
  • 举报
回复
inFile.get()可以

while(!inFile.eof())
{
inFile>>ar;
cout<<ar;
}
这样就行了
不必向你那样区分
elegant87 2008-08-04
  • 打赏
  • 举报
回复
是的!有何高见!
太乙 2008-08-04
  • 打赏
  • 举报
回复
lz什么意思??
没看明白~
elegant87 2008-08-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 npuhuxl 的回复:]
inFile.get()
[/Quote]
这是为什么呢?能解释一下吗?
veloting 2008-08-04
  • 打赏
  • 举报
回复
lz的意思是怎么能判断出txt文件中的换行符吧?

64,649

社区成员

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

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