Back to C++
2026-03-205 min read

file name and line information (C++)

Learn file name and line information (C++) step by step with clear examples and exercises.

Why This Matters

Understanding file name and line information in C++ is essential for several reasons. It empowers developers to efficiently debug their code, manage errors, and optimize applications effectively. By mastering the use of file names and line numbers, you can:

  • Quickly identify and rectify errors during development
  • Enhance code readability with logging functionality
  • Implement robust error handling mechanisms for reliable software
  • Simplify troubleshooting when dealing with intricate issues

Prerequisites

To fully comprehend the concept of file name and line information in C++, you should have a strong foundation in:

  • Basic C++ syntax and programming concepts (variables, data types, operators, control structures)
  • Familiarity with text files and their structure
  • Understanding of C++ compilers and standard libraries
  • Knowledge of exception handling in C++

Core Concept

The Standard Library in C++ offers predefined functions that help developers work with file names and line numbers. These functions enable navigating code, reading/writing files, and error handling more efficiently.

File Name

__FILE__ is a built-in macro that returns the name of the current source file being compiled. It can be utilized within any source file to obtain its own name as a string literal.

#include <iostream>
int main() {
std::cout << __FILE__ << std::endl;
return 0;
}

When executing this code, it outputs the name of the current source file.

Line Number

__LINE__ is another built-in macro that returns the line number where it appears within the current source file being compiled. It can be used to print the line number during runtime.

#include <iostream>
int main() {
std::cout << __LINE__ << std::endl;
return 0;
}

When executing this code, it outputs the current line number where the std::cout statement is located.

Combining File Name and Line Number

To combine file name and line number, you can use a combination of both macros:

#include <iostream>
int main() {
std::cout << "Current file: " << __FILE__ << ", Current line: " << __LINE__ << std::endl;
return 0;
}

This code prints the name of the current source file and its corresponding line number.

Worked Example

Let's consider a simple example where we want to log the filename, line number, function name, and error message when an exception occurs in our C++ program:

#include <iostream>
#include <string>
#include <exception>

void myFunction() {
throw std::runtime_error("Error occurred in myFunction");
}

int main() {
try {
myFunction();
} catch (const std::exception& e) {
std::cerr << "File: " << __FILE__ << ", Line: " << __LINE__
<< ", Function: " << __FUNCTION__ << ", Error: " << e.what() << std::endl;
}
return 0;
}

In this example, we define a function myFunction that throws an exception. In the main function, we catch the exception and print the error message along with the file name, line number, function name using the __FILE__, __LINE__, and __FUNCTION__ macros.

Common Mistakes

  1. Not using __FILE__ or __LINE__ in the correct context: These macros should be used within the source code to obtain the current file name and line number. They cannot be used as function arguments or variables.
  2. Confusing __FILE__ with a string literal: __FILE__ returns a character array containing the name of the current source file, not a string literal. Be careful when concatenating it with other strings to avoid potential issues.
  3. Not handling exceptions properly: When using exceptions, make sure to catch the correct exception type and provide proper error handling in your code.
  4. Using __FILE__ and __LINE__ outside of a function body: These macros only work within the scope of a function; if used outside, they will return undefined behavior.
  5. Incorrectly assuming that __FILE__ includes the directory path: __FILE__ returns only the filename without the directory path, so you may need to use additional functions or techniques to obtain the full file path.

Subheadings under Common Mistakes:

  • Misuse of macros in function arguments or variables
  • Ignoring exception types and proper error handling
  • Using __FILE__ and __LINE__ outside of function bodies
  • Assuming __FILE__ includes directory paths

Practice Questions

  1. Write a C++ program that logs the file name, line number, function name, and error message when an arithmetic exception occurs during runtime.
  2. Modify the previous example to log the current time along with the file name, line number, function name, and error message when an exception is thrown.
  3. Create a custom exception class named CustomException that includes the file name, line number, and error message as members. Override the what() function to return this information.
  4. Write a program that reads a text file line by line and prints the filename, line number, and content of each line.
  5. Implement a logging system in your C++ application that logs errors, warnings, and informational messages with file name, line number, and message details.

FAQ

Q: Can I use __FILE__ and __LINE__ in header files?

A: Yes, you can use them in both source files and header files. However, keep in mind that they will return the name of the file where they are being used, not necessarily the file including the header.

Q: Is there a way to obtain the line number for preprocessor directives like #include or #define?

A: No, __LINE__ only provides the line number for source code lines that are processed after the preprocessor stage. Preprocessor directives are not included in this count.

Q: What happens if __FILE__ and __LINE__ macros are used outside of a function body?

A: If these macros are used outside of a function body, they will return undefined behavior, as they are only defined within the scope of a function.

Q: Can I use __FILE__ and __LINE__ to find the location of an error in a third-party library or system code?

A: While it's possible to use these macros to gather information about errors occurring in third-party libraries, you should consult their documentation first, as they might provide their own error reporting mechanisms.

file name and line information (C++) | C++ | XQA Learn