如题,是用C编写的,能读取一个文本文件显示到屏幕上..求各位编程帝,谢谢~!
#include<stdio.h>
#include<string.h>
void main()
{
FILE *fp; //创建一个文件指针*fp
char ch;
fp=fopen("D:/data.txt","r"); //以只读方式打开D:\data.txt文件
if(fp==NULL)
printf("can not open!\n"); //如果fp指针指向为空,即文件为空,则输出can not open
else{
//读取字符:fscanf(fp,"%c",&ch),ch=fgetc(fp);
fscanf(fp,"%c",&ch); //读取字符
while(!feof(fp)){ //feof()这个函数是用来判断指针是否已经到达文件尾部
putchar(ch); //输出
fscanf(fp,"%c",&ch); //再次读取字符
}
fclose(fp); //关闭文件
}
printf("\n");
}
#include<string.h>
void main()
{
FILE *fp; //创建一个文件指针*fp
char ch;
fp=fopen("D:/data.txt","r"); //以只读方式打开D:\data.txt文件
if(fp==NULL)
printf("can not open!\n"); //如果fp指针指向为空,即文件为空,则输出can not open
else{
//读取字符:fscanf(fp,"%c",&ch),ch=fgetc(fp);
fscanf(fp,"%c",&ch); //读取字符
while(!feof(fp)){ //feof()这个函数是用来判断指针是否已经到达文件尾部
putchar(ch); //输出
fscanf(fp,"%c",&ch); //再次读取字符
}
fclose(fp); //关闭文件
}
printf("\n");
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2018-02-27
#include ""void main()
{
FILE *fp;
if((fp=fopen("f1.txt","r"))==NULL)
{
printf("无法打开文件\n");
return ;
}
while(!feof(fp))
{
putchar(fgetc(fp));
}
printf("\n\n显示完毕\n");}本回答被网友采纳
{
FILE *fp;
if((fp=fopen("f1.txt","r"))==NULL)
{
printf("无法打开文件\n");
return ;
}
while(!feof(fp))
{
putchar(fgetc(fp));
}
printf("\n\n显示完毕\n");}本回答被网友采纳
第2个回答 2017-07-07
#include <iostream>
#include <cstdio>
using namespace std;
void print(char *p)
{
while(1)
{
if(*p!=0)
printf("%c",*p++);
else
break;
}
return;
}
int main()
{
string a;
a=freopen("xx.txt",r,stdin)//这是一个名字叫做“xx”的txt文档
print("a");
return 0;
}
//应该是这样吧……
#include <cstdio>
using namespace std;
void print(char *p)
{
while(1)
{
if(*p!=0)
printf("%c",*p++);
else
break;
}
return;
}
int main()
{
string a;
a=freopen("xx.txt",r,stdin)//这是一个名字叫做“xx”的txt文档
print("a");
return 0;
}
//应该是这样吧……
第3个回答 2017-07-31
假设文本文件data.txt的内容是
abcde
12345
#include<stdio.h>
void main() { FILE *fp; char buffer[256];
if ( fp=fopen("data.txt","r") ) {
while ( !feof(fp) ) {
fgets(buffer,255,fp); printf("%s\n",buffer);
}
fclose(fp);
} else printf("无法打开文本文件读取。\n");
}
abcde
12345
#include<stdio.h>
void main() { FILE *fp; char buffer[256];
if ( fp=fopen("data.txt","r") ) {
while ( !feof(fp) ) {
fgets(buffer,255,fp); printf("%s\n",buffer);
}
fclose(fp);
} else printf("无法打开文本文件读取。\n");
}
第4个回答 2017-08-02
int main(){
FILE* file=fopen("D:\\report_data2.txt","r");
if(file==NULL){
return 0;
}
char ch;
while((ch=fgetc(file))!=EOF){
printf("%c",ch);
}
fclose(file);
printf("\n");
}
FILE* file=fopen("D:\\report_data2.txt","r");
if(file==NULL){
return 0;
}
char ch;
while((ch=fgetc(file))!=EOF){
printf("%c",ch);
}
fclose(file);
printf("\n");
}