Solved

File I/O

Posted on 2003-10-24
3
196 Views
Last Modified: 2010-04-02
what is the easiest way to insert or remove something in the middle of a file?
is there a way to do it without rewriting the whole file? (aka the file is HUGE) and i dont want to overwrite exsisting data, I want to selectively insert and delete data
0
Comment
Question by:GaryJoe
3 Comments
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9616362
0
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 125 total points
ID: 9617101
The quick answer is "no", except in very rare circumstances.  A file is a serial array
of bytes laid out on the disk.  To insert or delete data from the middle of the file
requires moving all data after the insertion/deletion point either backward or forward.

Here is code (from a previous question) that removes the first line of a file and shuffles
everything down.

#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
  FILE * fin = fopen(argv[1], "r");
  FILE * fout = fopen(argv[1], "r+");
  char buffer[4096];

  fgets(buffer, sizeof(buffer), fin);   // read first line & discard it

  while (fgets(buffer, sizeof(buffer), fin))
    fputs(buffer, fout);

  fclose(fin);
  ftruncate(fileno(fout), ftell(fout));
  fclose(fout);

  return 0;
}


Inserting is only slightly more difficult.  It involves rewriting the end of the file.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char **argv)
{
  FILE * fin = fopen(argv[1], "r");
  FILE * fout = fopen(argv[1], "r+");
  char *newline = "Insert this after line 3\n";
  off_t startgap, endgap, curpoint;
  int i, gaplen;
  static char buffer[1024*1024];

  /* skip passed the first 3 lines unmolested */
  for (i = 0; i < 3; i++)
    fgets(buffer, sizeof(buffer), fin);

  /* remember the start and endpoints of the gap we are making */
  startgap = ftello(fin);
  gaplen = strlen(newline);
  endgap = startgap + gaplen;

  /* move to the end of the file and start moving it out */
  fseeko(fin, 0, SEEK_END);
  curpoint = ftello(fin);

  while (curpoint > startgap) {
    off_t len = ((curpoint - startgap) > sizeof(buffer)) ? sizeof(buffer) : (curpoint - startgap);
    fseeko(fin, curpoint-len, SEEK_SET);
    curpoint = ftello(fin);
    fseeko(fout, curpoint+gaplen, SEEK_SET);
    fread(buffer, 1, len, fin);
    fwrite(buffer, 1, len, fout);
  }


  /* now write the new line into the gap we opened up */
  fseeko(fout, startgap, SEEK_SET);
  fputs(newline, fout);

  fclose(fin);
  fclose(fout);

  return 0;
}
0
 
LVL 1

Expert Comment

by:Mustak_Shaikh
ID: 9617154
if you want to modiy one character then you can proceed with the following code:

#include<iostream.h>
#include<fstream.h>
int main()
{
      ofstream out_file;
      out_file.open("c:\\sample.txt");
      if(out_file.bad())
      {
            cout<<"Invalid file name"<<endl;
            return -1;
      }

      out_file.seekp(5);
      out_file.write("G",1);
      out_file.close();
      return 0;
}

Mustak_Shaikh
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now