Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Altering The Content Of A File

Open File ungetc getc fopen updating content replacing content fflush

  • Please log in to reply
7 replies to this topic

#1 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 21 June 2012 - 01:35 AM

Earlier today I was answering on a post. The poster wants to replace all the bytes in a file with zero (0). He posted in general discussion section and he has not said about any programming language to solve it. This is easy to solve in C# and such but I was trying to solve it with C with no platform dependent function call. All the functions -- fprintf, fputs, fputc, fwrite etc -- that write to a file, increases the file size. And I discovered that I don't know about any C functions that can remove a part of the content of a file or replace the content of the file or reduce the size of the file content. Believe me I spent approx one hour on this but I found no solution. The only function I found closer to what I want is the ungetc function. Quote about what ungetc does is "A character is virtually put back into an input stream at the same position the last character was read and the internal file position indicator is decreased back to that previous position so that this character is returned by the next call to a reading operation on that stream." And I wrote the following code with it.
#include <stdio.h>

int main()
{
      FILE* fptr = fopen("c:\\myfile.rt", "r+");
      char c;
      if (fptr) {
      /* Advance file position by one */
      c = fgetc(fptr);
      while(!feof(fptr)) {
            /* ungetc decreases file position by one */
            c = ungetc(0, fptr);
            /* So we  need to advance the file position by two*/
            c = fgetc(fptr);
            c = fgetc(fptr);
      }
      fflush(fptr);
      fclose(fptr);
  }
   return 0;
}
So the fact is that ungetc modify the buffer associated with a file but not the file itself -- not even after flushing the buffer.

So can anyone help on this?
  • 0

#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 21 June 2012 - 11:02 AM

Just a shot in the dark, but you are not writing it to the file. Try a putc inside the loop.
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 21 June 2012 - 06:41 PM

Just a shot in the dark, but you are not writing it to the file. Try a putc inside the loop.

I'm sorry to say but I think perhaps you have missed the point I wanted to say. Anyway, FYI, putc will increase the size of the file by one.
  • 0

#4 rocketboy9000

rocketboy9000

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 79 posts

Posted 24 June 2012 - 02:55 PM


#include "stdio.h"

int main(int argc,char ** argv){//use the command line arguments

   if(argc<1)return 1;//make sure we have a filename to use

   char *fname=argv[1];//get a pointer to the filename

   FILE *f=fopen(fname,"r+");//open the file

   int i;

   while((fgetc(f))!=EOF)i++;//count the characters in the file

   rewind(f);//go back to the beginning of the file

   while(i-->0)fputc(0,f);//write that number of zero characters into the file

   return 0;//all done, woohoo

}


  • 0

#5 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 24 June 2012 - 08:56 PM

#include "stdio.h"
int main(int argc,char ** argv){//use the command line arguments
   if(argc<1)return 1;//make sure we have a filename to use
   char *fname=argv[1];//get a pointer to the filename
   FILE *f=fopen(fname,"r+");//open the file
   int i;
   while((fgetc(f))!=EOF)i++;//count the characters in the file
   rewind(f);//go back to the beginning of the file
   while(i-->0)fputc(0,f);//write that number of zero characters into the file
   return 0;//all done, woohoo
}

The above code crash at runtime as i is not initialized to a initial value.

I guess you both (lespauled & rocketboy9000) have not understand yet what putc is doing : It is increasing your file size man!.

rocketboy9000, have you tested your code? please test it and let me know your result.
  • 0

#6 rocketboy9000

rocketboy9000

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 79 posts

Posted 25 June 2012 - 09:02 AM

With the line changed to "int i=0;",
the following is done:
$ cat >test
shkdhkfdhkhashfadkbadfwruhfbwoeucy
$ wc -c test
35 test
$ ./a.out test
$ wc -c test
35 test
$ od test
0000000 000000 000000 000000 000000 000000 000000 000000 000000
*
0000040 000000 000000
0000043
Works perfectly.

The key to this is that when you open a file in writing "w" mode, it erases everything that was previously in the file. Then you can simply write zeroes until it is the same size as it was.
  • 0

#7 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 25 June 2012 - 09:13 AM

Well, that't not exactly what I want. I want to open in append mode so that the content exists and then want to replace the characters with zero.
  • 0

#8 Yonatan

Yonatan

    CC Regular

  • Member
  • PipPipPip
  • 37 posts
  • Location:Israel
  • Programming Language:C, Java, C++, C#, JavaScript, PL/SQL, Visual Basic .NET
  • Learning:Python, JavaScript

Posted 25 June 2012 - 02:37 PM

--Edit: never mind won't work.
  • 0





Also tagged with one or more of these keywords: Open File, ungetc, getc, fopen, updating content, replacing content, fflush

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download