Showing posts with label common-mistakes. Show all posts
Showing posts with label common-mistakes. Show all posts

Sunday, April 3, 2011

One wrong click and all my files gone!

   Currently I am working on an application for syncing folders(yeah I know its a common app,but it's a useful one!).I as usual started could not compile the code in the first time coz of some syntax errors but what followed next was more horrific(for me) and enjoyable(for some).
   In my code I instead of copying the file, wrote the command for moving the file.Now, for the command we need to give a name for the output file. As this is a sync app, I need to give the same name to the output file.Hence, there came a villain named extractname(). It extracted the name Ok but it gave the name in the reverse order! Hence beads.c became c.sdaeb! Also, I forgot to add another '\' at the end of the folder name.
  But, this was a logical error and not a syntax error so it passed without any trouble till I ran it. While running it, I thought that there would not be a major problem and hence selected a not so important folder as the source and a new folder as destination.
  But, fate had something else in mind! Not only did all my files move out of the folder but as I had forgotten to put the extra '\' in place, they all went to the wrong folder and it's not over yet. The files were named like testc.sdaebs and testc.xiptas .I was stunned, confuse and angry over myself. Thankfully, after some debugging I came to know about the horrific mistake I made. Now I have renamed the files and they are at the right places. Also the code is corrected and the basic version of my app is ready.Now some more functionality and the app will be completely ready!

Friday, November 19, 2010

Think before you type!

We,all know the importance of functions in C. And any good-programmer will be using well coded functions in his program, may be including them in a loop as well.But, while using the function in a nested if else we must be careful and should keep in mind the output of the function and what exactly do we want.I am saying this because my program returned insane values due to a careless use of functions.

The problem causing bit of code is here:
if(strt < insert(strt,end,value)
 {
     //some stuff;
  }
else if(strt == insert(strt,end,value)
{
   //some stuff;
}

what happened here was that, for the 1st if statement the function insert was called. Now suppose the value is false and the other if statement is to be implemented, then it will again call the insert function which is not how it should happen because for the  new function, it will find a new value instead of the old value. So, it is better to store the value of the function and compare the stored value.
Lets hope that we don't make this mistake again. : )