社区
C++ 语言
帖子详情
将一个文本文件读入,打印在屏幕上!
babam
2004-04-08 08:28:55
我是菜鸟!
...全文
162
11
打赏
收藏
将一个文本文件读入,打印在屏幕上!
我是菜鸟!
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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();
}
python 试题:写一段程序逐行
读入
一个
文本文件
,并在
屏幕
上
打印
出来。
写一段程序逐行
读入
一个
文本文件
,并在
屏幕
上
打印
出来。 参考答案: f = open(filename) while True: line = f.readline() if not line: break print(line) f.close()
c 语言 readline,如何用C实现
一个
readLine()函数- 该函数每次
读入
文本文件
的一行并
打印
到
屏幕
...
如何用C实现
一个
readLine()函数-该函数每次
读入
文本文件
的一行并
打印
到
屏幕
(2012-04-13 00:23:41)标签:
打印
文本文件
如何杂谈如何用C实现
一个
readLine()函数? 该函数每次
读入
文本文件
的一行并
打印
到
屏幕
以前看到过这方面的讨论http://bbs.chinaunix.net/viewthread.php?tid=248256但是总感觉没有
一个
好的总结方案,就没了下文...
200826-C语言
打印
文件中的文本内容
1. Description 在桌面上创建
一个
txt文件,输入一些文本内容,我们的任务是把文本内容
打印
出来。 在编程之前,关于一些函数的定义我们需要了解下。 fopen fopen的函数原型为: FILE *fopen(const char *filename, const char *mode);其功能是使用给定的模式 mode 打开 filename 所指向的文件。文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回 NULL,并把错误代码存在 error 中。该函数位于C 标准库&
文本文件
读入
学生信息 排序后输出c语言,IO流的练习4 —— 键盘录入学生成绩信息,进行排序后存入文本中...
需求:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入
文本文件
分析:A:创建学生类B:创建集合对象TreeSetC:键盘录入学生信息存储到集合D:遍历集合,把数据写到
文本文件
首先创建个学生类package zl_Test;/*** 这是个记录学生成绩类* @author LZL**/public class Student {private String name;...
《编写
一个
程序,从
一个
文件中读出字符串,并显示在
屏幕
上》
注意:在程序的第11行用fgets函数
读入
字符串时,指定一次
读入
10个字符,但按fgets函数的规定, 如果遇到“\n”就结束字符串输入,“\n”作为最后
一个
字符也
读入
到字符数组中 //编写
一个
程序,从f:\\FILE_1\\file_2.txt中读回字符串 #include<stdio.h>#include<string.h>#include<s...
C++ 语言
65,187
社区成员
250,526
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章