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:

  1. Validates the input parameters (data and file_name). If either is NULL, calls exit_error.
  2. Opens the map file in read-only mode. If the file cannot be opened, calls exit_error.
  3. Reads the first line using get_next_line(). If reading fails, calls exit_error.
  4. Iterates over each line in the file:
    • Increments data->map.height for each line read.
    • Frees the memory allocated for the line after counting it.
  5. 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);