Archived

This topic is now archived and is closed to further replies.

barazor

why does this code crash?

Recommended Posts

/* fsetpos example */
#include 

main ()
{
  FILE * pFile;
  fpos_t position;

  pFile = fopen ("myfile.txt","w");
  fgetpos (pFile, &position);
  fputs ("That is a sample",pFile);
  fsetpos (pFile, &position);
  fputs ("This",pFile);
  fclose (pFile);
  return 0;
}
 
why does this code like to crash my program? i''m compiling it on devcpp(mingw), and ive tracked the bug down to being fgetpos(). anybody have an idea why this is happening? do i need a new version of the libs or what? thx in advance

Share this post


Link to post
Share on other sites
The only think I can think of after looking at your code (unless I missed something obvious, which I wouldn''t doubt...) is that you have a bad file pointer.

If you haven''t already, try making sure pFile points to a valid open file after you assign it, like so:


pFile = fopen ("myfile.txt", "w");
if (pFile == 0L)
{
printf ("Unable to open \"myfile.txt\"\n");
return 0;
}


Game: The Adventure (tm).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Excuse me but don''t you need to put a filename
after the #include ? This has ofcourse nothing to do
with your program crashing , since it obviously compiles.
(Unless that is a typo, in your post.)

Share this post


Link to post
Share on other sites
When you post brackets on this board they don''t show up. So any compiler include libraries won''t show up, but you will see the #include. If they include local files with quotes, you can see them.

Code comment of the week:
// loop of faith

Share this post


Link to post
Share on other sites
Another thing that can catch me out sometimes: Paths in Win32 are seperated with ''\'', but you have to put ''\\'' in a filename string, because ''\'' is an escape character. That wouldn''t cause the crash directly, but it''s a good reason for the file not to open, even if it looks like it''s there.

"c:\xyzzy\plugh\myfile.txt" becomes "c:xyzzyplughmyfile.txt".

All your bases belong to us

Share this post


Link to post
Share on other sites