Hi all,
I've written a C program. I need to write some data into files. I've written some code and I'm getting a segmentation fault during a call to the fopen function. I've given part of the code here.
FILE *fp ;
/* ---
---
Some code here
---
--- */
/* Check for existence of file */
fname = strdup("Host");
strcat (fname , server_address) ;
strcat (fname , ".txt") ;
exist = access(fname , F_OK) ;
/*If it doesn't exist, create a new one */
if (exist == -1)
{
fp = fopen(fname , "w") ;
if (fp == NULL)
{
printf ("\nCannot open file\n") ;
return ;
}
else
{
fprintf (fp , "%s %s %s %s %s\n" , inf , ib , ob , ie , oe) ;
fclose(fp) ;
}
printf ("\n\nThe program has to be run once again for performing the necessary calculations.\n \n") ;
return ;
}
/* ---
---
Program continued
----
----
*/
As u can see, i've checked if fopen returns null. I can't figure out why this is happening. Can anyone help me?
I've written a C program. I need to write some data into files. I've written some code and I'm getting a segmentation fault during a call to the fopen function. I've given part of the code here.
FILE *fp ;
/* ---
---
Some code here
---
--- */
/* Check for existence of file */
fname = strdup("Host");
strcat (fname , server_address) ;
strcat (fname , ".txt") ;
exist = access(fname , F_OK) ;
/*If it doesn't exist, create a new one */
if (exist == -1)
{
fp = fopen(fname , "w") ;
if (fp == NULL)
{
printf ("\nCannot open file\n") ;
return ;
}
else
{
fprintf (fp , "%s %s %s %s %s\n" , inf , ib , ob , ie , oe) ;
fclose(fp) ;
}
printf ("\n\nThe program has to be run once again for performing the necessary calculations.\n \n") ;
return ;
}
/* ---
---
Program continued
----
----
*/
As u can see, i've checked if fopen returns null. I can't figure out why this is happening. Can anyone help me?
Comment