0

I just started learning C language and I need some help with a program. Here is the code.
Questions:

  1. What is this? customerData[NUM_FIELDS][FIELD_LENGTH];
  2. Is it a char 2D array?
  3. How do you input data into the array? fgetC, putchar, getchar ?

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


#define INPUT_LENGTH 128 
#define FIELD_LENGTH 30 
#define NUM_FIELDS   9 

    int main()
    {   
       FILE *data=NULL; 
       char input[INPUT_LENGTH];
       char customerData[NUM_FIELDS][FIELD_LENGTH];

       int element=0;
       char *next;
       char ch;

       data= fopen("data.txt","r");
       if(data!=NULL)
       {
          //token=strtok(input,"|");

     /*while loop will go through line by line and stored it in an input array*/ 
          while(fgets(input,INPUT_LENGTH,data)!= NULL)
          {
             next=strtok(input,"|");
             while(next!=NULL)
             {  
                 //ch=getchar()
                 //>probably a get char for ch  
                 strcpy(next,customerData[element][strlen(next)]);
                 /*need to put the values into customer data one by one*/
                 printf("%s\n",next);
                //element+=1; 

                next=strtok(NULL,"|");

             }
    //element=0;
          } 


          printf("program is done\n");

       } 
       fclose(data);
       return 0; 
    }


  • (1) It's a 2D `char` array. (2) Yes. (3) You could do that in a billion different ways, that's way too broad to answer. – Marco Bonelli Jan 18 '20 at 00:00

1 Answers1

0

In general, "help me with my code" questions are off-topic on Stack Overflow. In order to keep the question on-topic, I'm going to focus only on the question of how to access 2D char arrays.

Yes, this is a 2D char array. Or, put another way, it's an array with NUM_FIELDS elements, where each element of the array is a char array with FIELD_LENGTH elements.

There are loads of ways to insert data into a 2D char array, but there are probably two I've encountered most often. Which one you choose to use will depend on how you want to think of this array.

Option 1: A 2D array of single chars

The first way to think about this variable is simply as a 2D array of chars - a grid of elements that you can access. Here, you can simply input values using the normal assignment operator. You'll want to make sure that your indexes are in range, or you'll start accessing invalid memory.

//Set a known element that's definitely in range
customerData[1][2] = 'A';

//Loop through all the elements
for(int ii = 0; ii < NUM_FIELDS; ii++)
{
    for (int jj = 0; jj < FIELD_LENGTH; jj++)
    {
        customerData[i][j] = 'B';
    }
}

//Set an element from variables
char nextInput = getNextCharFromInput();

if(x < NUM_FIELD && y < FIELD_LENGTH)
{
    customerData[x][y] = nextInput;
}

//Bad. This could corrupt memory
customerData[100][60] = 'X';

//Risky without check. How do you know x and y are in range?
cusomterData[x][y] = 'X';

You could certainly write your code by assigning these elements on character at a time. However, the broader context of your program heavily implies to me that the next option is better.

Option 2: A 1D array of fixed-length strings

In C, a "string" is simply an array of chars. So another way to look at this variable (and the one that makes the most sense for this program) is to treat it as a 1D array of length NUM_FIELDS, where each element is a string of length FIELD_LENGTH.

Looking at this this way, you can start using the C string functions to input data into the array, rather than needing to deal character by character. As before, you still need to be careful of lengths so that you don't go off the end of the strings.

Also be aware that all array decay into pointers, so char* is also a string (just of unknown length).

//Set a specific field to a known string, which is short enough to fit
strcpy(customerData[2], "date");

//Loop through all fields and wipe their data
for(int ii = 0; ii < NUM_FIELDS; ii++)
{
    memset(customerData[ii], 0, FIELD_LENGTH);
}

//Set field based on variables
if(x < NUM_FIELDS)
{
    //Will truncate next if it is too long
    strncpy(customerData[x], next, FIELD_LENGTH);

    //Will not input anything if field is too long
    if(strlen(next) < FIELD_LENGTH)
    {
        strcpy(customerData[x], next);
    }
}

//Bad. Could corrupt memory
strcpy(customerData[100], "date");
strcpy(customerData[1], "this string is definitely much longer than FIELD_LENGTH");

//Risky. Without a check, how do you know either variable in in range?
strcpy(customerData[x], next);

getchar and fgetC both deal with reading characters, from stdout and a file respectively, so can't be used to put data into a variable. putchar does deal with put character into things, but only stdout, so can't be used here.

Korosia
  • 593
  • 1
  • 5
  • 19
  • I got it now. strcpy the word to the specific location customerData[element] is enough as long as the [field length] has enough size. Still, I'll look into your explanation. – Adrian Ong Zhen Yang Jan 19 '20 at 20:28