Why would I get the title error when executing this twice?
Code:char t[30] = {0x0}; char tt[16] = { 0 }; time_t s; time(&s); sprintf(t,"%s",ctime(&s)); strncpy(tt, t + 11, 12); tt[2] = '\0'; hours = atoi(tt);
Why would I get the title error when executing this twice?
Code:char t[30] = {0x0}; char tt[16] = { 0 }; time_t s; time(&s); sprintf(t,"%s",ctime(&s)); strncpy(tt, t + 11, 12); tt[2] = '\0'; hours = atoi(tt);
I couldn't get an error from the code provided, even executing it four times ...
It's part of a function...and it works perfectly the first time it goes through...but the second time it doesn't. Please help!! Any idea's at all.
Post more code - there's nothing in that bit of code which would cause a problem.
The real cause is usually elsewhere when it comes to malloc/free problems.
Code:void Func_Get_Image_Details() { char a[MAX_DIR_LENGTH]={0x0}; char b[30] = {0x0}; char c[16] = {0x0}; int hours = 0; int minutes = 0; int seconds = 0; FILE *in_two=popen("ls -l","r"); while(fgets(a,sizeof(a),in_two)) pclose(in_two); FILE *in_five=popen("ls -w","r"); while(fgets(temp,sizeof(temp),in_five)) pclose(in_five); time_t s; time(&s); sprintf(b,"%s",ctime(&s)); strncpy(c, b + 11, 12); c[2] = '\0'; hours = atoi(c); }
> while(fgets(a,sizeof(a),in_two))
> pclose(in_two);
So you read a line, close the stream, then try and read from it again?
How does this make any sense?
Use some braces to make your intentions explicit.
Also, there is no 'temp' in scope to validate your 2nd loop.
Also, what's with all the C99 code? That is all those mixed declarations and statements.
> strncpy(c, b + 11, 12);
Why copy 12 characters, when all you need is 2?
If you're just looking to extract time components, look at gmtime() or localtime() to split a time_t into a struct tm.
Something like this will work better
Including pclose() in your loop was obviously what caused your problem. Also, even if you kept the stream open, you were overwriting your buffer with every line.Code:#include <stdio.h> #include <string.h> /* arbitray value for testing purposes; perhaps too small * ... it would likely be better to malloc() a nice-sized buffer */ #define MAX_DIR_LENGTH 4096 void test_popen (void) { char a[MAX_DIR_LENGTH]={0x0}; char line[256]; FILE *in_two; in_two = popen ("ls -l", "r"); while (fgets (line, sizeof line, in_two)) strcat (a, line); pclose (in_two); printf ("Directory contents: %s\n", a); } int main (void) { test_popen(); test_popen(); return 0; }
Last edited by eerok; 02-05-2006 at 01:38 PM.
The above now works, however, after about 23 or so loops I get a segmentation fault. Should I be using malloc or calloc...if so can anybody give me some pointers (new to c).
Thanks..
> however, after about 23 or so loops I get a segmentation fault
Check the combined length of the output!.
> in_two = popen ("ls -l", "r");
You're doing this.
Try this on the command line
ls -l | wc -c
What number does it print out - it's the number of characters of output that command generates.
How does this compare with
#define MAX_DIR_LENGTH 4096