This article is related to one of my bug fixes. When we moved from WinCE to freescales Embedded Linux, our goal was to port the same application of WinCE to Linux. Hence we started to implement linux version of WinCE OS dependent code. Device detection is one of those implementations.
One day, when working on implementation of Device Detection code in C, we wanted to read the temp file which is the output of find command. When i read the first path from the file using fgets() function and used the same buffer in fopen(), opening a file has failed...!
system("find /home/raghu > temp");
fp = fopen("temp","r");
fgets(buffer_path, 500, fp);
fgets(buffer_path, 500, fp);//this is to avoid reading of first line in temp file which is the name of the directory itself
fp2 = fopen(buffer_path, "r");
Here, fp2 was NULL. I was shocked..! path of the file present in the buffer_path is valid, but fopen was failing..! I thought how it is possible? Then I tried,
fp2 = fopen("/home/raghu/temp","r");
Then, fopen was successful...! Then, have printed buffer_path character by character using one for loop. It was printing 'next line character' (going to next line) at the end when printed using '%d'. I have made it as '%x'. Then it has printed 'a' at the end BEFORE NULL CHARACTER. Then I tested by adding a SPACE character the end of path like,
fp2 = fopen("/home/Raghu/temp ", "r");
Then fopen has failed. I confirmed that fopen will fail if any extra character is present at the end. Then I made it like,
buffer_path[strlen(buffer_path)-1]='\0';
Then it worked fine.
In windows operating system also behavior is same as in Linux. I have tested. I had a doubt regarding this in Windows. But same behavior.
When i have done this, i got more confidence in my coding skill. From that day itself, i have involved in porting the application into linux. Hence i have understood more implementations of my colleagues on linux.
Even though coding language is same, every new implementation will give us more confidence and more happyness. Thats why i have started this blog also....
Feel free to write to me regarding ur doubts and suggestions. pramoda.ma@gmail.com
I wish u best of luck...
No comments:
Post a Comment