gpt4 book ai didi

c - 如何在C中将文件中每一行的首字母设为大写

转载 作者:太空宇宙 更新时间:2023-11-04 03:52:32 25 4
gpt4 key购买 nike

在 text.txt 文件中,我有一些行,例如“Apples are red.guavas are green.lemons are yellow”。我想要新行中的第一个字母( Guava 中的 g,柠檬中的 l 大写)。但是文件中的输出是相同的...

#include<stdio.h>
main()
{
FILE *p;
char c;
int i;
int end_of_line=0;
p=fopen("text.txt","r+");//opening file for reading & writing.

while(c!=EOF)
{
c=fgetc(p);
if(end_of_line==1) // if it is a new line
if (islower(c)!=0) // and if the first letter is small
fputc(toupper(c),p); //change the small to capital
if(c=='.')
end_of_line=1;
else
end_of_line=0;
}
fseek( p, 0, SEEK_SET ); //setting the file pointer to the start of file
while((c=fgetc(p))!=EOF)
printf("%c",c);
fclose(p);
}

最佳答案

For files open for update (those which include a "+" sign), on which both input and output operations are allowed, the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a writing operation followed by a reading operation or a reading operation which did not reach the end-of-file followed by a writing operation.

我通过在行前后使用 ftell anf fseek 让你的示例工作:

fputc(toupper(c),p);

关于c - 如何在C中将文件中每一行的首字母设为大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19615557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com