fgets is only reading part of the line

Nibbie
Posts: 1
Joined: 2010.11
Post: #1
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?
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
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!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #3
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.
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #4
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..."
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Read a text file line by line? jdunehew 6 11,559 Dec 29, 2011 11:54 AM
Last Post: Blacktiger
  Use an integer value as part of a variable name PHANTOMIAS 5 3,879 Dec 8, 2009 09:24 AM
Last Post: PHANTOMIAS