Harikrishnan
Harikrishnan

Reputation: 8065

File handling program not working as expected in C

FILE *mails;
FILE *tempmails;
mails = fopen("mailer.txt", "r");
tempmails = fopen ("tempmailer.txt" , "a+");
char line[200],templine[200];
char blnklne[]="\n";        
while(fgets(line, sizeof line, mails) != NULL)
{       
    int flag=0;    
    while(fgets(templine, sizeof line, tempmails) != NULL)
    {               
        if((strcmp(line,templine)==0) || (strcmp(line,blnklne)==0))
        {               
            flag = 1;               
        }               
    }           
    if(flag == 0)
    {           
        fputs(line, tempmails);             
    }       
}       
fclose(mails);
fclose(tempmails);
tempmails = fopen ("tempmailer.txt", "r");
remove("mailer.txt");
FILE *newmails;
newmails = fopen("mailer.txt", "a");
while(fgets(templine, sizeof line, tempmails) != NULL)
{       
    fputs(templine, newmails);      
}       
fclose(newmails);
fclose(tempmails);
remove("tempmailer.txt");

I have written the above C code for the following purposes:

  1. Have to read each line from mailer.txt and check if a line is blank or repeated and if both conditions are false have to enter it to a temp file tempmailer.txt
  2. Remove the file mailer.txt and then create a new one and copy enter it into the new file one by one and then remove tempmailer.txt.

But on running what actually happens is:

  1. Copies from mailer.txt to tempmailer.txt ALL the lines irrespective of any conditions given(undesired)
  2. Deletes mailer.txt and creates a new mailer.txt(desired)
  3. Copies from tempmailer.txt to the new file as such(desired)
  4. Deletes tempmailer.txt(desired)

Whatever I do, I can't erradicate this problem. The OS is linux. Please help me. Thanks in advance.

Upvotes: 0

Views: 702

Answers (5)

pmg
pmg

Reputation: 108938

Reset tempmails to the beginning of the file before starting each 2nd loop.

while(fgets(line, sizeof line, mails) != NULL)
{
    int flag=0;
    rewind(tempmails);                              /* go back to the begining */
    while(fgets(templine, sizeof line, tempmails) != NULL)
    {
        /* ... */
    }
}

Upvotes: 2

MYMNeo
MYMNeo

Reputation: 836

Maybe this part of code is the reason why you got your question 1

while(fgets(line, sizeof line, mails) != NULL)
{       
     int flag=0;    
     while(fgets(templine, sizeof line, tempmails) != NULL)
    {               
        if((strcmp(line,templine)==0) || (strcmp(line,blnklne)==0))
       {               
            flag = 1;               /* this part may be not correct */
        }               
    }           
    if(flag == 0)
    {           
        fputs(line, tempmails);             
    }       
}         

If the program finds a line that makes if condition true, then the flag is set to 1, but next line maybe not make if condition true, and you program can not set the flag to 0.So you will never put the mismatch lines which are after the the matched line into the tempmails.

Upvotes: 1

Jon Cage
Jon Cage

Reputation: 37490

You need to seek to the beginning of the file before the second while loop.

As it stands, your second while loop will never find any matching lines because it will always be pointed to the end of the file.

Upvotes: 2

Tony The Lion
Tony The Lion

Reputation: 63220

Disclaimer: This is not really an answer, but can definitely aid in debugging!

After the following lines, you should check if the pointers are NULL or not:

mails = fopen("mailer.txt", "r");
tempmails = fopen ("tempmailer.txt" , "a+");

if (mails == NULL) printf("Error: could not open file");
if (tempmails == NULL) printf("Error: could not open file");

Now you can at least know if it can open and read the files. To check whether fgets works, and isn't giving you errors, use ferror or feof.

You should add the same NULL checks for every time you call fopen on a FILE handle.

Upvotes: 0

unwind
unwind

Reputation: 399881

The repeat-detection code is very strange, it's reading from both files at the same time. You can't read from a file opened for append. Try mode a+.

This is solvable with a trivial shell script, do you really have to write it in C?

Upvotes: 1

Related Questions