将一个文本文件读入,打印在屏幕上!

babam 2004-04-08 08:28:55
我是菜鸟!
...全文
153 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ymbymb 2004-04-08
  • 打赏
  • 举报
回复
还是我这个简单
#include <iostream>
#include <fstream>
using namespace std;

ifstream ifile("test.txt");
char buf[256];
while(!ifile.eof())
{
ifile.getline(buf, 256);
cout << buf << endl;
}
古布 2004-04-08
  • 打赏
  • 举报
回复
string file_name;

cout << "please enter file name: ";
cin >> file_name;

ifstream infile( file_name.c_str(), ios::in );
assert( infile != 0 );

string textline;

while ( getline( infile, textline, '\n' ))
cout << textline;
happlyman 2004-04-08
  • 打赏
  • 举报
回复
string ifile;

cout << "Please enter file to sort: ";
cin >> ifile;

ifstream infile(ifile.c_str());

if (!infile)
{
cerr << "error: unable to open input file: " << ifile << endl;
return -1;
}
string buffer;
vector<string> text;
int cnt = 1;
while (infile >> buffer)
{
text.push_back(buffer);
cout << buffer << (cnt++%8 ? " " : "\n");
}
rorot 2004-04-08
  • 打赏
  • 举报
回复
#define MAXSIZE 1024*1024
char buffer[MAXSIZE];

够不?
babam 2004-04-08
  • 打赏
  • 举报
回复
你给出的buf是多大?
redcard 2004-04-08
  • 打赏
  • 举报
回复
偶,发错了哈,发成java的了,呵呵。
redcard 2004-04-08
  • 打赏
  • 举报
回复
/** * 加载给定文件名的内容,如果文件不可加载返回null。 */
static public String loadFile(String filename)
{
File file = new File( filename );
try { Reader rdr = new FileReader( file );
long sz = file.length(); // can only read in things of MAXINT length
char[] ch = new char[(int)sz];
rdr.read(ch); rdr.close();
return new String(ch);
} catch(IOException ioe) {
return null;
}
}

用这个把文件内容读出来,在用打到屏幕上。
xstring 2004-04-08
  • 打赏
  • 举报
回复

#include <stdio.h>

int main (int argc, char **argv)
{
FILE *fp;
char buf [0x1000];

if (argc < 2)
{
fprintf (stderr, "未指定文件!\n");
return -1;
};

fp = fopen (argv [1], "r");
if (fp == NULL)
{
fprintf (stderr, "不能打开文件 %s\n", argv [1]);
return -1;
};

while (!feof (fp))
{
fgets (buf, 0x1000, fp);
fputs (buf, stdout);
};
fclose (fp);
return 0;
};
zhouqingyuan 2004-04-08
  • 打赏
  • 举报
回复
c#下面的

using System;
using System.IO;
class Fileread
{
public static void Main()
{
FileStream fs=new FileStream("c:\\net\\1.txt",FileMode.Open,FileAccess.Read);
StreamReader sr=new StreamReader(fs);
string nextline;
while((nextline=sr.ReadLine())!=null)
{
Console.WriteLine(nextline);
}
}
}
happyasen 2004-04-08
  • 打赏
  • 举报
回复
最近写的一部分

do
{
puts("_______________________________________\n");
printf(" 请输入数据文件名称(例如:asen.csv):");
scanf("%s",&fname);
if((fp=fopen(fname,"r"))==NULL)
{
puts("________________________________________\n");
puts(" 文件不存在或无法打开!\n");
puts(" 按 \"Y\" 重新输入\n");
puts(" 按其它键退出程序\n");
puts("________________________________________\n");
getchar();
scanf("%c",&in);
if(in=='y'||in=='Y');
else
{
puts("\n 已退出程序,谢谢使用!\n");
exit (0);
}
}
else break;
}while(1);

puts("_______________________________________________\n");
puts(" 请输入已知样本数 n,待判样本数 n1,样本列数 m");
printf(" 注意:数据间以\",\"号隔开(例如:25,4,17)。");
scanf(" %d,%d,%d",&n,&n1,&m);
puts("_______________________________________________");

for(i=0;i<n1+n;i++)
for(j=0;j<m;j++)
fscanf(fp,"%f,",&y[i][j]);
fclose(fp);

puts("按 \"Y\" 查看数据,其它键继续程序。\n");
getchar();
scanf("%c",&in);
if(in=='y'||in=='Y')
for(i=0;i<n1+n;i++)
{
printf("\n %d ",i);
for(j=0;j<m;j++)
printf(" %.3f,",y[i][j]);
}
wythust 2004-04-08
  • 打赏
  • 举报
回复
用C 吗,如果是,下面是一个简单的:

#include <stdio.h>
FILE *fpin;
static void openInputFile(){
if((fpin=fopen("d:\\tc\\in.txt","r"))==NULL){
printf("open InputFile error!!");
exit(-1);
}
}

static void dispSourceFile(){
while(!feof(fpin))
putchar(getc(fpin));
fclose(fpin);
fflush(fpin);
}

void main(){
openInputFile();
dispSourceFile();
}

64,682

社区成员

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

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