\n "t"<\/p>\n "b"<\/p>\n<\/td>\n<\/tr>\n<\/table>\n \u201ct\u201d stands for text mode. In text mode, each byte of the file will be treated as its own character. You will gener\u00adally use this format if the file was or needs to be human-readable.<\/p>\n \u201cb\u201d is for binary. A file opened in binary mode will be treated as raw data and is generally not human-readable.<\/p>\n <\/p>\n<\/p>\n <\/p>\n This will open the file file. bin for reading only, in binary mode. This is the most common mode used when loading and using 3D models within your games for several reasons. First, most model files are in binary form (but not all of them; the next chapter contains a file format that is text-based). Second, you rarely need to write to a model file when working with it in your game. Generally, you only need to load and use, rather than modify, the data it contains. Don\u2019t forget to determine whether your file pointer is valid (not zero) before you try to use it.<\/p>\n Now that you have an open file, you need to be able to retrieve data from it. When working with model files, I like to read in the whole file as a chunk of unformatted data, and then sort it out later. To do this<\/p>\n you use fread().<\/p>\n fread is used for just what it sounds like: file reading. This function reads raw, unformatted data into an array. This is a quick and easy way to get data from the file into memory. The fopen function takes four param\u00adeters, as follows:<\/p>\n ■ The first parameter is a pointer to a buffer in which to store the new data.<\/p>\n ■ The second and third parameters will tell fread how much data you want. These parameters contain the number of bytes in a single \u201citem,\u201d or chunk, of data, and the number of items you would like to read, respectively. Be sure your buffer is large enough to hold all of your data; nothing is worse than overwrit\u00ading an array and losing data.<\/p>\n <\/p>\n ■ The fourth and final parameter is a file pointer. The parameter should contain the file pointer that you created when you opened the file using fopen. You can check fread to make sure it read everything. The fread function returns the number of items actually read. If this return value and the third parameter of fread are equal, all the data was read successfully.<\/p>\n Here is a snippet of code that would read 100 bytes of unformatted data from one of the files you opened previously.<\/p>\n byte Buffer[100]; fread(Buffer, 1, 100, f);<\/p>\n This will store 100 bytes of data (one byte per item * 100 items) read from the file that f points to, in Buffer, an array of 100 bytes.<\/p>\n Just as fread is used for reading unformatted data from a file, fwrite is used for writing unformatted data. The parameters for fwrite are exactly the same as fread. The first parameter is the buffer containing the data to write to the file, the second and third parameters hold the sizes of the data to be written, and the fourth is a file pointer so fwrite knows which file to output the data to. Here is code to take the data you just read in (now stored in Buffer) and write it back to the file.<\/p>\n fwrite(Buffer, 1, 100, f);<\/p>\n Sometimes you need to read or write formatted data to a file. In these cases, fread and fwrite will not work. You will need to look toward functions such as fscanf and fprintf for formatted input and output.<\/p>\n These functions are used just like their text counterparts scanf\/sscanf and printf\/sprintf. Other functions are in place to read or write single variables to files such as fputc\/fgetc (single characters) and fputs\/fgets (strings).<\/p>\n All of this is very useful, but what if you need to write or read data to or from other parts of the file? You simply use the fseek() function. fseek will move your file pointer to a new place in the file. The fseek func\u00adtion takes three variables. The first parameter takes the file pointer you want to manipulate. The second argument takes the number of bytes you want to move, relative to the origin. The origin point is given by the third parameter. The origin can be one of three constant values:<\/p>\n ■ SEEK_BEGIN places the origin at the beginning of the file. In this case, fseek will move the specified number of bytes from the start of the file.<\/p>\n<\/p>\n ■ The final constant for the origin is SEEK_SET. SEEK_SET will tell fseek to move the specified number of bytes from the current location in the file. After fseek is called, you may return to reading and writing your file. This time, the file is coming from or going to a new location.<\/p>\n When you are done reading or writing from a file, you need to close it. Failing to close a file leaves some memory allocated, thus creating a memory leak in your program. To close a file you opened with fopen, you use fclose. fclose() takes a single parameter, the file pointer of the file you want to close. You can also use the _fcloseall() to close all open files; it takes no parameters at all and returns the number of files it successfully closed. Here, you see the simple code necessary to close the file f once you are done working with it.<\/p>\n fclose(f);<\/p>\n Well, there you have it. A short introduction to file I\/O using FILE *. This should be plenty to get you started loading your own 3D models. If you want to know more, or would like to learn about other methods of file I\/O, I would suggest picking up a book on C or C++. Any good C\/C++ book will contain a section pertaining to file I\/O, whether covering FILE * or another method.<\/p>\n With that out of the way, and a review of file I\/O complete, you are ready to move onto the hard part\u2014deciphering the MD2 format.<\/p>\n","protected":false},"excerpt":{"rendered":" One of the most important parts of model loading is just getting the file into memory. One of the ways to do this is to use a set of functions from the standard C library, collectively known as the FILE * functions. You have probably heard of them, perhaps even used them before. This section […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/posts\/68"}],"collection":[{"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":0,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"wp:attachment":[{"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
|