社区
C++ 语言
帖子详情
c++里读取一个*.txt文件
chenchen
2004-05-13 11:09:28
c++里读取一个*.txt文件把它的内容显示在屏幕上怎么编啊
文件路径e:\*.txt
dos界面的
...全文
417
31
打赏
收藏
c++里读取一个*.txt文件
c++里读取一个*.txt文件把它的内容显示在屏幕上怎么编啊 文件路径e:\*.txt dos界面的
复制链接
扫一扫
分享
转发到动态
举报
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)
C++
从.
txt
文件
中
读取
数据
在编程中避免重复性输入工作,从
文件
读写数据,这样的操作是很有必要的,认真读啊,我试错了很久的呜呜没有人教。 第一步:打开
文件
第二步:看看打开成功没(因为有可能这个
文件
打开方式不对,或者你根本没这个
文件
而且no-create了) 第三步:
读取
的知识点 第四步:关闭 完整代码 #include<iostream> #include<fstream> using namespace std; int main(){ ifstream infile("input.
txt
",ios::in
C++
读取
.
txt
文本
文件
操作
用
C++
读取
.
txt
文本
文件
步骤如下:定义
一个
文件
流对象、打开
文件
、读
文件
、关闭
文件
。本文对各个步骤作了简要介绍。
C++
实现从.
txt
文件
中
读取
数据存入数组,将数组数据写入.
txt
文件
声明: 编译器:vs2017 所有用到的.
txt
文件
都是提前放在当前工程目录下的。 1.从.
txt
文件
中
读取
数据,并存入数组 #include <iostream> #include <fstream> #include<vector> using namespace std; int main() { //
读取
数据
文件
ifstream in("datadata.
txt
", ios::in); if (!in.is_open()) { cout
Windows系统 Prefetch目录 *.pf
文件
解析 -- 探索程序运行记录、保护隐私
一、前言 1、本文说明 如果喜欢打开XX软件看XX视频等,看了多少次,什么时候看的,都会被记录下来,用软件来查看一下,全部的隐私都没有了。 家
里
的小孩玩的啥游戏,玩了多少次,也都可以看得一清二楚。 查看*.pf格式的软件有:WinPrefetchView、LastActivityView等。 本文带你深入了解pf格式
文件
所隐藏的内容,并用C/
C++
编写了个小软件,实现了隐私
读取
和分析。...
C++
读取
.
txt
文件
时中文出现乱码问题的解决方案
错误原因: .
txt
文件
在保存的时候默认是使用UTF-8编码方式,而这种编码方式虽然可以显示中文,但不能显示所有的汉字繁简字体,所以有时候会出现乱码问题。 解决方法: Step1:打开原来的.
txt
文件
,选择“
文件
”——>“另存为”: Step2:在出现的另存为界面上,看到底部的编码方式的选择默认是UTF-8,这
里
我们修改为ANSI,点击保存: Step3:再次运行
C++
程序,发现中文可以正常显示啦~ ...
C++ 语言
65,187
社区成员
250,526
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章