龟etc

问题描述:

#include< stdio.h>


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

{

if(argc!= 2)/ * argc应为2以便正确执行* /

{

/ *我们打印argv [0]假设它是程序姓名* /

printf(" usage:%s filename",argv [0]);

}

else

{

//我们假设argv [1]是要打开的文件名

FILE * file = fopen(argv [1]," r") ;


/ * fopen返回0,NULL指针,失败时* /

if(file == 0)

{

printf(无法打开文件\ n);

}

其他

{

unsigned char x;

/ *从文件中一次读取一个字符,在EOF停止,

其中

表示文件的结尾。请注意,

的成语为变量赋值

,检查值下面使用的是因为

赋值语句的计算结果为

赋值。 * /

while((x = fgetc(file))!= EOF)

{

printf("%c",x) );

}

}

fclose(文件);

}

这个程序不起作用。$ b / b
当我输入程序名称并遵循

文件名时,它会清除屏幕并显示任何内容。哪里错了?

我不确定函数fgetc 。

#include <stdio.h>

int main ( int argc, char *argv[] )
{
if ( argc != 2 ) /* argc should be 2 for correct execution */
{
/* We print argv[0] assuming it is the program name */
printf( "usage: %s filename", argv[0] );
}
else
{
// We assume argv[1] is a filename to open
FILE *file = fopen( argv[1], "r" );

/* fopen returns 0, the NULL pointer, on failure */
if ( file == 0 )
{
printf( "Could not open file\n" );
}
else
{
unsigned char x;
/* read one character at a time from file, stopping at EOF,
which
indicates the end of the file. Note that the idiom of
"assign
to a variable, check the value" used below works because
the assignment statement evaluates to the value
assigned. */
while ( ( x = fgetc( file ) ) != EOF )
{
printf( "%c", x );
}
}
fclose( file );
}
}
this program doesnt work. when i type the program name and following a
file name, it clear the screen and display nothing. where was wrong ?
and i am not sure about the function "fgetc" .



01写道:

01 wrote:
#include< stdio .H&GT;


< snip>

else
{
unsigned char x;
/ *一次读一个字符从文件中,停止在EOF,
表示文件的结尾。请注意,为变量赋值
,检查值的成语。下面使用的是因为赋值语句的计算结果为赋值。 * /
while((x = fgetc(file))!= EOF)
{
printf("%c",x);
}
}
fclose(文件);
}
}

这个程序不起作用。当我输入程序名称并按照
文件名时,它会清除屏幕并显示任何内容。哪里错了?
我不确定函数fgetc是什么? 。
#include <stdio.h>
<snip>
else
{
unsigned char x;
/* read one character at a time from file, stopping at EOF, which
indicates the end of the file. Note that the idiom of "assign
to a variable, check the value" used below works because
the assignment statement evaluates to the value assigned. */
while ( ( x = fgetc( file ) ) != EOF )
{
printf( "%c", x );
}
}
fclose( file );
}
}
this program doesnt work. when i type the program name and following a
file name, it clear the screen and display nothing. where was wrong ?
and i am not sure about the function "fgetc" .




函数`fgetc`返回一个`int`,你强迫它变成`unsigned

char`。由于EOF保证是负数,你的`while`将永远运行
。屏幕实际上并不是空白,而是填充了

空白。由`printf()`发出(实际上,无论EOF恰好是什么,一旦强制进入`unsigned char`,都是
)。你实际上是(非)幸运的,

因为没有终止''\ n'','printf()`可能不会产生任何输出。


将`x`更改为`int`,然后再试一次。



Function `fgetc` returns an `int`, and you''re forcing it into `unsigned
char`. As EOF is guaranteed to be negative, your `while` will run
forever. The screen does not actually go blank, but filled with
"blanks" issued by `printf()` (actually, whatever EOF happens to be
once forced into `unsigned char`). You''re actually (un)lucky it does,
as without terminating ''\n'', `printf()` may not produce any output.

Change `x` to `int`, and try again.


哦,谢谢你的帮助,它现在正常工作,thanx

Vladimir Oka写道:
oh, thank you for your help , it work now, thanx
Vladimir Oka wrote:

函数`fgetc`返回一个`int`,你强迫它进入`unsigned
char`。由于EOF保证是负数,你的`while`将永远运行
。屏幕实际上并不是空白,而是填充了空白空白。由`printf()`发出(实际上,无论EOF恰好是什么,一旦强制进入`unsigned char`)。你实际上(非)幸运,
因为没有终止''\ n'','printf()`可能不会产生任何输出。

改变`x`到`int`,然后再试一次。

Function `fgetc` returns an `int`, and you''re forcing it into `unsigned
char`. As EOF is guaranteed to be negative, your `while` will run
forever. The screen does not actually go blank, but filled with
"blanks" issued by `printf()` (actually, whatever EOF happens to be
once forced into `unsigned char`). You''re actually (un)lucky it does,
as without terminating ''\n'', `printf()` may not produce any output.

Change `x` to `int`, and try again.








01写道:

01 wrote:
哦,谢谢你的帮助,现在正常工作,感谢
Vladimir Oka写道:
oh, thank you for your help , it work now, thanx
Vladimir Oka wrote:

函数`fgetc`返回一个`int`,你''强迫它进入`unsigned
char`。由于EOF保证是负数,你的`while`将永远运行
。屏幕实际上并不是空白,而是填充了空白空白。由`printf()`发出(实际上,无论EOF恰好是什么,一旦强制进入`unsigned char`)。你实际上(非)幸运,
因为没有终止''\ n'','printf()`可能不会产生任何输出。

改变`x`到`int`,然后再试一次。

Function `fgetc` returns an `int`, and you''re forcing it into `unsigned
char`. As EOF is guaranteed to be negative, your `while` will run
forever. The screen does not actually go blank, but filled with
"blanks" issued by `printf()` (actually, whatever EOF happens to be
once forced into `unsigned char`). You''re actually (un)lucky it does,
as without terminating ''\n'', `printf()` may not produce any output.

Change `x` to `int`, and try again.



哦,对不起*,


oh, sorry to top-post,