Menu
Lumberyard
Developer Guide (Version 1.11)

Reading Files with CryPak

Now you can write some code to read the information from the ExampleText.txt file that you created.

  1. Type the following, which contains the if-else statement that frames the code. The ReadFromExampleFile() function will read the contents of the file and return true if it succeeds, and false if not.

    Copy
    char* fileContent = NULL; if (!ReadFromExampleFile(&fileContent)) { CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "ReadFromExampleFile() failed"); } else { CryLogAlways("ExampleText contains %s", fileContent); [...] // this line will be added later on   }

    If ReadFromExampleFile() is successful in reading ExampleText.txt, fileContent will be the space in memory that contains the text that it read.

  2. Type the following, which stubs out the ReadFromExampleFile() function.

    Copy
    bool ReadFromExampleFile(char** fileContent) { CCryFile file; size_t fileSize = 0; const char* filename = "examples/exampletext.txt"; [...] }
    • file of type CCryFile can make use of CryPak to access files directly from the file system or from inside a .pak archive.

    • fileSize - Defines the end of the message. In this case, reading does not end by detecting the null character '\0'.

    • filename - Specifies the path of the file to be loaded and is case-insensitive.

  3. Type the following, which uses CryPak to search the file.

    Copy
    char str[1024]; if (!file.Open(filename, "r")) { sprintf(str, "Can't open file, (%s)", filename); CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "%s", str); return false; }
    • Open() invokes CryPak to search the file specified by filename.

    • File access mode "r" specifies that a plain text file is going to be read. To read a binary file, use "rb" instead.

  4. Type the following, which gets the length of the file. If the file is not empty, it the allocates the memory required as indicated by the file length. It then reads the file content. It aborts if the size of the content is not equal to the file length.

    Copy
    fileSize = file.GetLength(); if (fileSize <= 0) { sprintf(str, "File is empty, (%s)", filename); CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "%s", str); return false; } char* content = new char[fileSize + 1]; content[fileSize] = '\0'; if (file.ReadRaw(content, fileSize) != fileSize) { delete[] content; sprintf(str, "Can't read file, (%s)", filename); CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "%s", str); return false; }
    • content is the local pointer to a char array in memory which gets initialized by the length returned by GetLength() and an extra null character.

    • ReadRaw fills content with the information read from the text file. In case of a failure, the allocated memory of content is freed.

  5. Type the following, which closes the file handle and sets the fileContent pointer so that the locally created data can be used outside the function. Finally, it returns true since the reading was successful.

    Copy
    file.Close(); *fileContent = content; return true;

    Note

    In the example, the caller of ReadFromExampleFile() is responsible for freeing the heap memory which has been allocated to store the data from the text file. Thus, after the data has been used, be sure to add the call delete[] fileContent;.

  6. To check if the reading was successful, run the game and check the Game.log file.

Complete example code (file reading)

Calling ReadFromExampleFile()

Copy
char* fileContent = NULL; if (!ReadFromExampleFile(&fileContent)) {  CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "ReadFromExampleFile() failed"); }  else { CryLogAlways("ExampleText contains %s", fileContent); delete[] fileContent; }

ReadFromExampleFile() implementation

Copy
bool ReadFromExampleFile(char** fileContent) { CCryFile file; size_t fileSize = 0; const char* filename = "examples/exampletext.txt"; char str[1024]; if (!file.Open(filename, "r")) { sprintf(str, "Can't open file, (%s)", filename); CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "%s", str); return false; } fileSize = file.GetLength(); if (fileSize <= 0) { sprintf(str, "File is empty, (%s)", filename); CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "%s", str); return false; } char* content = new char[fileSize + 1]; content[fileSize] = '\0'; if (file.ReadRaw(content, fileSize) != fileSize) { delete[] content; sprintf(str, "Can't read file, (%s)", filename); CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "%s", str); return false; } file.Close();  *fileContent = content; return true;  }