c++里读取一个*.txt文件

chenchen 2004-05-13 11:09:28
c++里读取一个*.txt文件把它的内容显示在屏幕上怎么编啊
文件路径e:\*.txt
dos界面的
...全文
417 31 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dublue 2004-06-17
  • 打赏
  • 举报
回复
qiqi162002(qiqi)说得很清楚,强烈支持~~
lei601 2004-06-17
  • 打赏
  • 举报
回复
# include <iostream>
# include <fsrtream>
using namespace std;
void main()
{
char ch;
ofstream f("e:\\*.txt");
while (f.get(ch))
cout<<ch;
return 0;
}
alever513 2004-06-16
  • 打赏
  • 举报
回复
我觉得还是重定向好些:

#include <iostream>
#include <fstream>
using namespace std;

fstream fin("*.txt");
istream& in = fin;

#define cin in

int main()
{
//
...//这里的输入输出操做跟控制台一样
//
return 0;
}
qiqi162002 2004-06-16
  • 打赏
  • 举报
回复
下面的代码可以原样在屏幕上输出文本的内容,空格和回车都有.
#include <fstream.h>
#include <stdlib.h>
#include<string>
void main()
{


ifstream fin("main.cpp");
if(!fin)
{
cerr<<"file con not open!"<<endl;
exit(1);
}
string str;
getline(fin,str); //在fin中取一行到str中
str+="\n"; //在str后加入换行符
cout<<str;
while(!fin.eof())
{ getline(fin,str);
str+="\n";
cout<<str;
}
fin.close();
system("PAUSE");
}
qiqi162002 2004-06-16
  • 打赏
  • 举报
回复
上面基本上都讲出了实现方法,但有的程序有点小错误.例如:只#INCLUDE<fstream>没有了<iostream>.

还有如果直接用{fin>>str;cout<<str;}这样的方法,空格和回车不会显示了.
因为>>符号一遇到空格就停止插入. str里只存入单词.

用getline(str,fin)是比较理想的方法.这时str中是"aa.txt"中的一行.用READ()就有点小题大作了.它比较适合 结构 这样的数据读入.

所以:cngdzhang()的那种方法不符要求,面且他只输出一个单词^_^.
liu_feng_fly 2004-06-16
  • 打赏
  • 举报
回复
// PrintTxtToConsole.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
copy(istream_iterator<string>(ifstream("aaa.txt")),istream_iterator<string>(),ostream_iterator<string>(cout,"\n"));//方法1
cout << ifstream("aaa.txt").rdbuf();//方法2
return 0;
}

peter9606 2004-06-16
  • 打赏
  • 举报
回复
怎么 没有人回答?
peter9606 2004-06-16
  • 打赏
  • 举报
回复
不知道重定向再这里有什么意义
麻烦给小弟解释一下
谢谢啦
peter9606 2004-06-16
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <io.h>
int main(void)
{
char c;
FILE *file1, *file2;
file1 = fopen(__FILE__, "r");
if (file1)
{
file2 = fopen("bak.dat", "w");
while(!feof(file1))
{
fread(&c, 1, 1, file1);
fwrite(&c, 1, 1, file2);
}
fclose(file2);
fclose(file1);
}

file2 = fopen("bak.dat", "r");
if (file2)
{
while(!feof(file2))
{
fread(&c, 1, 1, file2);
printf("%c", c);
}
fclose(file2);
}
}

这程序把自己输出
点燃你的火花 2004-06-15
  • 打赏
  • 举报
回复
路过。
学习!
tatbaby 2004-06-15
  • 打赏
  • 举报
回复
cngdzhang() 的已经够了
vcchunhong 2004-06-14
  • 打赏
  • 举报
回复
#include <fstream.h>
#include <stdlib.h>
void main()
{
ofstream fout("d:\\nihao.txt");
if(!fout)
{
cerr<<"file con not open!"<<endl;
exit(1);
}
for(int i=0;i<50;i++)
fout<<i<<" ";
fout.close();

ifstream fin("d:\\nihao.txt");
if(!fin)
{
cerr<<"file con not open!"<<endl;
exit(1);
}
int j;
while(fin>>j)
{
cout<<j<<" ";
}
fin.close();
cout<<endl;
}
追忆青春 2004-06-14
  • 打赏
  • 举报
回复
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
using namespace std;

int main(int argc, char* argv[])
{
ifstream in("e:\\a.txt");//文件名
if( !in )
{
cerr<<" !! unable to open files.\n";
return -1;
}

istream_iterator< string > is(in);
istream_iterator< string >eof;

vector< string >text;
copy( is, eof, back_inserter( text ) );

ostream_iterator< string >os( cout, " " );
copy( text.begin(), text.end(), os );

return 0;
}
talkingmute 2004-06-14
  • 打赏
  • 举报
回复
学习!
a00 2004-06-13
  • 打赏
  • 举报
回复
呵呵,
file.read((char*)&temp,sizeof(char));
可以写为
file.read(&temp,sizeof(char));
因为temp是char型,&temp就是char*了,没有必要再次强制转换。

或也许是hardsonxu(阿山)编译器需要这样做?!
webnumen 2004-06-13
  • 打赏
  • 举报
回复
file.read((char*)&temp,sizeof(char));这句话
请高手具体意思解释一下
cnBoysbe 2004-06-13
  • 打赏
  • 举报
回复
file.read((char*)&temp,sizeof(char));这句话
请高手具体意思解释一下
martmy 2004-06-13
  • 打赏
  • 举报
回复
mark
hardsonxu 2004-06-07
  • 打赏
  • 举报
回复
#include<fstream.h>

void main()
{
ifstream file("e:/aa.TXT");
char temp;

while(file)
{
file.read((char*)&temp,sizeof(char));
cout<<temp;
}

file.close();

}

这样写可以保持原有的输出格式
kaphoon 2004-06-07
  • 打赏
  • 举报
回复
最简单版
std::ifstream in( "readme.txt");
std::cout << in.rdbuf();
//用缓冲
加载更多回复(11)

65,187

社区成员

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

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