打印出一个文件? [英] print out a file?

查看:84
本文介绍了打印出一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚搜索谷歌,但还没有拿出任何东西。只是

想知道,最好的打印方式是将file.txt或其他格式说成

屏幕吗?一个链接会很棒而且读起来很糟糕。干杯

Just been searching google, but havent come up with anything. Just
wondering, whats the best way to print say a file.txt or other format to the
screen? A link would be great and ill read up on it. Cheers

推荐答案

提倡< ...... @ ...... com>这样说:
Advocated <......@......com> spoke thus:
刚刚搜索谷歌,但还没有拿出任何东西。只是想知道,最好的打印方式是将file.txt或其他格式说成
Just been searching google, but havent come up with anything. Just
wondering, whats the best way to print say a file.txt or other format to the




您可以使用以下C程序...


#include< stdio.h>

#include< stdlib.h>


int main( int argc,char * argv [])

{

char buf [256];

FILE * fp;


if(argc!= 2){

fprintf(stderr," Usage:%s< filename> \ n",argv [0]);

返回EXIT_FAILURE;

}

if((fp = fopen(argv [1]," r"))== NULL){

fprintf(stderr,无法打开文件\"%s \" \ n,argv [1]);

返回EXIT_FAILURE;

}

while(fgets(buf,sizeof buf,fp)){

printf("%s",buf);

}

printf(" \ n"); / *如果文件中没有换行符* /

fclose(fp);

返回EXIT_SUCCESS;

}


(常客的赞赏评论赞赏)


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



You could use the following C program...

#include <stdio.h>
#include <stdlib.h>

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

if( argc != 2 ) {
fprintf( stderr, "Usage: %s <filename>\n", argv[0] );
return EXIT_FAILURE;
}
if( (fp=fopen(argv[1],"r")) == NULL ) {
fprintf( stderr, "Could not open file \"%s\"\n", argv[1] );
return EXIT_FAILURE;
}
while( fgets(buf,sizeof buf,fp) ) {
printf( "%s", buf );
}
printf( "\n" ); /* in case there were no newlines in the file */
fclose( fp );
return EXIT_SUCCESS;
}

(salient comments from the regulars appreciated)

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.




2003年12月24日星期三,Christopher Benson-Manica写道:

On Wed, 24 Dec 2003, Christopher Benson-Manica wrote:

倡导< ...... @ ...... com>这样说:

Advocated <......@......com> spoke thus:

刚刚搜索谷歌,但还没有拿出任何东西。只是想知道,最好的打印方式是说文件.txt或其他格式
到[屏幕?]
您可以使用以下C程序......

Just been searching google, but havent come up with anything. Just
wondering, whats the best way to print say a file.txt or other format
to the [screen?]
You could use the following C program...



[使用fgets()剪断24行有点不合适]
(常客赞赏的显着评论)


[snip 24 lines using fgets() somewhat inappropriately]
(salient comments from the regulars appreciated)




如果你得到了什么file.txt中的一行超过255个字符?

无论如何,简单的解决方案是最好的...使用''cat file.txt'',

''输入file.txt'',或者,如果你的操作系统没有其中任何一个,那么只需编写你自己的''猫'克隆... [未测试]


/ *一个简单的''cat''实用程序* /


#include< stdio.h>


void进程(FILE * fp )

{

if(fp == NULL)return;

while((c = getc(fp))!= EOF)< br $> b $ b putchar(c);

putchar(''\ n'');

}


int main(int argc,char ** argv)

{

if(argc< 2){

process(stdin) ;

}

else {

int i;

for(i = 1; i< argc; ++ i)

过程(fopen(argv [i]));

}

返回0;

}


HTH,

-Arthur



What if you get a line in "file.txt" longer than 255 characters?
Anyway, the simple solutions are best... Use ''cat file.txt'',
''type file.txt'', or, if your OS doesn''t have either of those,
just write your own ''cat'' clone... [UNTESTED]

/* A simple ''cat'' utility */

#include <stdio.h>

void process(FILE *fp)
{
if (fp == NULL) return;
while ((c=getc(fp)) != EOF)
putchar(c);
putchar(''\n'');
}

int main(int argc, char **argv)
{
if (argc < 2) {
process(stdin);
}
else {
int i;
for (i=1; i < argc; ++i)
process(fopen(argv[i]));
}
return 0;
}

HTH,
-Arthur


Arthur J. O''Dwyer写道:
Arthur J. O''Dwyer wrote:

如果你在file.txt中输入一行怎么办?超过255个字符?
无论如何,简单的解决方案是最好的...使用''cat file.txt'',
''输入file.txt'',或者,如果你的操作系统没有'''有其中任何一个,
只写自己的''猫'克隆... [未测试]

/ *一个简单的''猫''实用工具* /

#include< stdio.h>

void进程(FILE * fp)
{
if(fp == NULL)return;
while ((c = getc(fp))!= EOF)
putchar(c);
putchar(''\ n'');
}

int main(int argc,char ** argv)
{
if(argc< 2){
process(stdin);
}
else {
int i;
for(i = 1; i< argc; ++ i)
process(fopen(argv [i]));
}
返回0 ;
}

What if you get a line in "file.txt" longer than 255 characters?
Anyway, the simple solutions are best... Use ''cat file.txt'',
''type file.txt'', or, if your OS doesn''t have either of those,
just write your own ''cat'' clone... [UNTESTED]

/* A simple ''cat'' utility */

#include <stdio.h>

void process(FILE *fp)
{
if (fp == NULL) return;
while ((c=getc(fp)) != EOF)
putchar(c);
putchar(''\n'');
}

int main(int argc, char **argv)
{
if (argc < 2) {
process(stdin);
}
else {
int i;
for (i=1; i < argc; ++i)
process(fopen(argv[i]));
}
return 0;
}




你忘了fclose()文件了。看起来这样做需要

除了添加电话之外的一些变化。


-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用最近发布的地址。



You forgot to fclose() the files. It looks like doing so will require
some changes beyond just adding the call.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


这篇关于打印出一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆