C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 20th, 2006, 05:23 AM
3saul 3saul is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Posts: 9 3saul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 54 m 28 sec
Reputation Power: 0
What's wrong with my func - popen fgets...

Using Linux/C - can't get the following to work...beginner please help

void Func_Get_Image()
{

int Current_Image_Num = 1;
char Var_Get_Image[MAX_DIR_LENGTH] = { 0x0 };
sprintf( Var_Get_Image, "find $HOME/Pictures -iregex '.*\.\(gif\|jpg\|jpeg\|bmp\|png\||tif\||tiff\||svg\)' | sed %d'q;d'", Current_Image_Num);
FILE *in=popen("Var_Get_Image","r");
char tmp[MAX_DIR_LENGTH]={0x0};
while(fgets(tmp,sizeof(tmp),in))
printf("%s",tmp);

pclose(in);
}

This compiles...but Var_Get_Image is being sent as the command rather than the contents of Var_Get_Image (find $HOME/Pictures -iregex '.*\.\(gif\|jpg\|jpeg\|bmp\|png\||tif\||tiff\||svg\)' | sed %d'q;d')

Reply With Quote
  #2  
Old January 20th, 2006, 06:12 AM
ZuK ZuK is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2005
Posts: 580 ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 13 h 22 m 26 sec
Reputation Power: 96
Quote:
Originally Posted by 3saul
This compiles...but Var_Get_Image is being sent as the command rather than the contents of Var_Get_Image

Code:
FILE *in=popen(Var_Get_Image,"r");

would be the right popen command.
But there is another problem with your code. You have to escape the backslashes in the format-string.
Something like that
Code:
sprintf( Var_Get_Image, "find $HOME/Pictures -iregex '.*\\.\\(gif\\|jpg\\|jpeg\\|bmp\\|png\\||tif\\||tiff\\||svg\\)' | sed %d'q;d'", Current_Image_Num);

Kurt

Reply With Quote
  #3  
Old January 20th, 2006, 06:24 AM
3saul 3saul is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Posts: 9 3saul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 54 m 28 sec
Reputation Power: 0
ok I just change to the code you suggested but still get the same error (sh: cannot find command Var_Get_Image)..

I've tried both
FILE *in=popen(Var_Get_Image,"r");
and
FILE *in=popen("Var_Get_Image","r");
with the same results...this seems very strange...your help is very much appreciated.

Reply With Quote
  #4  
Old January 20th, 2006, 07:15 AM
ZuK ZuK is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2005
Posts: 580 ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 13 h 22 m 26 sec
Reputation Power: 96
Code:
sprintf( Var_Get_Image, "find $HOME/Pictures -iregex '.*\\.\\(gif\\|jpg\\|jpeg\\|bmp\\|png\\||tif\\||tiff\\||svg\\)' | sed %d'q;d'", Current_Image_Num);

with
Code:
FILE *in=popen(Var_Get_Image,"r");

Works fine for me. But the find command seems not to be correct
Kurt

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > What's wrong with my func - popen fgets...

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap