Segmentation Fault at fwrite... Help

V

vasu_7799

Hi,
I have 2 structure.

struct a
{
char cn[14+1];
char tl[5+1];
char roup[3+1];
char rc[6+1];
char adj[13+1];
char qty[13+1];
char cr[1];
} abrec;


EXEC SQL BEGIN DECLARE SECTION;
struct a_data
{
char cn[14];
char tl[5];
char roup[3];
char rc[6];
char adj[13];
char qty[13];
} abdata;
EXEC SQL END DECLARE SECTION;

There is a function getting data from database and write into the
file. A few lines from this function,

::::::
memcpy ( abrec.adj, abdata.adj, 13);
memcpy ( abrec.cr, "\n", 1);

Then, I have

if ( fwrite ( &abrec, sizeof(abrec), 1, fp1 ) != 1 )
{
fprintf ( stderr, "ERROR: Could not write cob_adjd_rec!
\n" );
fflush(stderr);
exit(-1);
}

Here, fwrite is giving me segmentation fault. More details (using
debugger)

(dbx) run
Running: pm
(process id 11523)
t@1 (l@1) signal SEGV (no mapping at the fault address) in fwrite at
0xfe613e88
0xfe613e88: fwrite+0x0018: ld [%i3 + 12], %o1
Current function is write_ub92_cob_adjd_rec
2986 if ( fwrite ( &abrec, sizeof(abrec), 1, fp1 ) != 1 )
(dbx)

Can anyone help me with this.....???? Please

Thank you
vasu
 
P

Peter Nilsson

Hi,
   I have 2 structure.

Did you mean you have _a_ structure?
struct a
{
    char    cn[14+1];
    char    tl[5+1];
    char    roup[3+1];
    char    rc[6+1];
    char    adj[13+1];
    char    qty[13+1];
    char    cr[1];
} abrec;

EXEC SQL BEGIN DECLARE SECTION;
     struct a_data
     {
        char    cn[14];
        char    tl[5];
        char    roup[3];
        char    rc[6];
        char    adj[13];
        char    qty[13];
     } abdata;
EXEC SQL END DECLARE SECTION;

Please don't post implementation specific extensions.
If your question isn't about the C language, take it
to a group that specialises in the extensions you're
using.
There is a function getting data from database and
write into the file. A few lines from this function,

   memcpy ( abrec.adj, abdata.adj, 13);
   memcpy ( abrec.cr, "\n", 1);

Then, I have

   if ( fwrite ( &abrec, sizeof(abrec), 1, fp1 ) != 1 )
    {
       fprintf ( stderr, "ERROR:  Could not write cob_adjd_rec!
\n" );
       fflush(stderr);
       exit(-1);
    }

Here, fwrite is giving me segmentation fault.

Please post a compilable snippet that exhibits the problem.
The most obvious guess at the moment is that fp1 is not
a pointer to a valid file stream.
 
M

Martin Ambuhl

Hi,
I have 2 structure.
[etc.]
It is impossible to diagnose your problem without a minimal, compilable
program that reproduces your problem. Below I have attempted to turn
your disconnected fragments into a C program. This code does not,
however, exhibit the behavior you claim, so whatever you are doing wrong
does not survive in my attempt to turn your fragments into a program.
So try again, but with real code this time. And the SQL junk is not C
unless you include the definitions for those identifiers.

/* mha: code cleaned up to produce a C program */

#include <string.h> /* mha: added for memcpy */
#include <stdio.h> /* mha: added for fwrite, fprintf,
fflush, and the added fopen and
fclose */
#include <stdlib.h> /* mha: added for exit */

/* mha: introduced main in order to have a compilable program */
int main(void)
{
struct a
{
char cn[14 + 1];
char tl[5 + 1];
char roup[3 + 1];
char rc[6 + 1];
char adj[13 + 1];
char qty[13 + 1];
char cr[1];
} abrec;

/* mha: use of undefined identifiers EXEC, SQL, BEGIN, DECLARE, and
SECTION removed */

struct a_data
{
char cn[14];
char tl[5];
char roup[3];
char rc[6];
char adj[13];
char qty[13];
} abdata;


/* mha: non-C random text removed */
FILE *fp1; /* mha: added everything to ... */
if (!(fp1 = fopen("/dev/nul", "wb"))) {
fprintf(stderr,
"could not open \"/dev/nul\" in \"wb\" mode.\n");
exit(EXIT_FAILURE);
}
memcpy(abdata.adj, "1234567890123", 13);
/* ... here */

memcpy(abrec.adj, abdata.adj, 13);
memcpy(abrec.cr, "\n", 1);


if (fwrite(&abrec, sizeof(abrec), 1, fp1) != 1) {
fprintf(stderr, "ERROR: Could not write cob_adjd_rec!\n");
fflush(stderr);
fclose(fp1); /* mha:added */
exit(EXIT_FAILURE); /* mha: replaced non-standard argument
for exit() */
}

fclose(fp1); /* mha:added */
return 0; /* mha: added */
}
 
R

Richard Tobin

t@1 (l@1) signal SEGV (no mapping at the fault address) in fwrite at
0xfe613e88
0xfe613e88: fwrite+0x0018: ld [%i3 + 12], %o1
Current function is write_ub92_cob_adjd_rec
2986 if ( fwrite ( &abrec, sizeof(abrec), 1, fp1 ) != 1 )

fp1 is probably not an open file. Did you check the return value of
fopen()?

-- Richard
 
V

vasu_7799

t@1 (l@1) signal SEGV (no mapping at the fault address) in fwrite at
0xfe613e88
0xfe613e88: fwrite+0x0018:      ld       [%i3 + 12], %o1
Current function is write_ub92_cob_adjd_rec
2986       if ( fwrite ( &abrec, sizeof(abrec), 1, fp1 ) != 1 )

fp1 is probably not an open file.  Did you check the return value of
fopen()?

-- Richard

I have got this. Actually, problem with the file permissions.

Thank you,
vasu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top