#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Croatia
    Posts
    18
    Rep Power
    0

    insert a string into file at exact position


    I need to insert a string into file at exact position. for example:

    file content:
    aaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaa

    and after inserting "bbbbbb" in 3rd row after 4th character
    aaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaa
    aaaabbbbbbaaaaaaaaaaaa
    aaaaaaaaaaaaaaaa

    I need this for edit .ini or .conf files in installation process.
    I want to write simple program like insert_ini.exe <line number> <place> <string to insert>. For upper example this would be:
    insert_ini.exe 3 4 "bbbbbb"

    any suggestions?
  2. #2
  3. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Since you're talking about inserting, and not overwriting, it involves moving all bytes in the file after the insert ahead the number of bytes you inserted. The only way to do this is to rewrite that portion of the file (as well as the bytes you insert). This means you must store a copy of the file somewhere (in memory or in another file). This can be done in a number of ways. If you're speaking about small sized files (like .ini files), you could have an array of character pointers (for each line of the file). Or just an array of characters (this will require more memory to be moved when inserts happen).

    Hope this helps.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Southern California
    Posts
    73
    Rep Power
    15
    I want to write simple program like insert_ini.exe <line number> <place> <string to insert>. For upper example this would be:
    insert_ini.exe 3 4 "bbbbbb"
    Here's one way:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <libgen.h>
    
    #define INI_FILE "test.ini"
    
    void usage(char *s) {
      printf("Usage: %s <line> <pos> <str>\n", basename(s));
      exit(0);
    }
    
    int main (int argc, char* argv[])
    {
      FILE *fp_in, *fp_out;
      int tline, tcnum;
      unsigned line =1;
      unsigned cnum =0;
      char infile[256];
      char insert[81];
      int ch;
    
      if (argc < 4)
    	usage(argv[0]);
    
      tline = atoi(argv[1]);
      tcnum = atoi(argv[2]);
    
      strncpy(insert, argv[3], 80);
      strcpy(infile, INI_FILE);
      strcat(infile, ".bak");
      rename(INI_FILE, infile);
    
      if ((fp_in = fopen(infile, "r")) == NULL) {
    	fprintf(stderr, "Unable to open file '%s'\n", infile);
    	return EXIT_FAILURE;
      }
    
      if ((fp_out = fopen(INI_FILE, "w")) == NULL) {
    	fprintf(stderr, "Unable to open file '%s'\n", INI_FILE);
    	return EXIT_FAILURE;
      }
    
      while ((ch = fgetc(fp_in)) != EOF) {
    	if (ch == '\n') {
    	  cnum = 0;
    	  line++;
    	}
    	else {
    	  ++cnum;
    	  if (line == tline && cnum == (tcnum+1)) {
    		fprintf(fp_out, insert);
    	  }
    	}
    
    	fputc(ch, fp_out);
      }
    
      fclose(fp_out);
      fclose(fp_in);
    
      return EXIT_SUCCESS;
    }
    Last edited by vpopper; April 7th, 2003 at 12:52 PM.

IMN logo majestic logo threadwatch logo seochat tools logo