如果你只是想将文件中的所有字符转换为大写并将结果写回同一个文件,直接的方法是打开文件进行读取,获取文件的长度并分配一个缓冲区来保存整个文件并将整个文件读入缓冲区,然后关闭文件。然后循环遍历缓冲区中的每个字符,对每个字符调用 toupper() 并将缓冲区转换为全部大写。然后再次打开文件进行写入,这将截断文件,然后将整个缓冲区写回文件,关闭文件并在完成后释放缓冲区。
将要转换的文件名作为第一个参数的简短示例可能是:
以"r"(读取)模式打开文件
...
int main (int argc, char **argv) {
char *filebuf = NULL;
long fplen = 0;
FILE *fp = NULL;
if (argc < 2) { /* validate argument given for filename */
fprintf (stderr, "usage: %s filename\n", argv[0]);
return 1;
}
if (!(fp = fopen (argv[1], "r"))) { /* open/validate file open for read */
perror ("fopen-read");
return 1;
}
确定文件长度
fseek (fp, 0, SEEK_END); /* seek end of file */
if ((fplen = ftell (fp)) == -1) { /* get file length */
perror ("ftell-length");
return 1;
}
fseek (fp, 0, SEEK_SET); /* seek beginning */
为filebuf分配存储空间
/* allocate memory for file */
if (!(filebuf = malloc (fplen * sizeof *filebuf))) {
perror ("malloc-filebuf");
return 1;
}
将整个文件读入filebuf并关闭文件
/* read file into filebuf */
if (fread (filebuf, 1, fplen, fp) != (size_t)fplen) {
perror ("fread-filebuf");
return 1;
}
fclose (fp); /* close file after read */
将filebuf 转换为大写
for (long i = 0; i < fplen; i++) /* convert all chars toupper */
filebuf[i] = toupper(filebuf[i]);
打开文件以写入"w" 模式并将filebuf 写入文件并关闭
if (!(fp = fopen (argv[1], "w"))) { /* open/validate file open for write */
perror ("fopen-write");
return 1;
}
/* write filebuf to file */
if (fwrite (filebuf, 1, fplen, fp) != (size_t)fplen) {
perror ("fwrite-filebuf");
return 1;
}
if (fclose (fp) == EOF) /* validate close-after-write */
perror ("fclose_after-write");
(注意:您始终验证 close-after-write 以捕获与刷新流相关的任何错误,而这些错误在您对 fwrite 的验证中不会被捕获)
免费filebuf内存
free (filebuf);
简而言之就是这样。总而言之,您可以这样做:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
int main (int argc, char **argv) {
char *filebuf = NULL;
long fplen = 0;
FILE *fp = NULL;
if (argc < 2) { /* validate argument given for filename */
fprintf (stderr, "usage: %s filename\n", argv[0]);
return 1;
}
if (!(fp = fopen (argv[1], "r"))) { /* open/validate file open for read */
perror ("fopen-read");
return 1;
}
fseek (fp, 0, SEEK_END); /* seek end of file */
if ((fplen = ftell (fp)) == -1) { /* get file length */
perror ("ftell-length");
return 1;
}
fseek (fp, 0, SEEK_SET); /* seek beginning */
/* allocate memory for file */
if (!(filebuf = malloc (fplen * sizeof *filebuf))) {
perror ("malloc-filebuf");
return 1;
}
/* read file into filebuf */
if (fread (filebuf, 1, fplen, fp) != (size_t)fplen) {
perror ("fread-filebuf");
return 1;
}
fclose (fp); /* close file after read */
for (long i = 0; i < fplen; i++) /* convert all chars toupper */
filebuf[i] = toupper(filebuf[i]);
if (!(fp = fopen (argv[1], "w"))) { /* open/validate file open for write */
perror ("fopen-write");
return 1;
}
/* write filebuf to file */
if (fwrite (filebuf, 1, fplen, fp) != (size_t)fplen) {
perror ("fwrite-filebuf");
return 1;
}
if (fclose (fp) == EOF) /* validate close-after-write */
perror ("fclose_after-write");
free (filebuf);
}
输入文件示例
$ cat dat/cj2upper.txt
This is a tale
Of Captain Jack Sparrow
A Pirate So Brave
On the Seven Seas.
使用示例
$ ./bin/fread_file_toupper dat/cj2upper.txt
生成的输出文件
$ cat dat/cj2upper.txt
THIS IS A TALE
OF CAPTAIN JACK SPARROW
A PIRATE SO BRAVE
ON THE SEVEN SEAS.
检查一下,如果您有任何问题,请告诉我。有不止一种方法可以做到这一点,但这可能是更直接的途径之一。