|  | Segmentation Fault at fwrite... Help |  | |
| | | vasu_7799@yahoo.com |  |
| Posted: Wed Jun 04, 2008 1:04 am Post subject: Segmentation Fault at fwrite... Help |  |
| |  | |
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 |
| |
| | | Peter Nilsson |  |
| Posted: Wed Jun 04, 2008 1:33 am Post subject: Re: Segmentation Fault at fwrite... Help |  |
| |  | |
"vasu_7...@yahoo.com" <vasu_7...@yahoo.com> wrote:
| Quote: | Hi, I have 2 structure.
|
Did you mean you have _a_ structure?
| Quote: | 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.
| Quote: | 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.
-- Peter |
| |
| | | Martin Ambuhl |  |
| Posted: Wed Jun 04, 2008 4:13 am Post subject: Re: Segmentation Fault at fwrite... Help |  |
| |  | |
vasu_7799@yahoo.com wrote:
| Quote: | 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 */ } |
| |
| | | Richard Tobin |  |
| Posted: Wed Jun 04, 2008 6:52 am Post subject: Re: Segmentation Fault at fwrite... Help |  |
In article <a3daa8bd-7fd0-476a-87ab-84de1974e314@y38g2000hsy.googlegroups.com>, vasu_7799@yahoo.com <vasu_7799@yahoo.com> wrote:
| Quote: | 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 -- In the selection of the two characters immediately succeeding the numeral 9, consideration shall be given to their replacement by the graphics 10 and 11 to facilitate the adoption of the code in the sterling monetary area. (X3.4-1963) |
| |
| | | vasu_7799@yahoo.com |  |
| Posted: Fri Jun 06, 2008 10:37 pm Post subject: Re: Segmentation Fault at fwrite... Help |  |
On Jun 4, 4:52 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
| Quote: | In article <a3daa8bd-7fd0-476a-87ab-84de1974e...@y38g2000hsy.googlegroups.com>,
vasu_7...@yahoo.com <vasu_7...@yahoo.com> wrote: 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 -- In the selection of the two characters immediately succeeding the numeral 9, consideration shall be given to their replacement by the graphics 10 and 11 to facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
|
I have got this. Actually, problem with the file permissions.
Thank you, vasu |
| |
|
|