Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22016

This code snippet is working the first while loop but is not executing the one after it

$
0
0

This code is supposed to skip a line of file and write everything else in a different file, delete the original one, and rename the different one to the one deleted. Whats wrong with this code is its not working after the first file ,i.e., the second file it is niether deleted nor a new file is created with the skipped line of file. what is the issue? Does it has to do something with the rename remove function?

FILE *lnameFILE *idFILE *replname = fopen("lname.txt", "r");id = fopen("id.txt", "r");rep = fopen("rep.txt", "w+");char ch1,ch2;int temp=1,delete_line=3; /*(delete_line is supposed to be taken as an input)*/ch1 = getc(lname);  while (ch1 != EOF)  {      if (ch1 == '\n')          temp++;     if(delete_line==1){      if (temp == 2 && ch1 == '\n')          ch1 = getc(lname);}      if (temp != delete_line)          putc(ch1, rep);​      ch1 = getc(lname);  }  fclose(lname);  fclose(rep);  remove("lname.txt");  rename("rep.txt","lname.txt");  rep = fopen("rep.txt", "w+");​​  ch2 = getc(id);  while (ch2 != EOF)  {      if (ch2 == '\n')          temp++;      //except the line to be deleted      if (temp == 2 && ch2 == '\n') //making sure to skip a blank line if delete_line=1          ch2 = getc(id);​      if (temp != delete_line)          putc(ch2, rep);​      ch2 = getc(id);  }  fclose(id);  fclose(rep);  remove("id.txt");  rename("rep.txt","id.txt");

data in id.txt

asd123xcv1323rijr123eieir2334

data in lname.txt

Bipul DasStar LordTony StarkVin Diesel

Viewing all articles
Browse latest Browse all 22016

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>