GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Jun-2005, 16:15
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Replacing N'th token in a file.


Hey.

I want to replace the N'th token found at byte number N with a new token.
I wrote the function to find the N'th character and it works just fine.
Here is my code :

CPP / C++ / C Code:
char *nth(FILE *my_file,int n)
{
	long lData_Size = sizeof(char);
	long file_size;
	char *buffer = NULL;

	//Calculate file size.
	fseek (my_file,No_Offset,SEEK_END);
	file_size = ftell (my_file);
	rewind (my_file);
	if (file_size < (long)n)
		printf("The File doen't contain %i characters.\n",n);
	else
	{
		if (fseek(my_file,n,SEEK_SET) == fseek_Failure)
			printf("fseek failed.\n");
		else
			fread (&buffer,Number_Of_Bytes,lData_Size,my_file);	
	}
	fclose(my_file);

	return(buffer);
}

I need direction with how to insert my own token into the file.
For example :
If I have the file :

my_file.txt
----------
first line
second line

and I want to use replace(my_file,5,"kobi") than the file would now be :

my_file.txt
----------
firskobi line
second line

But, If I use replace(my_file,1,"kobi") the result would be :

my_file.txt
----------
kobi line
second line

I came upon this issue while writing the assembler I told you guys about.
Any reference or idea would be most welcome.

Respect,
Kobi.
  #2  
Old 23-Jun-2005, 20:18
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Assuming the file you are trying to edit is arranged in certain structured manner when it was fist created, IMO one way to do it would be to read in the whole file into a runtime memory buffer (linked list of some sort), if the match for the token to be replaced is found, update the buffer and write the whole buffer back to the same file. Its as good as creating a new file with updated data.

This may not be the most efficient way of doing it but should get you started in the some direction.
  #3  
Old 24-Jun-2005, 03:00
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about
Quote:
Originally Posted by nkhambal
This may not be the most efficient way of doing it but should get you started in the some direction.

Thanks, but I am looking for an efficient way of doing the task. I want to update locally.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.