打印文本文件内容的函数
function to print the contents of a text file
这是我写的代码:
#include <stdio.h>
#include <stdlib.h>
void Print_File(FILE *f)
{
fopen("f", "r");
char s = fgetc(f);
while (s != EOF)
{
printf("%c", s);
s = fgetc(f);
}
fclose(f);
}
int main(void)
{
FILE *ptr = fopen("info.txt", "a");
if(ptr == NULL)
{
printf("Invalid Input\n");
return 1;
}
char *c = malloc(sizeof(char) * 101);
printf("One entry cannot be more than 100 characters long!\n");
printf("Enter your text here - ");
scanf("%[^\n]%*c", c);
fprintf(ptr, "%s\n", c);
fclose(ptr);
Print_File(ptr);
free(c);
}
在命令行执行程序后,当我手动打开文件时,它更新好了!
但是文件没有打印出来!我是不是把 Print_File() 函数写错了?
看看the manual page for fopen()
。它以文件名作为第一个参数,它 returns 一个 FILE *
。你这样做是错误的。
你应该改变这个:
fopen("f", "r");
为此:
FILE *f;
f = fopen("file-name-here", "r");
if (f == NULL) {
puts("Error opening file!");
exit(1);
}
其次,将关闭的文件ptr
传递给函数是没有用的。在调用函数之前 打开它(并且此时不要在其中使用 fopen()
)或者只是将函数声明为不带参数并在内部打开它。
选项 1:
void Print_File(FILE *f)
{
// ... use already opened file ...
}
// Then in main:
ptr = fopen("file-name-here", "r");
if (ptr == NULL) {
puts("Error opening file!");
exit(1);
}
Print_File(ptr);
选项 2:
void Print_File(void) // void == takes no arguments
{
FILE *f;
f = fopen("file-name-here", "r");
// ...
}
// Then in main:
Print_File();
最后,fgetc()
returns 一个 int
。您需要使用 int
变量来保存结果,否则您将无法区分有效字符和 EOF
:
int s = fgetc(f);
while (s != EOF)
{
// ...
完整的工作示例:
#include <stdio.h>
#include <stdlib.h>
void Print_File(FILE *f)
{
int s = fgetc(f);
while (s != EOF)
{
printf("%c", s);
s = fgetc(f);
}
fclose(f);
}
int main(void)
{
FILE *ptr = fopen("info.txt", "a");
if(ptr == NULL) {
printf("Error opening file for writing.\n");
return 1;
}
char *c = malloc(sizeof(char) * 101);
if (c == NULL) {
printf("Error allocating memory.\n");
return 1;
}
printf("One entry cannot be more than 100 characters long!\n");
printf("Enter your text here - ");
scanf("%[^\n]%*c", c);
fprintf(ptr, "%s\n", c);
fclose(ptr);
ptr = fopen("info.txt", "r");
if(ptr == NULL) {
printf("Error opening file for reading.\n");
return 1;
}
Print_File(ptr);
free(c);
return 0;
}
输出:
$ gcc prog.c -o prog
$ ./prog
One entry cannot be more than 100 characters long!
Enter your text here - HELLO WORLD
HELLO WORLD
$ ./prog
One entry cannot be more than 100 characters long!
Enter your text here - HELLO WORLD AGAIN
HELLO WORLD
HELLO WORLD AGAIN
这是我写的代码:
#include <stdio.h>
#include <stdlib.h>
void Print_File(FILE *f)
{
fopen("f", "r");
char s = fgetc(f);
while (s != EOF)
{
printf("%c", s);
s = fgetc(f);
}
fclose(f);
}
int main(void)
{
FILE *ptr = fopen("info.txt", "a");
if(ptr == NULL)
{
printf("Invalid Input\n");
return 1;
}
char *c = malloc(sizeof(char) * 101);
printf("One entry cannot be more than 100 characters long!\n");
printf("Enter your text here - ");
scanf("%[^\n]%*c", c);
fprintf(ptr, "%s\n", c);
fclose(ptr);
Print_File(ptr);
free(c);
}
在命令行执行程序后,当我手动打开文件时,它更新好了! 但是文件没有打印出来!我是不是把 Print_File() 函数写错了?
看看the manual page for fopen()
。它以文件名作为第一个参数,它 returns 一个 FILE *
。你这样做是错误的。
你应该改变这个:
fopen("f", "r");
为此:
FILE *f;
f = fopen("file-name-here", "r");
if (f == NULL) {
puts("Error opening file!");
exit(1);
}
其次,将关闭的文件ptr
传递给函数是没有用的。在调用函数之前 打开它(并且此时不要在其中使用 fopen()
)或者只是将函数声明为不带参数并在内部打开它。
选项 1:
void Print_File(FILE *f)
{
// ... use already opened file ...
}
// Then in main:
ptr = fopen("file-name-here", "r");
if (ptr == NULL) {
puts("Error opening file!");
exit(1);
}
Print_File(ptr);
选项 2:
void Print_File(void) // void == takes no arguments
{
FILE *f;
f = fopen("file-name-here", "r");
// ...
}
// Then in main:
Print_File();
最后,fgetc()
returns 一个 int
。您需要使用 int
变量来保存结果,否则您将无法区分有效字符和 EOF
:
int s = fgetc(f);
while (s != EOF)
{
// ...
完整的工作示例:
#include <stdio.h>
#include <stdlib.h>
void Print_File(FILE *f)
{
int s = fgetc(f);
while (s != EOF)
{
printf("%c", s);
s = fgetc(f);
}
fclose(f);
}
int main(void)
{
FILE *ptr = fopen("info.txt", "a");
if(ptr == NULL) {
printf("Error opening file for writing.\n");
return 1;
}
char *c = malloc(sizeof(char) * 101);
if (c == NULL) {
printf("Error allocating memory.\n");
return 1;
}
printf("One entry cannot be more than 100 characters long!\n");
printf("Enter your text here - ");
scanf("%[^\n]%*c", c);
fprintf(ptr, "%s\n", c);
fclose(ptr);
ptr = fopen("info.txt", "r");
if(ptr == NULL) {
printf("Error opening file for reading.\n");
return 1;
}
Print_File(ptr);
free(c);
return 0;
}
输出:
$ gcc prog.c -o prog
$ ./prog
One entry cannot be more than 100 characters long!
Enter your text here - HELLO WORLD
HELLO WORLD
$ ./prog
One entry cannot be more than 100 characters long!
Enter your text here - HELLO WORLD AGAIN
HELLO WORLD
HELLO WORLD AGAIN