#include <stdio.h>
#include <string.h>
#define LBL 2
#define NUMROWS 24
#define NUMCOLS 80
int main ( void )
{
FILE *fp = fopen( "/home/me/Desktop/screen.in", "r" );
FILE *fo = fopen( "/home/me/Desktop/screen.out", "w+" );
char x;
int l;
int r;
char L[LBL][NUMCOLS];
l = 0;
while ( ( l < NUMROWS) && ( ( x = fgetc( fp ) ) != EOF ) )
{
r = 0;
while ( ( r < NUMCOLS ) && ( x != '\n' ) )
{
L[l][r] = x;
printf( "%c", x );
fputc( x , fo );
r++;
}
L[l][r] = x;
printf( "%c",'\n' );
fputc( x , fo );
l++;
}
fclose( fp );
fclose( fo );
}
System Etc Etc
Next line etc etc
identifier which will be replaced by a code fragment
FFER_SIZE as an abbreviation for the token
simple identifier which will be replaced by a
ill recognize and expand the macro BUFFER_SIZE. The
's body, expansion or replacement list. For example,
ck the features they intend to implemen
recognize and expand the macro BUFFER_SIZE.
macro's body, expansion or replacement list.
then the C preprocessor will recognize and expand the macro
macro's body, expansion or replacement list. For example,
d object-like because it looks like a data object in
nd of the ‘#define’ line. You may continue the definition ont
njnfk;db; B;kdbw kb fkBNWKdbnW;KNWKNBKWBN
基本上我想读入24行和80列的任何文本文件,并且1.0将每个字符放入一个字符数组中,并将相同的副本写入新文件,包括行尾,所有这些都在精确地呈现输入与电脑屏幕相同的方向。他编译代码但不会产生输出到屏幕或输出文件。我以为我的语法正确但不起作用。
答案 0 :(得分:2)
你基本上没有糟糕的算法实现。让我们重新思考:二维数组L
没有为你做任何事情,所以让我们抛弃它;让我们使用有意义的变量名;我们不需要打开输出文件“w +”,只需“w”就可以了;你的内部while
循环没有做正确的事情,因为它没有接受额外的输入,所以让我们用if
语句替换它,然后立即生成else
子句;你对输出换行的处理是不一致的;你没有像其他人注意到的那样对文件打开进行错误检查。
让我们重建你的代码来解决上面的问题并稍微调整一下风格:
#include <stdio.h>
#define NUMROWS 24
#define NUMCOLS 80
int main (int argc, char *argv[])
{
if (argc != 3)
{
fprintf(stderr, "appropriate error message");
return 1;
}
FILE *input = fopen(argv[1], "r" );
if (input == NULL)
{
fprintf(stderr, "appropriate error message");
return 1;
}
FILE *output = fopen(argv[2], "w" );
if (output == NULL)
{
fprintf(stderr, "appropriate error message");
return 1;
}
int c, row = 0, column = 0;
while (row < NUMROWS && (c = fgetc(input)) != EOF)
{
if (column < NUMCOLS && c != '\n')
{
fputc(c, stdout);
fputc(c, output);
}
else
{
fputc('\n', stdout);
fputc('\n', output);
column = 0;
row++;
}
}
fclose(output);
fclose(input);
return 0;
}
现在你可以做到:
> ./a.out screen.in screen.out
输入(screen.in
)的内容将输出到屏幕(标准输出)和文件screen.out
,但会截断为24行和80列。