Solved

File I/O

Posted on 2003-10-24
3
199 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

910 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

24 Experts available now in Live!

Get 1:1 Help Now