Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a 4 column text file into two char arrays in C?

C - How to read a 4 column file into two string arrays

I am trying to read in a file of unknown length which has the following format:

2082018 1200 79 Meeting
3082018 1300 60 Lunch
4082018 1400 30 Dinner
5082018 0600 90 Work

I am trying to get the first column inside an array KEY_arr and the rest of the columns inside VAL_arr such that :

KEY_Arr[0] = "2082018" and VAL_arr[0] = " 1200 79 Meeting"

I tried two methods, both unsuccessful. In the first method I tried to read in the file using getc in a 2D array:

int main(int argc, char** argv) {
    FILE *inp_cal;
    int word_count = 0;
    char c;
    char arr[50][50];
    int char_count = 0;
    int space_cnt = 0;
    char line[100];
    int line_count = 0;
    inp_cal = fopen("calendar_out.txt", "r");

            if (inp_cal) {
            printf("Processing file...\n");
            while ((c = fgetc(inp_cal)) != EOF) {
                if ((c == ' ' || c == '\n') && space_cnt == 0) {
                    arr[word_count][char_count] = '\0'; //Terminate the string
                    char_count = 0; //Reset the counter.
                    word_count++;
                    space_cnt = 1;
                } else {
                    if ((c == ' ' || c == '\n') && space_cnt == 1) {
                        word_count++;
                        char_count = 0; 
                    }

                    arr[word_count][char_count] = c;
                    printf("%c", arr[word_count][char_count]);

                    if (char_count < 50) {
                        char_count++;
                    } else {
                        char_count = 0;
                    }
                }
            }
        } else {
            printf("no file found.\n");
        }
    printf("\nword count: %d, char count: %d\n\n", word_count, char_count);
    for (int i = 0; i < word_count; i++) {
        printf("%s\n", arr[i]);
    }



    return (EXIT_SUCCESS);
}

This yields the following output:

2082018
1200
 79
 Meeting

3082018
 1300
 60
 Lunch

4082018
 1400
 30
 Dinner

5082018
 0600
 90
 Work

I am then stuck when it comes to separating into separate strings for the key and value pairs.

The second method I tried was using fgets, but I was unable to get any correct output:

if (inp_cal) {
    printf("Processing file...\n");
    while (fgets(line, sizeof line, inp_cal)) {
        char KEY_arr[10];
        char VAL_arr[50];
        char KEY;
        char VAL;

        if (sscanf(line, "%29s%29s", KEY, VAL) != 2) { /* error, bad line */
            printf("Bad line format\n");
        } else {
            KEY_arr[line_count] = KEY;
            VAL_arr[line_count] = VAL;
            printf("%c, %c\n", KEY_arr[line_count], VAL_arr[line_count]);
        }
        line_count++;
    }
} else {
    printf("no file found.\n");
}

Any suggestions on how I can improve one of the above methods to read in the 4 column text file and store in two char arrays?

like image 354
rrz0 Avatar asked Dec 01 '25 13:12

rrz0


1 Answers

Use fscanf(), since you know the format of the data, and extract every token in a line. Then, the first token is your key, and the other three are meant to be together, so concatenate them with snprintf().

Example:

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

#define N 10 // max number of lines to be read
#define VALLEN 100
int main(void)
{
  int key_arr[N], a, count = 0;
  char val_arr[N][VALLEN], b[10], c[10], d[10];
  FILE* fp;
  fp = fopen("input.txt", "r");
  char bb[33];
  while (fscanf(fp, "%d %s %s %s", &a, b, c, d) != EOF) {
    if(count < N) {
      key_arr[count] = a;
      snprintf(val_arr[count++], VALLEN, "%s %s %s", b, c, d);
    }
  }
  for(int i = 0; i <count; ++i)
    printf("key=%d, val = %s\n", key_arr[i], val_arr[i]);
  fclose(fp);
  return 0;
}

Output:

key=2082018, val = 1200 79 Meeting
key=3082018, val = 1300 60 Lunch
key=4082018, val = 1400 30 Dinner
key=5082018, val = 0600 90 Work
like image 84
gsamaras Avatar answered Dec 04 '25 02:12

gsamaras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!