scan verse scanf
BTL24
Posts: 54
I have been experimenting with scan and scanf in an attempt to understand their differences. I have read on the forums that "simpletools.h" really can reduce code and that scan is the preferred input function over scanf, as it will l save program space.
BUT... they don't seem to operate/perform the same. Case in point.. see the following code....
Upon execution, the debug monitor prompts the user to enter a text file name e.g."bozo" via the keyboard and then prints it out. The scanf input function does fine and prints out the name "bozo".
Next, the debug monitor again prompts the user to input another filename, e.g., "clown", but the scan function does not receive the new input, but instead, prints out "bozo", the previous file name. Apparently, the scan function did not receive the keyboard input and defaulted to the previously entered data.
WHY? What is wrong in my assumption that the two functions should behave correctly? Does scan not receive/store any character text?
BTW...They both appear to work OK with numeric inputs using &variable as the pointer to variable upon input.
Any advice would be greatly appreciated...
Regards,
Brian (BTL24)
BUT... they don't seem to operate/perform the same. Case in point.. see the following code....
/* input test scan1.c */ #include "simpletools.h" char servostr [15]; // Filename - Includes null char... usable space 14 characters int main(void) // main function { print("Enter filename: "); scanf("%14s", servostr); // Scan in string from debug terminal (have to hit enter) <= 14 characters print("Filename entered: %14s\n\n", servostr); print("Enter Servo filename: "); scan("%14s", servostr); // Scan in string from debug terminal (have to hit enter) <= 14 characters print("Filename entered: %14s\n", servostr); } // end main
Upon execution, the debug monitor prompts the user to enter a text file name e.g."bozo" via the keyboard and then prints it out. The scanf input function does fine and prints out the name "bozo".
Next, the debug monitor again prompts the user to input another filename, e.g., "clown", but the scan function does not receive the new input, but instead, prints out "bozo", the previous file name. Apparently, the scan function did not receive the keyboard input and defaulted to the previously entered data.
WHY? What is wrong in my assumption that the two functions should behave correctly? Does scan not receive/store any character text?
BTW...They both appear to work OK with numeric inputs using &variable as the pointer to variable upon input.
Any advice would be greatly appreciated...
Regards,
Brian (BTL24)
Comments
I still want to know the differences between scan and scanf....
Regards,
Brian (BTL24)
I'll have a closer look tomorrow.
Your Code: Try this: In Spin the name of the variable is like a label; points to the first memory location of the variable. But if I remember correctly, this is the way to do in in C.
Hope it helps.
Scanf works fine without the pointer as demostrated in my original code. The issue is that scanf uses a lot of memory so it is best to use scan instead. Scan is the function not working. I did take your advice though and did try the pointer approach with scan.... but it didn't work.
I am no longer using scanf since getStr is working fine.
Thanks for the suggestion...
Brian (BTL24)
This is not specifically a Propeller issue. The same situation applies in the AVR code and their documents mention that even though these functions have been in the tutorial material for C from the very beginning, it may be best to avoid them and just get and put characters one at a time from the input and output buffers.
In other words, if you really want to use these io features, expect some slow down in just about any microcontroller. If you desire speed, there are faster ways with a bit more coding.
Change scan("%14s", servostr); to scan("%s", servostr);
And thus another buffer overflow exploit was created.
Any way to add an extra parameter to scan( ) for a maximum length?
Use getStr() which has such protections.
As you have read above, I already switched to getStr and am happy to report all is well. Regarding buffer overflow protection...yes it works. I overran one filename by accident and getStr properly processed the char string without issue.
Regards,
Brian (BTL24)