fgets is only reading part of the line
Hi,
I'm currently trying to load the current screensaver's path into an NSString. Here's my code:
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?
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.
Sir, e^iπ + 1 = 0, hence God exists; reply!
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.
/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.
"When you dream, there are no rules..."
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Read a text file line by line? | jdunehew | 6 | 9,023 |
Dec 29, 2011 11:54 AM Last Post: Blacktiger |
|
| Use an integer value as part of a variable name | PHANTOMIAS | 5 | 3,346 |
Dec 8, 2009 09:24 AM Last Post: PHANTOMIAS |
|

