#### Archived

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

# why does this code crash?

This topic is 5921 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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;}

##### Share on other sites
never mind, the stupid sample from cplusplus.com seemed to make it crash, i just made the fpos_t into a pointer and took the & out of the functions...silly me

##### Share on other sites
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 on other sites
Anon. poster:
The filename after #include is missing from his post due to it being encapsulated within the arrow-head brackets which are also used for HTML tags.

##### 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 on other sites
If you replace the first ''<'' with the string "&lt;" then it will work, observe:

#include <windows.h>

codeka.com - Just click it.

##### 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