Solved

File I/O

Posted on 2003-10-24
3
205 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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