Printf/scanf replacement methods: some feedback/advice needed

孤街醉人 提交于 2019-12-11 01:47:57

问题


I've recently been writing some basic command-line programs (I want to keep my skills sharp over the summer), but printf and scanf have been starting to annoy me. I'm not a wonderful C programmer, and having to get into printf/scanf and their instabilities (or even worse, fgets and their ilk) isn't exactly putting me in a comforting setting (for this reason exactly, I love NSLog, with its comforting default namespace and its automatic NSString and NSObject parsing).

Much to my disappointment, though, NSLog doesn't have a counterpart function, and prints a lot of extra 'junk' (time, function name, etc., along with a newline at the end), which defeats a lot of the purpose in my using it. So I decided to sit down for a different kind of programming exercise and write functions to replace printf and scanf that would meet my needs.

And voila, I came up with my own NSInput.h file, containing two functions: NSPrint(), and NSScan(). These two functions are modeled much after printf and scanf, but also handle NSString's. I know I'm treading on sacred namespace here, but I couldn't resist (IFPrint and IFScan just sound terrible!).

Now, while I'm really happy that I have working code (for which you can find the source here), I know that it's not efficient (much to my surprise, though, NSPrint is several times more efficient than printf under LLDB in Xcode 4, but that's beside the point). I need some advice on how to make the functions better, and more efficient. NSScan, for example, converts the va_list it recieves into an NSPointerArray, and uses NSScanner's to scan through the format and input strings, so I know there's a lot of room for improvement.

Basically, what I want to know is, are there any glaring mistakes I made that could and should be fixed? Is there anything huge that I missed? Should I just be called spoiled and go back to using printf and scanf? Please tell me, I'm looking for input here (pun not intended!)...

Thanks in advance!


回答1:


My thoughts:

  • Don't call them NSxxxxx, NS is reserved for Cocoa and Foundation.
  • Both functions should be modified to accept a FILE* i.e. you should be modelling the interface to fprintf() and fscanf() for more flexibility.
  • Your printf function would probably be better if you used fputs()

e.g.

void NSFPrint (FILE* fp, NSString *format, ...) 
{
    // Create the variable argument list.
    va_list args;
    va_start(args, format);

    // Using NSString, parse the argument list and convert it to a C string.
    fputs([[[[NSString alloc] initWithFormat:format arguments:args] autorelease] UTF8String], fp);
     va_end(args);
}
  • Consider adding support for input and output in encodings other than UTF-8.
  • Your scanf replacement mixes C buffered IO and Unix unbuffered IO on stdin. This might be bad.
  • Your scanf replacement reads up to the end of the line even when it doesn't need to. I haven't checked carefully, but if the scan format does not consume the entire line, it looks like you are discarding input. This might be bad.


来源:https://stackoverflow.com/questions/3436129/printf-scanf-replacement-methods-some-feedback-advice-needed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!