1

在 text.txt 文件中,我有一些行,例如“苹果是红色的。番石榴是绿色的。柠檬是黄色的”。我希望新行中的第一个字母(番石榴中的 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);
}
4

1 回答 1

1

对于为更新而打开的文件(包含“+”号的文件),在这些文件上允许输入和输出操作,应在写入操作之后刷新(fflush)或重新定位(fseek、fsetpos、rewind)流读取操作或未到达文件末尾的读取操作,然后是写入操作。

我通过在行前后使用ftellanf来使您的示例正常工作:fseek

fputc(toupper(c),p);
于 2013-10-27T08:16:56.150 回答