473,327 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Segfault during a call to fopen

5
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?
Jan 17 '07 #1
5 5996
what is fname..???
have u allocated enough memory for fname..????
check once memory issues...






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?
Jan 17 '07 #2
Hi
I tried ur program.Might be u r not creating fname properly.Better new up memory for fname and do forgot to delete in end.
Where u r using strcpy ,use strncpy() func.

i tested ur program like this...It's running fine...

#include<iostream>
#include<fstream.h>
using namespace std;
void main()
{
FILE *fp ;
int exist;
char *fname=new char[30];
char *server_address=new char[4];
strncpy(server_address,"mib",4);
/* 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 , "hi hi hi hi hi\n" ) ;
fclose(fp) ;
}
printf ("\n\nThe program has to be run once again for performing the necessary calculations.\n\n") ;
return ;
}
}
~




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?
Jan 17 '07 #3
divc
5
fname is a character pointer. I've initialized it as char *fname = NULL. Now, if i use fname = malloc(sizeof(char[30])) in my program as suggested above, the program works just fine. But the issue here is that I've used other strings in the program and i've not explicitly used malloc. I've only used strdup("") during initialization. I didn't have any problems with those other variables. Y am i having a memory problem with this one ?
Jan 17 '07 #4
Hi

You have declare poniter fname and intial it also.But u haven't allocated memory for that one.Thats why it's coring.Intial pointer to null allocate memory for it also.Either using new or malloc it's up to u.
Once u will intial sufficient or appropriate memory blocks it will not going to core(as shown by me in sample program).
After that u can use strdup or strncpy.I will suggest strncpy.

fname is a character pointer. I've initialized it as char *fname = NULL. Now, if i use fname = malloc(sizeof(char[30])) in my program as suggested above, the program works just fine. But the issue here is that I've used other strings in the program and i've not explicitly used malloc. I've only used strdup("") during initialization. I didn't have any problems with those other variables. Y am i having a memory problem with this one ?
Jan 18 '07 #5
you malloc() a limited buffer using strcat(), but if the string is longer than u allocated memory for fname, the buffer will not be big enough. use strncat, and calculate the remaining space for the strncat() appropriately every time .

u will not get any problem after this.always use strncat ,it solves memory problems.
Jan 18 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: H.A. Sujith | last post by:
The following code is causing a segfault at the first if statement. Am I doing something wrong or is it a compiler bug? //---------- #include <stdio.h> int main(int argc, char *argv) { int...
10
by: name | last post by:
When I started testing the algorithms for my wrap program, I threw together this snippet of code, which works quite well. Except that it (predictably) segfaults at the end when it tries to go...
3
by: kj | last post by:
I am trying to diagnose a bug in my code, but I can't understand what's going on. I've narrowed things down to this: I have a function, say foo, whose signature looks something like: int foo(...
4
by: rabidmonkey | last post by:
Hi all, I get a seg fault on the following line of code but not every time only after the program has been running for a while and has done the routine a few times successfully... FILE *...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.