user513164
user513164

Reputation: 1858

c file handling problem

hi every body can some one help me with thi problem i have a text file which is following

hi Hello this is my Hello to
 the Hello world

i write a code in c which is following

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

void main()
{
    FILE *fp,*fout;
    int i=0,len_string;
    char SearchText[]="Hello"; /* You can replace this text */
    char ReplaceText[]="Help"; /*You can also replace this text. */
    char temp[30];
    fp=fopen("ram.txt","a+");
    fout=fopen("temp.txt","a+");
    rewind(fp); /* for going to start of file. */
    if(fp==NULL || fout==NULL)
    {
        printf("File couldn't be opened ");
        exit(0);
    }
    len_string=strlen(SearchText);
    while(!feof(fp))
    {
        for(i=0;i<len_string;i++)
        { 
            temp[i]=fgetc(fp);
        }
        temp[i]='\0';
        if(strcmp(SearchText,temp)==0) /* the stricmp() is used for comparing both string. */
        {

            fprintf(fp,"%s ",ReplaceText);
            fprintf(fout,"%s",ReplaceText);
            fflush(fp);
            fclose(fp);
            fclose(fout);
            exit(1);
        }
    }
    fclose(fp);
    fclose(fout);
}

now my out put is like that

hi Hello this is my Hello to
 the Hello world

Help Help what i m doing wrong ? how to replace Hello to help in my text file ? how to get my output like that?

hi Help this is my Hello to
 the Help world

can anybody explain with code ?

Upvotes: 0

Views: 589

Answers (3)

maheshgupta024
maheshgupta024

Reputation: 7877

#include <stdio.h>
#include <string.h>
#define BUF_LEN 100
#define STR_REQ "Hello"
main()
{
    FILE * read_file, * write_file;
    read_file = fopen("file_read.dat", "r");
    if(read_file == NULL)
    {
        perror("Unable to open file for reading");
        exit(-1);
    }
    else
    {
        write_file = fopen("file_write.dat", "w");
        if(write_file == NULL)
        {
            perror("Unable to open file for writing");
            exit(-1);
        }
        else
        {
            char temp_buf[BUF_LEN], req_buf[BUF_LEN+strlen(STR_REQ)+10];
            char * index;
            while(!feof(read_file))
            {
                memset(temp_buf, 0, BUF_LEN);
                fscanf(read_file, "%[^\n]", temp_buf);
                fgetc(read_file);           /*To remove \n at last*/
                index = strstr(temp_buf, STR_REQ);
                if( index !=NULL )
                {

                    memset(req_buf, 0, BUF_LEN+strlen(STR_REQ)+10);
                    //strncpy(req_buf, temp_buf, index-temp_buf);
                    memcpy(req_buf, temp_buf, index - temp_buf);                /*This will copy upto the match*/

                    strcat(req_buf, "Help ");                                           /*Copy the help word*/

                    strcat(req_buf, index+strlen(STR_REQ));

                }
                else
                {
                    strcpy(req_buf, temp_buf);
                }
                fprintf(write_file, "%s\n", req_buf);
            }

        }
    }
}

Have this code.. it should work... If you have any question you can ask me

Upvotes: 0

Jonathan
Jonathan

Reputation: 13614

You are searching in 5-char groups for the string "Hello". So you are looking at your file like this:

hi He
llo t
his i
s my 
Hello
 to\n 
the H
ello 
world

Only one of those matches what you are looking for. You should probably read the file line-by-line (using a larger buffer, just in case), and search/replace on each line for your search text.

Additionally, you are exiting 0 on failure and 1 on success (which is backwards; 0 means success, anything else means failure, traditionally). And you are exiting after the first match, rather than continuing onward looking for more.

To read line by line and perform search/replace, do something like this:

FILE *f = ...;
char buf[1024];
while (fgets(buf, sizeof(buf), f)) {
    char *pos;
    while ((pos = strstr(buf, search)) != NULL) {
        char temp = *pos;
        *pos = 0;
        fprintf(out, "%s%s", buf, replace);
        *pos = temp;
        buf = pos + strlen(search);
    }
    fprintf(out, "%s", buf);
}

This isn't ideal, as a long line (> 1023 chars) will get cut into pieces, possibly in the middle of a search token, but it will work in most cases.

Upvotes: 2

maheshgupta024
maheshgupta024

Reputation: 7877

you have read line by line into a temporary character array, and use strstr to find the hello, then things will be faster.. check for strstr function.

Upvotes: 0

Related Questions