Thread: replacing string in file except the first

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    18

    replacing string in file except the first

    hi everyone
    it my first post here
    my problem is i want ot replace a string of text
    in a file the files are one a one word/one line
    bases
    i have come this far but i don't seem to get
    strstr() to work
    would it also be possible to a sort of temp file
    in place of asking for the tempfile
    i there a way that i can avoid gets()
    cause my compiler says that is dangerous
    but when i use fgets or scanf i got no result
    this my fisrt program ever so be nice
    if it looks crappy

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define BUFSIZE 255
    
    int main (void)
    {
    	char filename[BUFSIZE];
    	char tmpname[BUFSIZE];
    	char str_search[BUFSIZE];
    	char str_replace[BUFSIZE];
    	FILE *fp;
    	FILE *tmpfp;
    	char buffer[BUFSIZE];
    	int count = 0, count_lines = 0;
    	char *loc;
    	int first;
    	
    	puts("Enter filename to search: ");
    	gets(filename);
    
    	puts("Enter tmpfilename");
    	gets(tmpname);
    	
    	puts("Enter string to search: ");
    	gets(str_search);
    
    	puts("Enter string to replace");
    	gets(str_replace);
    
    	if ((fp = fopen(filename, "rb")) == NULL)
    	{
    		fprintf(stderr ,"Error opening filename");
    		exit(-1);
    	}
    	if ((tmpfp = fopen(tmpname, "wb")) == NULL)
    	{
    		fprintf(stderr,"Error opening filename");
    		exit(-1);
    	}
    
    	while(!feof(fp))
    	{
    		fgets(buffer,BUFSIZE,fp);
    		count++;
    	}
    	count_lines = count;
    	rewind(fp);
    	count = 0;
    	while(!feof(fp))
    	{
    		fgets(buffer,BUFSIZE,fp);
    		count++;
    		if((strstr(buffer, str_search) != NULL))
    		{
    			first = count;
    			break;
    		}
    	}
    	rewind(fp);
    
    	for(count = 0; count < count_lines-1; count++)
    	{
    		fgets(buffer,BUFSIZE,fp);
    		loc = strstr(buffer, str_replace);
    		printf("%d\n" , loc-buffer);
    		if (loc != NULL && count != count_lines)
    		{
    		fputs(str_replace, tmpfp);
    		}
    		else
    		{
    		fputs(buffer, tmpfp);
    		}
    	}
    	fclose(fp);
    	fclose(tmpfp);
    	/*rename(tmpname,filename);*/
    	return 0;
    }
    an example of the test file

    we
    came
    here
    we
    came
    here

    replace we with they or whatever

    thx in advance

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    18
    thx for your reply
    i tried it out
    and i am puzzled about two things
    fgets( buff, sizeof(buff), stdin ); //this is clear
    but this searches through the whole string
    for a new string if found replace with end char
    for strings
    { char *p = strchr( buff, '\n' ); if ( p != NULL ) *p = '\0'; }
    and
    Code:
    while ( fgets(buffer,BUFSIZE,fp) != NULL ) {
      if ( strstr( buffer, str_search ) != NULL ) {
        if ( !seen_first ) { //could you explan this if statenment i don't get it what suppossed to happen
          // this is the first, just set the flag
          seen_first = 1;
        } else {
          // all other matches
        }
      } else {
        // normal case
      }
    }
    is there also a way of only using one specified
    file by the user bu using a tmpfile
    my book says to use tmpname but
    again my comiler complains that this is dangerous

    thx again for your reply

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    18
    thx again for your quick reply

    the first one i understand now
    but when i follow the second piece of code
    i got this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define BUFSIZE 255
    
    int main (void)
    {
    	char filename[BUFSIZE];
    	char tmpname[BUFSIZE];
    	char str_search[BUFSIZE];
    	char str_replace[BUFSIZE];
    	FILE *fp;
    	FILE *tmpfp;
    	char buffer[BUFSIZE];
    	int seen_first = 0;
    	
    	puts("Enter filename to search: ");
    	fgets(filename,sizeof(filename),stdin);
    	char *p = strchr( filename, '\n' );
           		if ( p != NULL )
    	     		*p = '\0';
    	
    		
    	puts("Enter tmpfile: ");
    	fgets(tmpname, sizeof(tmpname), stdin);
    	p = strchr( tmpname, '\n' );
           		if ( p != NULL )
    	     		*p = '\0';
    
    	puts("Enter string to search: ");
    	fgets(str_search,sizeof(str_search),stdin);
    	p = strchr( str_search, '\n' );
           		if ( p != NULL )
    	     		*p = '\0';
    	
    	puts("Enter string to replace");
    	fgets(str_replace, sizeof(str_replace), stdin);
    	p = strchr( str_replace, '\n' );
           		if ( p != NULL )
    	     		*p = '\0';
    	
    
    	if ((fp = fopen(filename, "r")) == NULL)
    	{
    		fprintf(stderr ,"Error opening filename");
    		exit(-1);
    	}
    	if ((tmpfp = fopen(tmpname, "w")) == NULL)
    	{
    		fprintf(stderr,"Error opening filename");
    		exit(-1);
    	}
    
    	while(fgets(buffer,BUFSIZE,fp) != NULL);
    	{
    		if(strstr(buffer, str_search) != NULL)
    		{
    			if (!seen_first)
    			{
    			fputs(buffer, tmpfp);
    			seen_first = 1;
    			}
    			else
    			{
    			fputs(str_replace, tmpfp);
    			}
    		}
    		else
    		{
    		fputs(buffer, tmpfp);
    		}
    	}
    	fclose(fp);
    	fclose(tmpfp);
    	/*rename(tmpname,filename);*/
    	return 0;
    }
    result
    only the last line is in the file that
    i specified as tmpfile
    when i
    replace
    Code:
    	puts("Enter tmpfile: ");
    	fgets(tmpname, sizeof(tmpname), stdin);
    	p = strchr( tmpname, '\n' );
           		if ( p != NULL )
    	     		*p = '\0'
    with
    tmpname(tmpname);
    i get the following messages
    tmp/ccDoSU1B.o: In function `main':
    /tmp/ccDoSU1B.o(.text+0x179): the use of `tmpnam' is dangerous, better use `mkstemp'

    thx again for your help

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    18
    thx again for the quick reply
    ok i am getting nearer

    file to search
    we
    came
    here
    we
    came
    here
    we
    came
    here

    out put file
    we
    came
    here
    theycame
    here
    theycame
    here

    have you got any idea's
    on how this comes

    i didn't kwow that about the var
    will check the book again
    never tried c++(and i think i will keep it with c
    for the moment (define moment a year of two))

    i will experiment with tmp functions
    is it a problem reading and writing to
    binary files when it is a text file
    is a text file not also a binary file in a way

    thx again

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    18
    alreagy got it
    replaced
    [code]
    puts("Enter string to replace");
    fgets(str_replace, sizeof(str_replace), stdin);
    p = strchr( str_replace, '\n' );
    if ( p != NULL )
    *p = '\0';

    [\cpde]
    puts("Enter string to replace");
    fgets(str_replace, sizeof(str_replace), stdin);

    question is this save??????

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM