Usenet.com

www.Usenet.com

Group Index

Comp Thread Archive from Usenet.com

<-- __Chronological__ --> <-- __Thread__ -->

Re: how to update a text file in C?



"Rajiv" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Hi,
> I was wondering if you could update a text file in C in read/write
> mode (r+)
> I want to do something like this
> void uppercase(char *fname) {
>   FILE *fp;
>   int c;
>   fp=fopen(fname,"r+");
>   if(fp==NULL) {
>     printf("Unable to open file %s\n",fname);
>     exit(1);
>   }
>   while((c=fgetc(fp))!=EOF)
>     fputc(toupper(c),fp);
>   fclose(fp);
> }
> (I know this will not work, but is there anyway I can achieve this
> functionality of updating a text file in r+ mode. I DONT WANT to copy
> the contents of the text file to a temp file and then process the temp
> file, rename the temp file to fname and finally delete the original
> file.
> Please help me.
> Thanks,
> Rajiv
> --
> comp.lang.c.moderated - moderation address: [EMAIL PROTECTED]
I will write this in this way:
int changeFile (char *fileName)
{
 char c;
 int hand;

 hand = open(fileName, O_RDWR);

 while(!eof(hand)) {
  read(hand, &c, sizeof(char));
  lseek(hand, -1, SEEK_CUR);
  c = (char)toupper(c);
  write(hand, &c, sizeof(char));
 }

 close (hand);

 return 1;
}
-- 
comp.lang.c.moderated - moderation address: [EMAIL PROTECTED]



<-- __Chronological__ --> <-- __Thread__ -->


Usenet.com



Please check out one of the premium Usenet Newsgroup Service Providers below for access to Usenet.