Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<stdio.h>
int main()
{
    FILE *fptr;char ch;
    char fname[15];
    puts("Enter file name with directory");
    gets(fname);
    fptr=fopen(fname,"r+");
    if(fptr==NULL)
    {
        puts("Not successful");
    }
    else{
            puts("Successfully opened");getch();
    while(1)
    {
        ch=fgetc(fptr);
        if(ch==EOF)
        {
            break;
        }
        if(ch==';')
        {
           fseek(fptr,-1,SEEK_CUR);
           fputc('$',fptr);
           fseek(fptr,0,SEEK_CUR);---------->//////[HERE]//////
        }
    }


    }
    puts("Replacement Completed successfully");
    fclose(fptr);
    return 0;
}

This i a proagram in c to replace a character in file with other....if i remove the line [HERE] this proagram never stops..But this doesnt make anu sence..as fseek(fptr,0,SEEK_CUR); doesnot do anything..please help me out

What I have tried:

i trien in codeblocks and devcpp
Posted
Updated 30-Jul-17 21:21pm
v2
Comments
Graeme_Grant 31-Jul-17 2:53am    
And your problem is????

Please click on "Improve question" and update with more information. If you don't know how to do this, then please read this: Code Project Quick Answers FAQ - How to Impove a question[^]
Jochen Arndt 31-Jul-17 3:23am    
The problem description was at the end of the code block.
I have edited the question to show it now as normal text.
Graeme_Grant 31-Jul-17 3:25am    
I was sure the problem wasn't there before... I must have missed it. Sorry.
Richard MacCutchan 31-Jul-17 3:21am    
I have a feeling that the problem is caused by reading and writing the same file, which is not recommended for stream files.

It makes perfect sense.
fseek sets the file position to the selected place.
And if you read the documentation: fseek - C++ Reference[^] it is very clear:
Quote:
The end-of-file internal indicator of the stream is cleared after a successful call to this function, and all effects from previous calls to ungetc on this stream are dropped.

So with that line in, the next call to fgetc will not return EOF.
 
Share this answer
 
Comments
Gowtham Gampala 31-Jul-17 3:23am    
Can u please elobarate???Iam now to programming..
OriginalGriff 31-Jul-17 3:41am    
Read the documentation. I can't elaborate on that!
See fseek - C++ Reference[^]:
Quote:
On streams open for update (read+write), a call to fseek allows to switch between reading and writing.
That means whenever you change the IO direction of a stream you have to call fseek. The reason is that there are two internal file positions: One for reading and one for writing but only the corresponding one is updated with a read or write. fseek will update both to the position of the last operation.

In your case the fputc('$') will update the write position but not the read position that will be still on the position of the ';' character. So that character will be read again with the next loop iteration resulting in an infinite loop. But the fseek call sets the read position to the current write position which is one behind.

BTW:
Do you think a buffer of 15 characters is sufficient to hold a full path (directory with file name)?
Use PATH_MAX instead which is defined in limits.h.
 
Share this answer
 
Comments
Gowtham Gampala 31-Jul-17 3:37am    
Thank u sir :)
Gowtham Gampala 31-Jul-17 3:45am    
Iam using default directory...where the program is present
Jochen Arndt 31-Jul-17 3:50am    
You are printing "Enter file name with directory".

15 would be sufficient for DOS (8.3 file names) but not for Windows or Linux where file names may be longer.

Thank you also for accepting my solution.
Gowtham Gampala 14-Aug-17 6:53am    
yeah but...still iam using default directory
Jochen Arndt 14-Aug-17 8:00am    
But even the default directory can contain files with longer names like
"This_is_a_quite_long_file_name.ascii"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900