1

I am trying to put textfile to the struct

have issues with assigning values form text file to the struct, it keeps printing me the last value being printed incompletely.

Can you kindly help me out with the issue?

Thank you in advance.

#include <stdio.h> #include <stdlib.h> #define MAX_LEN 422498 #define MAX_KEY 128 struct record { char* a; char* b; }; int main(int argc, char **argv){ char* input=argv[1]; char* output=argv[2]; char* buff[MAX_LEN]; char *delimiter = ";"; //printf("%s\n",input); //printf("%s\n",output); FILE *fp; fp = fopen(input, "r"); //opening file*/ if( fp == NULL ) { perror("Error while opening the file.\n"); exit(EXIT_FAILURE); } int rowsNum=0; struct record* recArr = malloc(3 * sizeof (struct record)); //struct record recArr[4]; //recArr=malloc(1 * 2*sizeof(char*)); /*recArr[0].a="aa"; recArr[0].b="bb"; recArr[1].a="cc"; recArr[1].b="dd"; recArr[2].a="ee"; recArr[2].b="ff"; printf("%s\n", recArr[0].a); printf("%s\n\n", recArr[0].b); printf("%s\n", recArr[1].a); printf("%s\n\n", recArr[1].b); printf("%s\n", recArr[2].a); printf("%s\n\n", recArr[2].b);*/ while (fgets(buff, MAX_LEN, (FILE*)fp)!=NULL) { //recArr=realloc(recArr, (rowsNum+1) * 2*sizeof(char*)); //char* Key = strtok(buff, delimiter); //char* Value = strtok(NULL, delimiter); recArr[rowsNum].a=strtok(buff, delimiter); recArr[rowsNum].b=strtok(NULL, delimiter); printf("%s\n", recArr[rowsNum].a); printf("%s\n\n", recArr[rowsNum].b); /*Key=NULL; Value=NULL;*/ rowsNum++; printf("%d\n", rowsNum); } fclose(fp); int i; for (i=0;i<3;i++) { printf("%s\n", recArr[i].a); printf("%s\n\n", recArr[i].b); } //printf("%d\n", rowsNum); } 
5

1 Answer 1

1
#include <stdio.h> #include <stdlib.h> #include <string.h> // this is needed to use strtok function (and strlen, strcpy) #define MAX_LEN 422498 #define MAX_KEY 128 struct record { char* a; char* b; }; int main(int argc, char **argv){ char* input=argv[1]; char* output=argv[2]; char buff[MAX_LEN]; // buff should be char[MAX_LEN], not char*[MAX_LEN] char *delimiter = ";"; //printf("%s\n",input); //printf("%s\n",output); FILE *fp; fp = fopen(input, "r"); //opening file*/ if( fp == NULL ) { perror("Error while opening the file.\n"); exit(EXIT_FAILURE); } int rowsNum=0; struct record* recArr = malloc(3 * sizeof (struct record)); //struct record recArr[4]; //recArr=malloc(1 * 2*sizeof(char*)); /*recArr[0].a="aa"; recArr[0].b="bb"; recArr[1].a="cc"; recArr[1].b="dd"; recArr[2].a="ee"; recArr[2].b="ff"; printf("%s\n", recArr[0].a); printf("%s\n\n", recArr[0].b); printf("%s\n", recArr[1].a); printf("%s\n\n", recArr[1].b); printf("%s\n", recArr[2].a); printf("%s\n\n", recArr[2].b);*/ while (fgets(buff, MAX_LEN, (FILE*)fp)!=NULL) { //recArr=realloc(recArr, (rowsNum+1) * 2*sizeof(char*)); //char* Key = strtok(buff, delimiter); //char* Value = strtok(NULL, delimiter); //recArr[rowsNum].a=strtok(buff, delimiter); //recArr[rowsNum].b=strtok(NULL, delimiter); // you have to COPY the string char* a=strtok(buff, delimiter); char* b=strtok(NULL, delimiter); recArr[rowsNum].a = malloc(sizeof(char) * (strlen(a) + 1)); // +1 for terminating '\0' recArr[rowsNum].b = malloc(sizeof(char) * (strlen(b) + 1)); strcpy(recArr[rowsNum].a, a); strcpy(recArr[rowsNum].b, b); printf("%s\n", recArr[rowsNum].a); printf("%s\n\n", recArr[rowsNum].b); /*Key=NULL; Value=NULL;*/ rowsNum++; printf("%d\n", rowsNum); } fclose(fp); int i; for (i=0;i<3;i++) { printf("%s\n", recArr[i].a); printf("%s\n\n", recArr[i].b); } //printf("%d\n", rowsNum); // you should free the allocated buffer for (i=0;i<3;i++) { free(recArr[i].a); free(recArr[i].b); } free(recArr); } 
  • string.h should be included to use strtok : GCC made some warnings about it.
  • the type of buff was not proper: it should be char buff[MAX_LEN], not char* buff[MAX_LEN](an extra asterisk is there)
  • the function strtok modifies the buffer given and returns the pointer that is pointing somewhere in the buffer. When reading the next line, the buffer is overwritten and the text previously read is lost unless it is copied to somewhere. For that reason, I added some code that copies the strings read and save pointers that points where the strings are copied instead of somewhere in buff.
Sign up to request clarification or add additional context in comments.

1 Comment

Mike, can you please add information about what the OP did wrong and how you fixed that in your code?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.