iDevGames Forums

Full Version: fgets is only reading part of the line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm currently trying to load the current screensaver's path into an NSString. Here's my code:
screenSaverFile=popen("defaults -currentHost read modulePath","r");
char cs[1024];
NSString *st=[[NSString alloc] initWithCString:cs];

In the console "defaults -currentHost read modulePath" produces "/System/Library/Screen Savers/Cosmos.slideSaver" but this code only produces "/System/Library/Screen", cutting off at the space. I can't figure out why this is happening.
If its stopping at the first space, maybe you could do 'defaults -currentHost read modulePath > tmp.txt' and read that file if this is a problem.
That's weird, I copied and pasted you code (everything but the NSString stuff, since it was quicker to just use C and compile via command line), and I got a full result:
/Library/Screen Savers/Fluid.saver

Besides, fgets is supposed to read up to a newline or when it runs out of buffer. You can try first getting rid of the NSString and using printf to make sure it's getting the C string correctly. Afterwards, you might want to try a different constructor. (perhaps try UTF8String rather than CString) If it still doesn't work, try calling printf by getting the C string (via UTF8String) from the NSString to make sure it's being stored correctly.
There's a method in NSUserDefaults to read the defaults from a specific identifier. It will then return an NSDictionary where you just get the key/values.
Reference URL's