C 如何从文件中读取数据,然后编辑并在同一文件中打印?
我要做的是从文件中读取以下文本: 明天,明天,明天, 每天都以这种小节奏爬行, 直到记录时间的最后一个音节; 我们所有的昨天都照亮了傻瓜 通往尘土飞扬的死亡之路。熄灭,熄灭,短暂的烛光! 生活只是一个行走的影子,一个可怜的球员 他在舞台上昂首阔步,烦躁不安 然后就再也听不见了。这是一个故事 由一个白痴说的,充满了喧嚣和愤怒 毫无意义 并将其全部更改为大写字母 如果我从一个文件中读取文本并打印到另一个文件中,我就可以这样做C 如何从文件中读取数据,然后编辑并在同一文件中打印?,c,C,我要做的是从文件中读取以下文本: 明天,明天,明天, 每天都以这种小节奏爬行, 直到记录时间的最后一个音节; 我们所有的昨天都照亮了傻瓜 通往尘土飞扬的死亡之路。熄灭,熄灭,短暂的烛光! 生活只是一个行走的影子,一个可怜的球员 他在舞台上昂首阔步,烦躁不安 然后就再也听不见了。这是一个故事 由一个白痴说的,充满了喧嚣和愤怒 毫无意义 并将其全部更改为大写字母 如果我从一个文件中读取文本并打印到另一个文件中,我就可以这样做 #include <stdio.h> #include <
#include <stdio.h>
#include <ctype.h>
int main() {
FILE* input_file = fopen("some_text.txt", "a+");
char c = fgetc(input_file);
int charc = 0;
int alpha = 0;
while(1){
if(isalpha(c)){
alpha = alpha + 1;
}
charc = charc + 1;
fprintf(input_file,"%c",toupper(c));
c = fgetc(input_file);
if(feof(input_file)){
break;
}
}
fprintf(input_file,"\nTotal Characters: %d, Total alphabetical characters: %d",charc,alpha);
fclose(input_file);
return 0;
}
#包括
#包括
int main(){
FILE*input_FILE=fopen(“some_text.txt”、“a+”);
char c=fgetc(输入文件);
int charc=0;
intα=0;
而(1){
if(isalpha(c)){
α=α+1;
}
charc=charc+1;
fprintf(输入_文件,“%c”,toupper(c));
c=fgetc(输入文件);
if(feof(输入文件)){
打破
}
}
fprintf(输入文件,“\n总字符数:%d,总字母字符数:%d”,字符,字母);
fclose(输入_文件);
返回0;
}
如果您只是想将文件中的所有字符转换为大写并将结果写回同一个文件,直接方法是打开文件进行读取,获取文件长度并分配一个缓冲区来保存整个文件,将整个文件读取到缓冲区中,然后关闭文件。然后在缓冲区中的每个字符上循环,对每个字符调用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;
}
/* 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");
$ 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.
为文件buf分配存储
/* allocate memory for file */
if (!(filebuf = malloc (fplen * sizeof *filebuf))) {
perror ("malloc-filebuf");
return 1;
}
将整个文件读入filebuf
&关闭文件
...
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;
}
/* 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");
$ 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.
将filebuf
转换为大写
...
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;
}
/* 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");
$ 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.
(注意:您总是在写入后验证关闭,以捕获与刷新流相关的任何错误,这些错误在验证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);
}
示例使用
...
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;
}
/* 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");
$ 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.
结果输出文件
...
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;
}
/* 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");
$ 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.
仔细检查一下,如果有问题请告诉我。有不止一种方法可以做到这一点,但这可能是更直接的途径之一。几乎正确,这里有一些改变可以让它发挥作用
首先以二进制模式打开文件,这允许您使用fseek在文件中移动。rb+表示您可以读取/写入该文件。
添加了一些错误处理,如果文件不存在,则始终进行错误处理。
回写磁盘时,请确保将其刷新到磁盘,因为fgetc等人使用的是缓冲区。
在这种情况下,fputc更好,因为您只需编写一个字符
#include <stdio.h>
#include <ctype.h>
int main()
{
char filename[] = "some_text.txt";
FILE* input_file = fopen(filename, "rb+"); // open in binary mode
if ( input_file == NULL ) // some error handling
{
perror(filename);
return -1;
}
int c = fgetc(input_file); // return value is int
int charc = 0;
int alpha = 0;
do
{
if(isalpha(c))
{
alpha = alpha + 1;
}
charc = charc + 1;
// if alpha, then go back one character and rewrite it
if(isalpha(c))
{
fseek(input_file, -1, SEEK_CUR);
fputc(toupper(c),input_file); // use fputc instead of fprintf
fflush(input_file);
}
// read next
c = fgetc(input_file);
}
while (c != EOF);
// at end of file, add some more
fprintf(input_file,"\nTotal Characters: %d, Total alphabetical characters: %d",charc,alpha);
fclose(input_file);
return 0;
}
#包括
#包括
int main()
{
char filename[]=“some_text.txt”;
FILE*input_FILE=fopen(文件名,“rb+”)//以二进制模式打开
if(input_file==NULL)//一些错误处理
{
perror(文件名);
返回-1;
}
int c=fgetc(输入文件);//返回值为int
int charc=0;
intα=0;
做
{
if(isalpha(c))
{
α=α+1;
}
charc=charc+1;
//如果是alpha,则返回一个字符并重写它
if(isalpha(c))
{
fseek(输入文件,-1,查找当前);
fputc(toupper(c),input_file);//使用fputc而不是fprintf
fflush(输入_文件);
}
//接着读
c=fgetc(输入文件);
}
而(c!=EOF);
//在文件末尾,再添加一些
fprintf(输入文件,“\n总字符数:%d,总字母字符数:%d”,字符,字母);
fclose(输入_文件);
返回0;
}
出于性能原因,假设大多数字符需要转换,利用STDIO是有意义的,它将利用应用程序级缓冲
示例消除了可读性的错误检查
main(...)
{
// Open file
FILE *fp = ... ;
const int BUFFER_SIZE = 1024 ;
char buff[BUFFER_SIZE] ;
while ( 1 ) {
// Read
long loc = ftell(fp) ;
n = fread(buff, 1, sizeof(buff), fp) ;
if ( n <= 0 ) break ;
// Convert
int do_update = 0 ;
for (int i=0 ; i<n; i++ ) {
if ( islower(buff[i] ) {
buff[i] = toupper(buff[i])
do_update = 1 ;
} ;
if(isalpha(c)){
alpha = alpha + 1;
}
charc = charc + 1;
} ;
// Update, only if anything changed
if ( do_update ) {
long next = ftell(fp) ;
fseek(fp, loc, SEEK_SET) ;
fwrite(buff, 1, n, fp) ;
fseek(fp, next, SEEK_SET) ;
} ;
} ;
}
main(…)
{
//打开文件
文件*fp=;
const int BUFFER_SIZE=1024;
字符buff[缓冲区大小];
而(1){
//阅读
长loc=ftell(fp);
n=fread(buff,1,sizeof(buff),fp);
如果(n)您不想用C就地编辑文件(可以,但不能更改文件的长度-最好是dicy)。相反,打开一个文件进行读取,打开一个文件进行写入。从读取文件中读取数据,对数据进行任何更改并将其写回写入文件。然后删除读取文件并将写入文件移回原始读取文件名。另外,最好只使用while((c=fgetc(input_file))!=EOF){…}
(只需更改toupper(c)
并写回即可,但您必须小心地操作文件位置)就个人而言,处理此情况的最佳方法就是获取文件大小,声明一个缓冲区以保存整个文件,打开文件进行读取,使用fread()
要立即将整个文件读入缓冲区,请关闭文件,循环缓冲区中的每个字符,执行buffer[i]=toupper(buffer[i]);
然后打开文件进行写入,并通过调用fwrite()将整个缓冲区写回
@DavidRankin您的第二条评论肯定是最直接的方法。您应该将其作为答案发布……注意,在Windows上,fread返回的字节可能比fseek报告的要少(因为每个CRLF都转换为“\n”)@dash-o是的,这一点很好,但是将显式的size\u t size
参数设置为1
,这将消除多字节字符问题(并且基本上使touper