Jump to content
  • Advertisement


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


fopen in vc++ 7 question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi: The following code reads a txt file but the name passed in is a "bb.dat." The code works and runs under vc++ 6. The code can be compiled under vc++7 but crashes during runtime. I have spent hours to try to find out what the problem is. Can anyone help me please. Thanks bool Readfile( char* filename){ int length = StringLength(filename); filename[length-3]=''t''; filename[length-2]=''x''; filename[length-1]=''t''; FILE * pFile; pFile = fopen (filename,"r"); if (pFile == NULL) return false; }

Share this post

Link to post
Share on other sites
How come you didn''t include what the debugger says? Why didn''t you tell us what line it is crashing on?

You are modifying the string that is passed in (which is usually a bad idea). Where does the string coming from?
Does the code that calls ReadFile look like this: "ReadFile( (char *)name );"?
Are you sure the length of the string is always >= 3.

Share this post

Link to post
Share on other sites
Thanks for your reply guys. The name that is passed into the function is always something like "abc.dat" I just want to change the extension to txt so I can read another file with the same name.

The filename passed in is from the Win32 API open dialog function. Is there a way to just get the filename without the extension like get the name of "abc" without the .dat extension?

[edited by - JoeZ on March 2, 2004 4:02:28 AM]

Share this post

Link to post
Share on other sites

bool ReadFile(const char *File)
std::string str = File;
int Position;
Position = str.find(".");
str = str.substr(0, Position);

FILE *f = fopen(str.c_str(), "r");
// etc....


-Earth is 98% full. Please delete anybody you can.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!