NewsBot
1
I did an assignment where I had to read in a bunch of bank records from a file, sort them and then display them. When starting it in debug mode in Microsoft Visual Studio, I had trouble specifying the file that it was supposed to read. Every time I specified it, it would tell me that the file could not be read, unless I ran the program from the command prompt, in which case, it found the file, but did not help me much. I spent a long time debugging my program by going back and forth from Visual Studio, a secure file transfer application and a Unix shell where I would compile my program in GCC and run it, looking for indications of what was wrong by examining the output of printf statements I had inserted into the code. Needless to say, I need to familiarize myself with gdb.
Anyway, a few bugs caused me huge headaches. One involved a file pointer, where I was passing ofp to a function where I should have passed ifp. The other involved scanf. I ended up writing a program in Visual Studio specifically to debug my usage of the format descriptors. Here is some example code where I am trying to scan what would have been a record from a file if I had not been trying to debug the thing:
char * string = "Bob Jones&12/11/1965&854367451&30082.95", * s = string;
sscanf(s, "%s %[^&]s&%2i/%2i/%4i & %i & %lf", list.first, list.last,
*************** &list.month, &list.day, &list.year,
*************** &list.accountNumber, &list.balance);
That code will properly read the first two strings and then be able to read nothing else. Here is something I wrote (which I had to modify to be able to use with file pointers) that did work:
char * string = "Bob Jones&12/11/1965&854367451&30082.95", * s = string;
sscanf(s, "%s %[^&]s&", list.first, list.last);
while (*s++ != '&');
sscanf(s, "%2i/%2i/%4i & %i & %lf",
******* &list.month, &list.day, &list.year,
******* &list.accountNumber, &list.balance);
Because of problems I was having, for the final implementation, I split the fscanf into a separate statement for each variable with additional fscanf statements to skip over characters. Even then, it was not without issues. If the string was "Bob Jones &12/11/1965&854367451&30082.95," it would store "Jones " instead of "Jones" in list.last. I modified the descriptor to be %[^& ]s, which did not make any change. I modified it to be %[^ &]s and it broke the entire thing. I eventually decided to scan it to a temporary string where it would then be rescanned to its destination.
I spent a few hours working on these issues and I do not want any recurrences of it, so I plan to do a few things:
1. Learn to use gdb
2. Figure out why my program did not find the file under debug mode, but found it without a problem when I ran it through the command prompt.
3. Write my own scanf(), sscanf() and fscanf() functions. I am sure that they will make my life easier in the future. However, I have a few questions:
More...
View All Our Microsoft Related Feeds
Anyway, a few bugs caused me huge headaches. One involved a file pointer, where I was passing ofp to a function where I should have passed ifp. The other involved scanf. I ended up writing a program in Visual Studio specifically to debug my usage of the format descriptors. Here is some example code where I am trying to scan what would have been a record from a file if I had not been trying to debug the thing:
char * string = "Bob Jones&12/11/1965&854367451&30082.95", * s = string;
sscanf(s, "%s %[^&]s&%2i/%2i/%4i & %i & %lf", list.first, list.last,
*************** &list.month, &list.day, &list.year,
*************** &list.accountNumber, &list.balance);
That code will properly read the first two strings and then be able to read nothing else. Here is something I wrote (which I had to modify to be able to use with file pointers) that did work:
char * string = "Bob Jones&12/11/1965&854367451&30082.95", * s = string;
sscanf(s, "%s %[^&]s&", list.first, list.last);
while (*s++ != '&');
sscanf(s, "%2i/%2i/%4i & %i & %lf",
******* &list.month, &list.day, &list.year,
******* &list.accountNumber, &list.balance);
Because of problems I was having, for the final implementation, I split the fscanf into a separate statement for each variable with additional fscanf statements to skip over characters. Even then, it was not without issues. If the string was "Bob Jones &12/11/1965&854367451&30082.95," it would store "Jones " instead of "Jones" in list.last. I modified the descriptor to be %[^& ]s, which did not make any change. I modified it to be %[^ &]s and it broke the entire thing. I eventually decided to scan it to a temporary string where it would then be rescanned to its destination.
I spent a few hours working on these issues and I do not want any recurrences of it, so I plan to do a few things:
1. Learn to use gdb
2. Figure out why my program did not find the file under debug mode, but found it without a problem when I ran it through the command prompt.
3. Write my own scanf(), sscanf() and fscanf() functions. I am sure that they will make my life easier in the future. However, I have a few questions:
- I have a copy of The C Programming Language, by Brian W. Kernighan and Dennis M. Ritchie. Will I need any other reference materials or should this be sufficient?
- Would robust sscanf() and fscanf() implementations be written using parse trees or could they be written by implementing cases.
- Will I be violating Brian W. Kernighan's and Dennis M. Ritchie's vision for the scanf(), sscanf() and fscanf() functions if I make whitespace matter inside square brackets?
- Is it possible to do things in such a way that I could divert all function calls to scanf(), sscanf() and fscanf() to my own custom functions without affecting the other stdio.h functions or will I have to use different function names?
- If I manage to make robust implementations of scanf(), sscanf() and fscanf(), what will be my options for giving back to the community?
More...
View All Our Microsoft Related Feeds