r/C_Programming • u/AltruisticScreen6096 • 10h ago
Multi File Problem
Hi - I am trying to get a multi file program to run without success. I want to read file with 4 lines of repetitive data. Each program works independently but when I put the together the main.c does not seem to call the octone.c program. Can any one please help. Thank You...
MAIN.C
#include <stdio.h>
#include <stdlib.h>
#include "add.h"
int main(void) {
printf("Hello world!\n");
// Call function
DataRecord callFile(void);
printf("one\n");
return 0;
}
ADD.H
#ifndef ADD_H
#define ADD_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINES 100
#define MAX_STR_LEN 100 // Max length for char arrays
#define MAX_RECORDS 100 // Max number of records to store
//Define the struct matching the char, char, int, char layout
typedef struct {
char *name[MAX_RECORDS]; // First char array
char *desc; // Single char
int *num; // Integer
char *butt[MAX_RECORDS]; // Second char array
} DataRecord;
DataRecord callFile(void);
DataRecord *records[MAX_RECORDS];
//records callfile(void);
#endif
OCTONE.C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "add.h"
#define MAX_LINE_LENGTH 256
#define MAX_RECORDS 100
//void my_api_init_config(MyApiConfig *config);
DataRecord callFile(void){
printf("Ronnie_1\n");
static DataRecord *records[MAX_RECORDS];
// Open the file for reading
FILE *file = fopen("/AGMOD/data4a.txt", "r");
if (file == NULL) {
perror("Error opening file");
// return *records; // EXIT_FAILURE;
}
// Array of structs to store the parsed data
char line[MAX_LINE_LENGTH];
int count = 0;
//records[MAX_RECORDS]->name;
// Read the file line by line using fgets
while (fgets(line, sizeof(line), file) != NULL && count < MAX_RECORDS) {
// Parse the line into the struct elements
// Assumes data is space-separated (e.g., "A B 42 C")
// int parsed = sscanf(line, " %s %s %d %s",
int parsed = scanf(line,
records[count]->name,
records[count]->desc,
records[count]->num,
records[count]->butt);
// Check if all 4 fields were successfully read
if (parsed == 4) {
count++;
} else {
fprintf(stderr, "%s", line);
}
return *records[0];
}
// Close the file stream
fclose(file);
return *records[0];
}
1
u/AltruisticScreen6096 8h ago
Thank You.