Home > Blockchain >  I can't get this function for creating strings from file
I can't get this function for creating strings from file

Time:04-26

#include <stdio.h>

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

#include <math.h>
#include <time.h>
#include <assert.h>

#define MAX_SIZE 100

#define ODIT "log_file.txt"
#define INVENTORY_N "inventory_names.txt"
#define INVENTORY_A "inventory_amount.txt"
#define CURR_DIR "main-2dynamic"

static int inventory_amount_array[MAX_SIZE];
static char inventory_names_array[MAX_SIZE][MAX_SIZE];

void print_avail_opt(){
    printf("1) Read from the log file\n");
    printf("2) Save the current inventory in the log file\n");
    printf("3) Print current inventory item amounts\n");
    printf("4) Print current inventory item names\n");
    printf("5) Exit\n");

}

void get_inventory_int(){
    int i,n;
    char *token;
    char help[256];
    FILE *InputFile;
    InputFile = fopen(INVENTORY_A, "r");
    fscanf(InputFile, "%s", help);
    token = strtok(help, ",");
    i = 0;
    while(token != NULL){
        inventory_amount_array[i] = atoi(token);
        token = strtok(NULL, ",");
        i  ;
    }
    n = i;
}

void get_inventory_string(){
    int i,n;
    char *token;
    char help[256];
    FILE *InputFile;
    InputFile = fopen(INVENTORY_A, "r");
    fscanf(InputFile, "%s", help);
    token = strtok(help, ",");
    i = 0;
    while(token != NULL){
        inventory_names_array[i][i] = token;
        token = strtok(NULL, ",");
        i  ;
    }
    n = i;
}

void print_inventory_int(){
    int n = 10;
    get_inventory_int();
    for (int i = 0; i<n; i  ){
        if( inventory_amount_array[i] == '\0'){
            break;
        }
        else{
            printf("Available stock from item:%d\n",inventory_amount_array[i]);
        }
        
    }
}

void print_inventory_string(){
    int n = 10;
    get_inventory_string();
    for (int i = 0; i<n; i  ){
        if(inventory_names_array[i][i] == '\0'){
            break;
        }
        else{
            printf("Available stock from %s\n",inventory_names_array[i]);
        }
    }
}

void f_print_inventory_int(FILE * log_file){
    int n;
    get_inventory_int();
    for (int i = 0; i<n; i  ){
        if( inventory_amount_array[i] == '\0'){
            break;
        }
        else{
            fprintf(log_file,"%d ",inventory_amount_array[i]);
        }
        
    }
}

int read_from_file(FILE * log_file){
    log_file = fopen(ODIT, "r");
    char str[MAX_SIZE]; 

    while(fscanf(log_file,"%s", str)!=EOF){
        printf("%s", str);
    }
 
    fclose(log_file);

}

int write_in_file(FILE * log_file, int tm_year, int tm_mon, int tm_mday,int tm_hour, int tm_min, int tm_sec){
    log_file = fopen(ODIT, "a");
    

    fprintf(log_file,"\nInventory at d.d.d - d:d:d\n", tm_mday, tm_mon, tm_year, tm_hour, tm_min,tm_sec);
    f_print_inventory_int(log_file);
    
    fclose(log_file);

}

int restock (){

}

int main(){

    time_t T = time(NULL);
    struct tm tm = *localtime(&T);

    FILE * log_file;

    bool loop = true;
    while (loop == true){

        print_avail_opt();

        printf("Enter op:");
        int op = 0;scanf("%d", &op);

        switch (op){
        case 1: read_from_file(log_file);
            break;
        case 2: write_in_file(log_file, tm.tm_year   1900, tm.tm_mon   1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
            break;
        case 3: print_inventory_int();
            break;
        case 4: print_inventory_string();
            break;
        case 5: loop = false;
            break;
        default: printf("Not correct option\n");
            break;
        }
    }

    return 0;
}

I want to make case 4 in the main switch to work properly

The things that are not working are the way of creating strings arrays and printing it out - everything else works fine.

PS: And i would really appreciate a working functions, because i have it for homework at school and it's due tomorrow.

Note from my compiler (gcc):

main.c: In function ‘get_inventory_string’:
main.c:57:37: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
   57 |         inventory_names_array[i][i] = token;
      |    

CodePudding user response:

Just as the compiler warning said, your error is here:

inventory_names_array[i][i] = token;

inventory_names_array[i][i] is a char; token is a pointer. You need to dereference the pointer (note: this will clear this error, but you have other problems in your code, too).

CodePudding user response:

The compiler is warning you that you are assigning a char* pointer to a single char value in this statement:

inventory_names_array[i][i] = token;

That is not what you want or need. You need to copy the characters pointed at by token into the array. You can use strncpy() for that:

strncpy(inventory_names_array[i], token, MAX_SIZE);

That being said, there are other problems with your code:

  • not checking fopen() for failure.
  • not closing opened files with fclose().
  • not preventing buffer overflows.
  • your get functions are not storing n anywhere that your print function can reach, so you end up looping through the arrays using incorrect counts. You should have the get functions return the actual array counts to the caller.
  • read_from_file() and write_in_file() both take in a FILE* parameter that they don't use. Also, they are declared as returning an int, but they don't actually return anything.

With that said, try something more like this instead:

#include <stdio.h>

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

#include <math.h>
#include <time.h>
#include <assert.h>

#define MAX_SIZE 100

#define ODIT "log_file.txt"
#define INVENTORY_N "inventory_names.txt"
#define INVENTORY_A "inventory_amount.txt"
#define CURR_DIR "main-2dynamic"

static int inventory_amount_array[MAX_SIZE];
static char inventory_names_array[MAX_SIZE][MAX_SIZE];

void print_avail_opt(){
    printf("1) Read from the log file\n");
    printf("2) Save the current inventory in the log file\n");
    printf("3) Print current inventory item amounts\n");
    printf("4) Print current inventory item names\n");
    printf("5) Exit\n")
}

int get_inventory_int(){
    int i = 0;
    char help[256];
    FILE *InputFile = fopen(INVENTORY_A, "r");
    if (InputFile == NULL) return 0;
    fscanf(InputFile, "%5s", help);
    fclose(InputFile);
    char *token = strtok(help, ",");
    while (token != NULL && i < MAX_SIZE){
        inventory_amount_array[i] = atoi(token);
        token = strtok(NULL, ",");
          i;
    }
    return i;
}

int get_inventory_string(){
    int i = 0;
    char help[256];
    FILE *InputFile = fopen(INVENTORY_A, "r");
    if (InputFile == NULL) return 0;
    fscanf(InputFile, "%5s", help);
    fclose(InputFile);
    char *token = strtok(help, ",");
    while (token != NULL && i < MAX_SIZE){
        strncpy(inventory_names_array[i], token, MAX_SIZE);
        token = strtok(NULL, ",");
          i;
    }
    return i;
}

void print_inventory_int(){
    int n = get_inventory_int();
    for (int i = 0; i < n;   i){
        if (inventory_amount_array[i] == 0){
            break;
        }
        printf("Available stock from item: %d\n", inventory_amount_array[i]);
    }
}

void print_inventory_string(){
    int n = get_inventory_string();
    for (int i = 0; i < n;   i){
        if (inventory_names_array[i][i] == '\0'){
            break;
        }
        printf("Available stock from %s\n", inventory_names_array[i]);
    }
}

void f_print_inventory_int(FILE * log_file){
    int n = get_inventory_int();
    for (int i = 0; i < n;   i){
        if (inventory_amount_array[i] != 0){
            fprintf(log_file, "%d ", inventory_amount_array[i]);
        }
    }
}

void read_from_file(){
    FILE *log_file = fopen(ODIT, "r");
    if (log_file == NULL) return;

    char str[MAX_SIZE];
    while (fscanf(log_file, "           
  •  Tags:  
  • c
  • Related