用C语言编写“读取一个给定的文本文件,并将文件的内容显示在屏幕上”的一个程序,用vc++6.0运行,谢谢各

如题所述

/*楼上的题目会误人子弟的,这样肯定没法过关,下面的才可以!*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define szBUF 256

int main(int argc, char * argv[])
{
char filename[szBUF], buf[szBUF]; int r = 0; FILE * f = 0;
if(argc < 2) {
printf("please input the file name:");
gets(filename);
}else
strcpy(filename, argv[1]);
//end if
f = fopen(filename, "r");
if(!f) {
printf("file %s open fault!\n", filename);
return 0;
}//end if
while(!feof(f)) {
r = fread(buf, sizeof(char), szBUF, f);
if(r < 1) break;
fwrite(buf, sizeof(char), r, stdout);
}//end while
fclose(f);
system("pause");
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-09-06
#include <iostream>
using namespace std;

int main() {
freopen("text.txt","r",stdin);
char str[1024];
cin >> str;
cout << str;
system("pause");
return 0;
}

简单可行……
第2个回答  2010-09-06
#include <stdio.h>
#include <stdlib.h>

void main ( void )
{
char fileName[] = "file_to_be_read.txt";
char cmd[1024];
sprintf ( cmd, "type \"%s\"", fileName );
system ( cmd );
}
相似回答