Sign in to follow this  
Toadhead

a couple of c++ questions..

Recommended Posts

Hey, I have some questions and I hope somebody can asnwer them to me. I learned C++ with a very big book, and made alot of examples etc. But while reading a tutorial of SDL ( http://cone3d.gamedev.net/cgi-bin/index.pl?page=tutorials/gfxsdl/tut3 ), I noticed some things were I've never heared of before. I tried to look on the internet and used google but I coult find this one: - FILE *fp; I always used ifstream and ofstream to open files etc, but now I saw this one. I know fp (filepointer) is just a pointer to a file, but is this just old C style of is my book just stupid? And btw what header files do I need to include to use this filepointer? - if((fp=fopen(filename, "r")) == NULL) eh... yeah.. what does it do? fopen will open the file "filename", (filename includes the full path to the file), but what does the "r" do??????? If the file could not be opened fp will be NULL so the if statement wont do his job. If the file could be opened the filepointer (fp) will point to this file right? - fgets(buffer, 255, fp); Yes, my next problem. fgets... what does that one do? I know 'gets' will read in one character, but never heared of fgets before. And what are the parameters? - while(!feof(fp) && count<mNumframes) I know what eof is, it returns NULL as soon as the end of the file has been reached right? But were is foef for?? Thanks, Rob [Edited by - Toadhead on November 29, 2004 12:46:31 PM]

Share this post


Link to post
Share on other sites
"r" specifies that fopen should only open the file if it exists (it shouldn't be created if it doesn't exist).

fgets gets a string from a file stream (fp) into a char buffer with the specified length (255 chars in your case).

feof tests if the position in the file has reached the end of the file.

Here might be a useful link:
http://www.cplusplus.com/ref/cstdio/index.html

FILE:

"File processing is traditionally performed using the FILE class. In the strict C sense, FILE is a structure and it is defined in the stdio.h header file." - http://functionx.com/cpp/articles/cfileprocessing.htm

\Jimmy H

Share this post


Link to post
Share on other sites
These are Standard C Library functions. The Standard C++ Library includes the whole of the Standard C Libraray, as well as the Standard Template Library, so they are perfectly valid C++. The header files are slightly changed, though. Where the C header was name.h, the corresponding C++ header is cname, no .h.

The functions you are looking at all deal with file input and output, which is why they all have an f prefix: fopen, fread, fwrite, fseek, fget, fput, feof, fclose and so forth.

The "r" parameter to fopen actually specifies to open the file as read only. If the file does not exist, fopen will not attempt to create it. In order to write to a file, you need to specify the "w" mode parameter. There are a few others that are relevant on various systems, such as "a" for append and "b" for binary. They can be used in a variety of combinations as well.

C I/O often relied on format and mode specifiers given as strings, which is error prone. Consequently, C++ moved to eliminate many of these by creating typesafe streams (eliminating format specifiers because the insertion and extraction operators know the operand type) and status bits and enumerations (ios::failbit, ios::read, etc).

Share this post


Link to post
Share on other sites
Quote:
Original post by Eriond
How come it's named FILE (all capitals) when everything else in C and C++ is lower case with underscores?
Because it's not a function or a macro, it's a type (struct). That's why we have WPARAM, FAR (now obsolete) and other all-caps type and convention identifiers in Win16, Win32 and other C-based APIs/libraries.

Share this post


Link to post
Share on other sites
a thanks but whats the difference btween eof and foef?
Isn't eof already searing in a file, for thev end of it.
I mean how are you gonna use eof if you dont have a file to look at???

Share this post


Link to post
Share on other sites
You should use the Cone3D SDL tutorials for what they're made for: Learning SDL. If you don't think what they're doing is necessary, then don't do it. I personally dislike how they do their sprites. Loading a whole animation into individual bitmap surfaces is a horrible way of doing it. A better way would be to load in a sprite sheet and then tile through each one as needed.

Share this post


Link to post
Share on other sites
yes, but I just want to know whats foef does, I learned what eof does and I want to know whats the difference between them :(

Ofcourse I can skip it but what if I get this one later?


Can anyone tell me the exact difference between eof and feof?

Share this post


Link to post
Share on other sites
ow..
EOF was the one I used, you was able to do it like this: while (variable != EOF) {..}

But I also saw this in a tut: if ( cin.eof() ) break;
So eof() also exists..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this