Andy,

 

Please disregard my first message from yesterday. As I'm sure you
already know, what is meant by the reference is that for modes "r" and
"r+", the file must already exist, or the function will return a null
pointer (not seg fault).

 

I ran the following test case (basically just the code you provided) on
both x86 and DaVinci-Arm9 and it worked as anticipated without any seg
faults. I'm using redhat enterprise linux 4 and montavista tools v.4.01
(from the DVEVM/SDK v.1.10 release), and I compiled without any flags,
i.e.:

# gcc main.c

(x86)

# arm_v5t_le-gcc  main.c

(arm 9)

 

I suppose you could run this test case to localize your problem. If the
test case works, perhaps there is something else in your code causing
the seg fault, i.e. a rogue pointer or stack overflow or something else
that would present itself at a random time. If this test case seg faults
for you, then that would seem to suggest that there is a problem in your
setup. 

 

Test case:

------------------------------------------------------------------------
----

#include <stdio.h>

 

int main(void)

{

    FILE *file;

 

    if ((file = fopen("mydata", "r")) == NULL) {

        if (file = fopen("mydata", "w+")) == NULL)

            printf("Unable to create configuration file\n");

        else

            fprintf(file, "(initialization goes here)\n");

    }

 

    fclose(file);

 

    return 0;

}

------------------------------------------------------------------------
----

Anyway, sorry I couldn't be of more help, but this at least confirms
that there is nothing wrong with the code snippet you sent and the
problem is apparently coming in from somewhere else.

 

Steve

________________________________

From: Preissig, Steve 
Sent: Saturday, December 02, 2006 5:02 PM
To: [email protected]
Subject: fopen segmentation fault

 

Andy,

 

According to my O'Reilly reference, under fopen:

For modes "r" and "r+", the file must already exist.

 

I'm surprised that this causes a seg fault, but apparently you are using
the function out of spec.  You may have to open in append mode and then
do an fread from the file to see if it is empty or contains data. My
reference mentions nothing about the file needing to exist to be opened
in append mode, so I assume that if the file doesn't exit, append mode
will create a new one.

Regards,
Steve Preissig

Texas Instruments
Technical Training Organization
9260 Waits Ferry Crossing
Duluth, GA  30097 
[EMAIL PROTECTED] 
(770) 840-0783 

 

_______________________________________________
Davinci-linux-open-source mailing list
[email protected]
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source

Reply via email to