xampp ubuntu
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file_pointer;
char file_content[1000];
file_pointer = fopen("/opt/lampp/htdocs/example.txt", "r");
if (file_pointer == NULL) {
printf("File cannot be opened or does not exist.\n");
return 1;
}
fgets(file_content, 1000, file_pointer);
printf("Content of the file:\n%s", file_content);
fclose(file_pointer);
return 0;
}
Explanation:
#include <stdio.h>
and#include <stdlib.h>
: These lines include necessary header files for input/output operations and standard library functions in C.int main() {...}
: This is the main function where the execution of the program begins.FILE *file_pointer;
: Declares a file pointer variable namedfile_pointer
used to handle file operations.char file_content[1000];
: Declares a character array namedfile_content
to store the content read from the file. It has a size of 1000 characters.file_pointer = fopen("/opt/lampp/htdocs/example.txt", "r");
: Opens a file named "example.txt" located at "/opt/lampp/htdocs/" in read-only mode. It assigns the file pointer tofile_pointer
.if (file_pointer == NULL) { ... }
: Checks if the file opening was successful. If the file pointer isNULL
, it means the file could not be opened, and it prints an error message.fgets(file_content, 1000, file_pointer);
: Reads a line from the file usingfgets()
function and stores it infile_content
. It reads at most 999 characters from the file pointed byfile_pointer
.printf("Content of the file:\n%s", file_content);
: Prints the content of the file to the console.fclose(file_pointer);
: Closes the file associated withfile_pointer
.return 0;
: Indicates successful execution of the program by returning 0 to the operating system.