Solved

File I/O

Posted on 2003-10-24
3
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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