#### Archived

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

# why does this code crash?

## Recommended Posts

barazor    122
/* 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
RabidOcelot    122
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
barazor    122
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
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 on other sites
Maximus    124
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
BaShildy    122
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
Dean Harding    546
If you replace the first ''<'' with the string "&lt;" then it will work, observe:

#include <windows.h>

codeka.com - Just click it.