-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.c
48 lines (38 loc) · 1018 Bytes
/
read.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#define MAX_FILE_SIZE 1000000 // Adjust this size based on your file's expected size
int main()
{
// Open the file for reading
FILE *file = fopen("example.txt", "r");
if (file == NULL)
{
fprintf(stderr, "Error opening file.\n");
return 1;
}
// Allocate memory for the buffer
char *buffer = (char *)malloc(MAX_FILE_SIZE);
if (buffer == NULL)
{
fprintf(stderr, "Error allocating memory.\n");
fclose(file);
return 1;
}
// Read the file content into the buffer
size_t bytesRead = fread(buffer, 1, MAX_FILE_SIZE, file);
// Close the file
fclose(file);
// Display the content
if (bytesRead > 0)
{
printf("Content read from file:\n");
printf("%.*s", (int)bytesRead, buffer); // Print the buffer up to bytesRead
}
else
{
printf("Empty file or read error.\n");
}
// Free the allocated memory
free(buffer);
return 0;
}