SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old April 6th, 2003, 07:20 PM
korn korn is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Croatia
Posts: 18 korn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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?

Reply With Quote
  #2  
Old April 6th, 2003, 09:55 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
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.

Reply With Quote
  #3  
Old April 7th, 2003, 11:12 AM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
Quote:
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 11:52 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > insert a string into file at exact position


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway