0% found this document useful (0 votes)
0 views

C_arrays

Uploaded by

SQL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

C_arrays

Uploaded by

SQL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h>

#define MAX_LINES 100 // Maximum number of lines to read


#define MAX_LENGTH 100 // Maximum length of each line

int main() {
FILE *file;
char line[MAX_LENGTH];
char *lines[MAX_LINES];
int num_lines = 0;

// Open the file


file = fopen("example.txt", "r");
if (file == NULL) {
fprintf(stderr, "Error opening file.\n");
return 1;
}

// Read lines into array of strings


while (fgets(line, sizeof(line), file)) {
// Allocate memory for the line
lines[num_lines] = malloc(strlen(line) + 1);
if (lines[num_lines] == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
return 1;
}

// Copy the line into the allocated memory


strcpy(lines[num_lines], line);

// Increment the line counter


num_lines++;

// Ensure we don't exceed maximum lines


if (num_lines >= MAX_LINES) {
fprintf(stderr, "Exceeded maximum number of lines.\n");
break;
}
}

// Close the file


fclose(file);

// Now 'lines' array contains each line as a separate string


// Print them for demonstration
printf("Contents of file:\n");
for (int i = 0; i < num_lines; i++) {
printf("%s", lines[i]);
free(lines[i]); // Free allocated memory for each line
}

return 0;
}

# With proper memory handling


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINES 100 // Maximum number of lines to read


#define MAX_LENGTH 100 // Maximum length of each line

int main() {
FILE *file;
char line[MAX_LENGTH];
char *lines[MAX_LINES];
int num_lines = 0;

// Open the file


file = fopen("example.txt", "r");
if (file == NULL) {
fprintf(stderr, "Error opening file.\n");
return 1;
}

// Read lines into array of strings


while (fgets(line, sizeof(line), file)) {
// Allocate memory for the line
lines[num_lines] = malloc(strlen(line) + 1);
if (lines[num_lines] == NULL) {
fprintf(stderr, "Memory allocation failed.\n");

// Clean up allocated memory before exiting


for (int i = 0; i < num_lines; i++) {
free(lines[i]);
}
fclose(file);
return 1;
}

// Copy the line into the allocated memory


strcpy(lines[num_lines], line);

// Increment the line counter


num_lines++;

// Ensure we don't exceed maximum lines


if (num_lines >= MAX_LINES) {
fprintf(stderr, "Exceeded maximum number of lines.\n");
break;
}
}

// Close the file


fclose(file);

// Now 'lines' array contains each line as a separate string


// Print them for demonstration
printf("Contents of file:\n");
for (int i = 0; i < num_lines; i++) {
printf("%s", lines[i]);
free(lines[i]); // Free allocated memory for each line
}

return 0;
}

# With bidimensional array

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define INITIAL_CAPACITY 10

int main() {
char **lines = NULL;
int num_lines = 0;
int capacity = INITIAL_CAPACITY;

// Allocate initial memory for array of string pointers


lines = malloc(capacity * sizeof(char *));
if (lines == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
return 1;
}

// Example: Adding strings to the array


lines[num_lines] = strdup("First line");
num_lines++;

lines[num_lines] = strdup("Second line");


num_lines++;

// Example: Printing the stored strings


for (int i = 0; i < num_lines; i++) {
printf("%s\n", lines[i]);
}

// Free allocated memory


for (int i = 0; i < num_lines; i++) {
free(lines[i]);
}
free(lines);

return 0;
}

You might also like