新学标准C++中显示文本文件内容的问题

cidao1971 2008-10-22 06:32:16
请各位帮助,我新学 C++ 现在遇到了显示文本文件内容的问题,问题如下:
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
fstream out_file("d:\input.txt"); \\ 在 D 盘根目录下已经创建了该文件,并且有文本字符内容
…………………… \\ 请问我用什么语句能够把这个文本文件中的内容显示出来呢?
}

我把书中的类、对象、函数、指针、数组、字符串都看了,还是想不出如何显示出文本文件的内容,而书中的示例都是显示一个数组或结构的内容。
恳请帮助,谢谢各位。
...全文
101 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cidao1971 2008-10-22
  • 打赏
  • 举报
回复
谢谢各位,我原来使用的路径分隔符为/,现在知道了用 \\ 也是可以的。可我看了好几本关于 C++ 的学习书籍都没有这个介绍,不知你们是从什么地方看到的。请指点一下。
yujian793 2008-10-22
  • 打赏
  • 举报
回复
[Quote=引用楼主 cidao1971 的帖子:]
请各位帮助,我新学 C++ 现在遇到了显示文本文件内容的问题,问题如下:
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
fstream out_file("d:\input.txt"); \\ d:\\input.txt 应该用双斜杠吧
…………………… \\ 请问我用什么语句能够把这个文本文件中的内容显示出来呢?
}

我把书中的类、对象、函数、指针、数组…
[/Quote]

如上
帅得不敢出门 2008-10-22
  • 打赏
  • 举报
回复
查下MSDN就有了
发个读写的例子

// crt_fread.c
// This program opens a file named FREAD.OUT and
// writes 25 characters to the file. It then tries to open
// FREAD.OUT and read in 25 characters. If the attempt succeeds,
// the program displays the number of actual items read.

#include <stdio.h>

int main( void )
{
FILE *stream;
char list[30];
int i, numread, numwritten;

// Open file in text mode:
if( fopen_s( &stream, "fread.out", "w+t" ) == 0 )
{
for ( i = 0; i < 25; i++ )
list[i] = (char)('z' - i);
// Write 25 characters to stream
numwritten = fwrite( list, sizeof( char ), 25, stream );
printf( "Wrote %d items\n", numwritten );
fclose( stream );

}
else
printf( "Problem opening the file\n" );

if( fopen_s( &stream, "fread.out", "r+t" ) == 0 )
{
// Attempt to read in 25 characters
numread = fread( list, sizeof( char ), 25, stream );
printf( "Number of items read = %d\n", numread );
printf( "Contents of buffer = %.25s\n", list );
fclose( stream );
}
else
printf( "File could not be opened\n" );
}


Output

Wrote 25 items
Number of items read = 25
Contents of buffer = zyxwvutsrqponmlkjihgfedcb


ChamPagneZ 2008-10-22
  • 打赏
  • 举报
回复

ifstream fin("d:\\input.txt", ios::in|ios::binary);
while (!fin.eof()){
fin.read(...)
// to do
}

65,211

社区成员

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

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