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

2024-12-21 22:37:32
推荐回答(3个)
回答1:

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

#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;
}

回答2:

#include
using namespace std;

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

简单可行……

回答3:

#include
#include

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