can't read sequential files

View: New views
12 Messages — Rating Filter:   Alert me  

can't read sequential files

by zirtik :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, I'm using cygwin and windows XP together with Eclipse IDE and CDT. I have a following piece of code:

....

        int i;
        fp = fopen ("phi.txt","r");
       
        for( i = 0; i < 51; i++ ) {
                fscanf(fp, "%d\n", &original_phi[i]);
        }

...

and when I try to compile it, it compiles well, but when I try to run it I get the following error message:

655 [main] Genetics 3012 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
2540 [main] Genetics 3012 open_stackdumpfile: Dumping stack trace to Genetics.exe.stackdump

I added the following line to check if fp is always NULL:

if (fp==NULL)
{
   printf("error, NULL pointer!\n");
   return(1);
}

and it is always NULL. I put the "phi.txt" in the same directory as the executable so it is in:

....\ProjectFolder\Debug  

folder as well as in

....\ProjectFolder\src

folder. I still keep getting the same error. I added the current directory "." to the PATH but it didn't help. This code used to run on another PC with Eclipse and cygwin again but this is the first time I am having problems with it on a different machine. Any help would be greatly appreciated.

Thanks.



Re: can't read sequential files

by jim marshall :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

zirtik wrote:

> Hi, I'm using cygwin and windows XP together with Eclipse IDE and CDT. I have
> a following piece of code:
>
> ....
>
> int i;
> fp = fopen ("phi.txt","r");
>
> for( i = 0; i < 51; i++ ) {
> fscanf(fp, "%d\n", &original_phi[i]);
> }
>
> ...
>
> and when I try to compile it, it compiles well, but when I try to run it I
> get the following error message:
>
> 655 [main] Genetics 3012 _cygtls::handle_exceptions: Exception:
> STATUS_ACCESS_VIOLATION
> 2540 [main] Genetics 3012 open_stackdumpfile: Dumping stack trace to
> Genetics.exe.stackdump
>
> I added the following line to check if fp is always NULL:
>
> if (fp==NULL)
> {
>    printf("error, NULL pointer!\n");
>    return(1);
> }
>
> and it is always NULL. I put the "phi.txt" in the same directory as the
> executable so it is in:
>
> ....\ProjectFolder\Debug  
>
> folder as well as in
>
> ....\ProjectFolder\src
>
> folder. I still keep getting the same error. I added the current directory
> "." to the PATH but it didn't help. This code used to run on another PC with
> Eclipse and cygwin again but this is the first time I am having problems
> with it on a different machine. Any help would be greatly appreciated.
>
> Thanks.
>
>
>
Maybe you don't have read access to the file? You could also check errno
(see perror, or strerror functions) to see what the error is.


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Re: can't read sequential files

by Brian Dessent :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

zirtik wrote:

> and when I try to compile it, it compiles well, but when I try to run it I
> get the following error message:
>
> 655 [main] Genetics 3012 _cygtls::handle_exceptions: Exception:
> STATUS_ACCESS_VIOLATION
> 2540 [main] Genetics 3012 open_stackdumpfile: Dumping stack trace to
> Genetics.exe.stackdump

There's a bug in your program that causes a segfault.  We can't know
what it is because you only posted a snippet.  We need a fully
standalone (compilable) testcase that exibits the problem.  Otherwise,
how are we supposed to know what you're doing?

Brian

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Re: can't read sequential files

by Alberto Luaces :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Maybe unrelated, but:

shouldn't this

> fp = fopen ("phi.txt","r");

be

fp = fopen ("phi.txt","rt");

?

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


RE: can't read sequential files

by Dave Korn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 31 October 2007 00:11, zirtik wrote:

> int i;
> fp = fopen ("phi.txt","r");
>
> for( i = 0; i < 51; i++ ) {
> fscanf(fp, "%d\n", &original_phi[i]);
> }

> 655 [main] Genetics 3012 _cygtls::handle_exceptions: Exception:
> STATUS_ACCESS_VIOLATION
> 2540 [main] Genetics 3012 open_stackdumpfile: Dumping stack trace to
> Genetics.exe.stackdump

> if (fp==NULL)
> {
>    printf("error, NULL pointer!\n");
>    return(1);
> }
>
> and it is always NULL.

  You should also print out the value of 'errno' here, it's the global
variable that holds the code for the last error from C library functions.

> I put the "phi.txt" in the same directory as the
> executable so it is in:
>
> ....\ProjectFolder\Debug
>
> folder as well as in
>
> ....\ProjectFolder\src
>
> folder. I still keep getting the same error.

  It would tell us a lot if we knew whether errno was equal to ENOENT or
EACCES.

> I added the current directory
> "." to the PATH but it didn't help.

  Nope, it wouldn't make a difference.  PATH is only used by the shell when
searching for commands; it isn't used when you're calling fopen.  For fopen,
you must provide the exact path to the file, or the file must be in the cwd
(current directory) when your program is running.

> This code used to run on another PC with
> Eclipse and cygwin again but this is the first time I am having problems
> with it on a different machine. Any help would be greatly appreciated.

  I don't use eclipse myself, but I'd guess there's a setting somewhere for
what the current directory should be when it launches the executable.  Make
sure your text file is in that directory, or change the setting so it points
at the debug/release folder where you already put the text file.

    cheers,
      DaveK
--
Can't think of a witty .sigline today....


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Re: can't read sequential files

by Larry Hall (Cygwin) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Alberto Luaces wrote:

> Maybe unrelated, but:
>
> shouldn't this
>
>> fp = fopen ("phi.txt","r");
>
> be
>
> fp = fopen ("phi.txt","rt");
>
> ?


It's not required, no.


--
Larry Hall                              http://www.rfk.com
RFK Partners, Inc.                      (508) 893-9779 - RFK Office
216 Dalton Rd.                          (508) 893-9889 - FAX
Holliston, MA 01746

_____________________________________________________________________

A: Yes.
 > Q: Are you sure?
 >> A: Because it reverses the logical flow of conversation.
 >>> Q: Why is top posting annoying in email?

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Re: can't read sequential files

by zirtik :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

After adding the line:


        if (fp==NULL)
        {
           printf("error, NULL pointer!\n");
           return(1);
        }

and then rebuilding the code, everything worked. But it's strange that if I delete that code segment and never check whether the fp pointer is NULL or not, I always get a segmentation fault. Can this be some kind of an optimization problem? I don't know why it happens. Thank you for the comments.

zirtik wrote:
Hi, I'm using cygwin and windows XP together with Eclipse IDE and CDT. I have a following piece of code:

....

        int i;
        fp = fopen ("phi.txt","r");
       
        for( i = 0; i < 51; i++ ) {
                fscanf(fp, "%d\n", &original_phi[i]);
        }

...

and when I try to compile it, it compiles well, but when I try to run it I get the following error message:

655 [main] Genetics 3012 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
2540 [main] Genetics 3012 open_stackdumpfile: Dumping stack trace to Genetics.exe.stackdump

I added the following line to check if fp is always NULL:

if (fp==NULL)
{
   printf("error, NULL pointer!\n");
   return(1);
}

and it is always NULL. I put the "phi.txt" in the same directory as the executable so it is in:

....\ProjectFolder\Debug  

folder as well as in

....\ProjectFolder\src

folder. I still keep getting the same error. I added the current directory "." to the PATH but it didn't help. This code used to run on another PC with Eclipse and cygwin again but this is the first time I am having problems with it on a different machine. Any help would be greatly appreciated.

Thanks.


RE: can't read sequential files

by Dave Korn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 01 November 2007 06:43, zirtik wrote:

> After adding the line:
>
>
> if (fp==NULL)
> {
>   printf("error, NULL pointer!\n");
>   return(1);
> }
>
> and then rebuilding the code, everything worked. But it's strange that if I
> delete that code segment and never check whether the fp pointer is NULL or
> not, I always get a segmentation fault. Can this be some kind of an
> optimization problem? I don't know why it happens. Thank you for the
> comments.

  A NULL pointer is never valid in C, and a segfault is what you get if you
try to make use of one (by dereferencing it).  You get a NULL pointer back
from fopen when it fails; a lot of library routines do this to indicate
failure, because any other value could be a valid pointer.  The other library
routines, such as fread and fwrite, will assume that you have done your error
checking and won't be passing them a NULL pointer, so they won't bother to
check what file pointer you pass them, they'll just go ahead and try and use
it.  So if you get a NULL pointer back from fopen and you don't check for it,
your code carries on and passes that same pointer to fread, which tries to use
it as if it pointed to a real FILE object, and crashes.

  The comp.lang.c FAQ has an entire section on NULL pointer, section 5.  It
should be available at
http://c-faq.com/null/index.html
but the website seems to be temporarily down right now; there's also a copy at
faqs.org:
http://www.faqs.org/faqs/C-faq/faq/


    cheers,
      DaveK
--
Can't think of a witty .sigline today....


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Re: can't read sequential files

by Lewis Hyatt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dave Korn wrote:

> On 01 November 2007 06:43, zirtik wrote:
>
>> After adding the line:
>>
>>
>> if (fp==NULL)
>> {
>>   printf("error, NULL pointer!\n");
>>   return(1);
>> }
>>
>> and then rebuilding the code, everything worked. But it's strange that if I
>> delete that code segment and never check whether the fp pointer is NULL or
>> not, I always get a segmentation fault. Can this be some kind of an
>> optimization problem? I don't know why it happens. Thank you for the
>> comments.
>
>   A NULL pointer is never valid in C, and a segfault is what you get if you
> try to make use of one (by dereferencing it).  You get a NULL pointer back
> from fopen when it fails; a lot of library routines do this to indicate
> failure, because any other value could be a valid pointer.  The other library
> routines, such as fread and fwrite, will assume that you have done your error
> checking and won't be passing them a NULL pointer, so they won't bother to
> check what file pointer you pass them, they'll just go ahead and try and use
> it.  So if you get a NULL pointer back from fopen and you don't check for it,
> your code carries on and passes that same pointer to fread, which tries to use
> it as if it pointed to a real FILE object, and crashes.
>
>   The comp.lang.c FAQ has an entire section on NULL pointer, section 5.  It
> should be available at
> http://c-faq.com/null/index.html
> but the website seems to be temporarily down right now; there's also a copy at
> faqs.org:
> http://www.faqs.org/faqs/C-faq/faq/

I think what the OP is saying is that if he adds the check for null,
then his code works normally, including the file read operation, (ie,
the pointer is not null), but if he removes the check, then he gets a
segfault. Strange behavior like this is typically the result of memory
corruption from earlier in the program. If you can post your entire code
(preferably a minimal example that exhibits the behavior), then we can
probably locate the problem.

-Lewis


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


RE: can't read sequential files

by Dave Korn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 01 November 2007 15:15, Lewis Hyatt wrote:


>>> if (fp==NULL)
>>> {
>>>   printf("error, NULL pointer!\n");
>>>   return(1);
>>> }

> I think what the OP is saying is that if he adds the check for null,
> then his code works normally, including the file read operation, (ie,
> the pointer is not null), but if he removes the check, then he gets a
> segfault.


  Please observe that the "check for NULL" also includes a return statement
that bypasses the rest of the code .... including in particular the file read
operation.
 




    cheers,
      DaveK
--
Can't think of a witty .sigline today....


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


RE: can't read sequential files

by Dave Korn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 01 November 2007 15:58, Dave Korn wrote:

> On 01 November 2007 15:15, Lewis Hyatt wrote:
>
>
>>>> if (fp==NULL)
>>>> {
>>>>   printf("error, NULL pointer!\n");
>>>>   return(1);
>>>> }
>
>> I think what the OP is saying is that if he adds the check for null,
>> then his code works normally, including the file read operation, (ie,
>> the pointer is not null), but if he removes the check, then he gets a
>> segfault.
>
>
>   Please observe that the "check for NULL" also includes a return statement
> that bypasses the rest of the code .... including in particular the file
> read operation.

  Hang on, I misread you, my eye skipped over the bit where you suggest that
adding the check somehow makes the preceding fopen call succeed instead of
fail.  However I still don't think that's what the OP was saying, unless the
subject line of this thread is terribly wrong, I think you just read a bit too
much into OP's phrase "everything worked"; I think that just means "program
ran to completion /without/ a segfault".

    cheers,
      DaveK
--
Can't think of a witty .sigline today....


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Re: can't read sequential files

by Lewis Hyatt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>   Hang on, I misread you, my eye skipped over the bit where you suggest that
> adding the check somehow makes the preceding fopen call succeed instead of
> fail.  However I still don't think that's what the OP was saying, unless the
> subject line of this thread is terribly wrong, I think you just read a bit too
> much into OP's phrase "everything worked"; I think that just means "program
> ran to completion /without/ a segfault".


It was more than that:

 >and then rebuilding the code, everything worked. But it's strange that
 >if I delete that code segment and never check whether the fp pointer is
 >NULL or not, I always get a segmentation fault. Can this be some kind
 >of an optimization problem? I don't know why it happens. Thank you for
 >the comments.

Especially when he suggested it was a problem with the compiler
optimizer, I am pretty sure that he means the code is working as
expected with this check in there, and he doesn't understand why. (The
subject line applies from back before he tried this.)

But anyway you could be right too, I guess there's no point in guessing
what he meant...

-Lewis


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/