Solved

File I/O

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TCP/IP Socket - connection close results in data lost 14 140
mixing C++ & C# in Vis Studio 2013 7 198
Create a path if not exists 7 89
designing in object programming 12 94
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 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