社区
C++ 语言
帖子详情
c++里读取一个*.txt文件
chenchen
2004-05-13 11:09:28
c++里读取一个*.txt文件把它的内容显示在屏幕上怎么编啊
文件路径e:\*.txt
dos界面的
...全文
453
31
打赏
收藏
c++里读取一个*.txt文件
c++里读取一个*.txt文件把它的内容显示在屏幕上怎么编啊 文件路径e:\*.txt dos界面的
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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)
c++
中
txt
文件
的
读取
以及在MFC中
读取
txt
坐标数据并完成图形绘制
本文介绍了如何在
C++
中使用
文件
指针和
文件
流
读取
txt
文件
中的坐标数据,并在MFC环境中进行图形绘制。通过示例详细讲解了
文件
的打开、
读取
和关闭过程,以及在MFC中实现画点、画线和填充画面的步骤。
C++
读取
txt
文档到数组
本文介绍使用
C++
从
TXT
文件
读取
数据的方法,包括
读取
到double数组及结构体数组的过程。通过定义
文件
流对象并打开指定
文件
,利用循环逐个
读取
数值到内存数组中。
C++
读取
和写入
txt
文件
该博客介绍了
C++
中使用ifstream进行
TXT
文件
读取
的方法,包括默认和指定
文件
名的构造方式,以及open函数和getline函数的用法。通过示例代码详细解释了如何逐行
读取
文件
内容并打印。同时还提供了写入
TXT
文件
的简单示例。
c++
读取
整个
txt
文件
三种方式
本文介绍了
C++
中
读取
整个
txt
文件
的三种常见方法:按行
读取
、使用read函数以及利用rdbuf。详细阐述了每种方法的实现步骤和适用场景。
C++
遍历
读取
文件
夹下特定格式的
文件
(以
txt
为例)
本文介绍了如何使用
C++
遍历
读取
文件
夹下指定格式(如
txt
)的
文件
。通过引入相关库,利用函数获取
文件
句柄,遍历
文件
信息,检查
文件
属性,实现对每个匹配
文件
的处理。示例代码演示了遍历
读取
txt
文件
并修改其内容的过程。
C++ 语言
65,210
社区成员
250,515
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章