Hey Guyz, i hav a pretty simple task, i had got some text files of data, which are like scores. and i need to be able to edit the txt file to say change a charector A -> X or B-> R etc.. etc.. it would be great if i can do this with out creating a buffer file or something..

IF any one has a solution it would be great.

Thnx in advance

8 Years
Discussion Span
Last Post by ArkM

If the replacement text is exactly the same length as the text being replaced, you can search the file and then overwrite what you need when you find it. Otherwise, you're probably stuck with the temporary file approach.

Alternatively, you can keep the records in memory and edit them to your liking, then write them to the file en masse at predefined intervals.


like Narue said, your replacements have to be of precisely identical lengths. if there's any variation, even including whitespace such as tabs and newlines, you'll wreck the file.

if the replacements characters are indeed one-to-one overwrites, you will use the following commands:


cheat_in_place(const char* sheet, char looser, char winner)
    int  n = 0;
    long fsz;
    char* buf, *p;
    FILE* f;
    if (!sheet) return 0;
    f = fopen(sheet,"r+b");
    if (!f) return 0;
    if (fseek(f,0L,SEEK_END) == 0
    && (fsz = ftell(f)) > 0)
    else {
        return 0;
    buf = (char*)malloc(fsz+1); /* cast in C++ only */
    if (fread(buf,1,fsz,f) == fsz) {
        buf[fsz] = '\0';
        for (p = buf; (p = strchr(p,looser)) != 0; n++)
            *p++ = winner;
        if (n) {
    return n;

Hey guys so iv been tring different things, it kinda wat i want it 2 do, but instead of supplying it a string of text, i want my file to be that text.

#include       <stdio.h>
#include 	<fcntl.h>
#include 	<unistd.h>
#include	<stdlib.h>
#include	<string.h>

#define BUFFERSIZE 200
#define TEXTMODE 0664

void error_manage( char *, char * );
void intro( void);

main(int argc, char *argv[] )
	int out_fd;
	char choice;
	char buff[BUFFERSIZE];
	char *pFound = NULL;
	int len = strlen( argv[1] );

	/* Check to see if the arguments are put in correctly */
        if( argc >  4 )
        {       /*Print out Error for too many arguments when trying to run the program.*/
                printf( "\nError: You put in too many arguments." );
                printf( "\n\nUsage: %s string1 string2 output_filename\n\n", argv[0] );
        if( argc < 4 )
                /*Print out Error message for not enough arguments.*/
                printf( "\nError: Not enough arguments!" );
                printf( "\n\nUsage: %s string1 string2 output_filename\n\n", argv[0] );
	 /*check whether the initial and replacement strings are of same size.*/
	if( strlen( argv[1] ) != strlen( argv[2] ) )
		printf( "\nError: string 1 must be of equal length to string 2\n\n" ); 

	fgets( buff, BUFFERSIZE, stdin ); 		/*put text from stdin into buffer*/

	/* Output statements */
	printf( "\nReplacing \"%s\" with \"%s\" in: %s", argv[1], argv[2], buff );
	printf( "\n\nDo you want to continue? y/n: ");
	scanf( "%c", &choice );

	/* This switch is to confirm that you really want to replace arg1 with arg2 */
	switch ( choice ) 
	   case 'Y': case 'y':
		printf( "\nLets replace them.......\n\n" );
	   case 'N': case 'n':
		printf( "\nNot a valid option!\n\n" );
	}/*End of switch */
        /*While loop that does the search and replace of the 1st and 2nd line arguments */
	while( pFound  = strstr( buff, argv[1] ) ) 	
		if( pFound == -1 )
			printf( "\nThere is nothing to replace\n\n" );
	  	strncpy( pFound, argv[2], len );

	} /* End of While */

	if ( ( out_fd = creat( argv[3], TEXTMODE ) ) == -1 )
	        error_manage( "Cannot create file ", argv[3] );

        if ( write( out_fd, buff, strlen( buff ) ) != strlen( buff ) )
        	error_manage( "Write error to ", argv[3] );
	if (strlen( buff ) == -1 )
		error_manage( "Read error from ", argv[1] );

        /* close file  */

        if ( close( out_fd ) == -1 )
                error_manage( "Error closing files","" );

	printf( "Done. Check %s to see the change.\n\n", argv[3] );
} /* End of Main */

void error_manage( char *s1, char *s2 )
        fprintf( stderr,"Error: %s ", s1 );
void intro  ( void )
        printf( "\nThis program will now find and replace the first argument with the\n" );
        printf( "second argument within the text you write next.\n\n" );
        printf( "Input the string of text and press enter:\n" );

any suggestion

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.