get_lines - 1Fr3aK2/Cub3d GitHub Wiki
📝 get_lines
Reads all lines from a map file and counts the total number of lines, storing the result in the map structure.
⚙️ Parameters
| Parameter | Type | Description |
|---|---|---|
data |
t_data * |
Pointer to the main data structure where the map information will be stored. Must be non-NULL. |
file_name |
char * |
Path to the map file to be read. Must be a valid string and the file must exist. |
🔁 Returns
| Return value | Description |
|---|---|
void |
The function does not return a value. On failure, it calls exit_error and terminates the program. |
📖 Description
The get_lines function performs the following steps:
- Validates the input parameters (
dataandfile_name). If either isNULL, callsexit_error. - Opens the map file in read-only mode. If the file cannot be opened, calls
exit_error. - Reads the first line using
get_next_line(). If reading fails, callsexit_error. - Iterates over each line in the file:
- Increments
data->map.heightfor each line read. - Frees the memory allocated for the line after counting it.
- Increments
- Closes the file descriptor after all lines have been read.
This function ensures that the map structure has an accurate line count (height) and prepares the program to process the map data.
💡 Example Usage
t_data data;
get_lines(&data, "map.cub");
printf("The map has %d lines.\n", data.map.height);