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.
Hi,
I'm currently trying to load the current screensaver's path into an NSString. Here's my code:
Code:
screenSaverFile=popen("defaults -currentHost read com.apple.screensaver modulePath","r");
char cs[1024];
fgets(cs,1024,screenSaverFile);
NSString *st=[[NSString alloc] initWithCString:cs];
NSLog(@"%@",st);

In the console "defaults -currentHost read com.apple.screensaver 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.
Anyone?
If its stopping at the first space, maybe you could do 'defaults -currentHost read com.apple.screensaver 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