这是我从教授那里得到的代码,但当它到达 while 循环时,我并不完全理解它。有人可以向我解释以下内容:
while(fscanf(pin, "%[^\n]", line) != EOF)
fscanf(pin, "%1[\n]", dummy);
line[0] = '\0';
顺便说一句,要复制的文本文件包含:
1号线
(空间)
2号线
(空间)
(空间)
3号线
(空间)
(空间)
(空间)
4号线
#include <stdio.h>
#define LNAME 31
#define LLINE 81
int main(void)
{
FILE *pin, *pout;
char name_in[LNAME], name_out[LNAME], line[LLINE];
char dummy[LLINE];
do
{
printf("Enter name of input file : ");
scanf("%30s", name_in);
pin = fopen(name_in, "r");
} while(pin == NULL);
do
{
printf("Enter name of output file: ");
scanf("%30s", name_out);
pout = fopen(name_out, "w");
} while(pout == NULL);
while(fscanf(pin, "%[^\n]", line) != EOF)
{
fprintf(pout, "%s\n", line); /*In output file */
printf("%s\n", line); /*Echo on screen */
fscanf(pin, "%1[\n]", dummy); /*skip over '\n' */
line[0] = '\0'; /*important, when next line empty!!! */
}
fclose(pin);
fclose(pout);
printf("\n\n");
return 0;
}