Reputation: 8065
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:
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
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:
mailer.txt
to tempmailer.txt
ALL the lines irrespective of any conditions given(undesired)mailer.txt
and creates a new mailer.txt
(desired)tempmailer.txt
to the new file as such(desired)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
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
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
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
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
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