Skip to main content
GameDev.net gamedev.net
🔒 Locked

is this code ok

Started by Psycho_68 Jul 18, 2003 at 7:24 AM 7 replies 450+ views
Original Post
Psycho_68
Psycho_68
id like to know if this code is ok or not, if its ok to return the pointer from the functon like that, it compiles and runs ok, but im not shure of it #include <stdio.h> FILE* OpenFile(char *srcFile) { FILE* pFile; if (!(pFile = fopen(srcFile,"rt"))) { pFile = NULL; cout << "error opening " << srcFile << " for read\n"; } cout << "before: " << pFile << "\n"; return pFile; } main() { FILE* pSource = OpenFile("src.txt"); cout << "after: " << &pSource << "\n"; return 0; }
VincentLascaux
VincentLascaux
It''s ok to return the pointer like that, but it''s not ok not to close the file at the end...

And since you program in C++ (you use cout...) you should use filestreams instead
Ruudje
Ruudje
don''t see anything wrong with that
Psycho_68
Psycho_68
thx for the help

I forgot the closing and iostream in my first post, is it ok now?:

#include <iostream.h>

FILE* OpenFile(char *srcFile)
{
FILE* pFile;
if (!(pFile = fopen(srcFile,"rt")))
{
pFile = NULL;
cout << "error opening " << srcFile << " for read\n";
}
cout << "before: " << pFile << "\n";
return pFile;
}

void CloseFile(FILE* pFile)
{
fclose(pFile);
}

main()
{
FILE* pSource = OpenFile("src.txt");
cout << "after: " << &pSource << "\n";
CloseFile(pSource);
return 0;
}
Trienco
Trienco
quote:
Original post by Psycho_68
thx for the help

I forgot the closing and iostream in my first post, is it ok now?:


he''s not talking about including iostream, but using filestreams. depends on if you want clean c++ code or a mixture of "old" c and c++.

f@dzhttp://festini.device-zero.de
sbennett
sbennett
What isn''t ok is using iostream.h. It isn''t part of the C++ standard. Include <iostream> instead.
AfroFire
AfroFire
What''s the downside of mixing C and C++? Just messy and annoying to follow? Or is there some underneath evil, dark, stuff, for doing something like this? I tend to be a little guilty of this :/


-AfroFire
AfroFire | Brin"The only thing that interferes with my learning is my education."-Albert Einstein
RenderTarget
RenderTarget
quote:
Original post by AfroFire
What''s the downside of mixing C and C++? Just messy and annoying to follow? Or is there some underneath evil, dark, stuff, for doing something like this? I tend to be a little guilty of this :/


-AfroFire


It''s mainly a little inconsistent. Purists will tell you to use iostream, but IMO you should use what''s best. You do have to be careful with crossing FILE* and ostream when they use the same file (like printf and cout)--they use separate buffers so if you don''t flush correctly you''ll get odd output.

Someday the C runtime library might be disabled in C++ compilers (not any time soon), so it''s beneficial to learn the C++ way. I personally haven''t used FILE* in years.

I like pie.
[sub]My spoon is too big.[/sub]
C-Junkie
C-Junkie
i''ve NEVER used FILE*. I''ve always hated it. so i went from using "to-the-metal" open read and write calls to using iostreams when i started with C++.

they''re really really nice. anybody who tells you otherwise has a specific case in mind (embedded systems) or doesn''t know what they''re saying.

but then again, games could be one of those specific cases....

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.