Hi:
I am trying to read from a file, which stores answers for a particular question(The answers are stop listed ). Each answer in the file is enclosed in opening and closing braces.
Here is the sample file
{ tenets nutrigenomics jim kaput improper diets risk factors disease dietary chemicals alter gene expression andor genome diseases }
{ individuals nutritional needs a result own genetic }
I am trying to read from this file and store all the answers in form of arrays. That is, each array element contains one answer. Each answer in the array element is again stored as a array of strings.
I am reading this input file using 'fscanf' function, I am able to read the first two words( tenets and nutrigenomis) properly, but when i try to read the third word in my while loop the the following string is being read( " jim kaput improper "). This string is not terminated by a null character.
I know the code can be optimized further for better performance, but besides this if you see any other problem in the code please let me know.
#define TRUE 1
#define FALSE 0
#include<stdio. h>
typedef struct answers
{
int ans_num; // stores the answer number
char data[50][20]; // stores the actual answer, we havw assumed that the length of the answer will not exceed 1000 characters
int total_num_words ; // stores the total number of answers
} ans_type;
/*
This structure is used to store information about the questions. Module number, question number, answers
related to the questions, and the total number of answers associated with the question
*/
typedef struct question_answer s
{
char module; // Stores the character associated with the module
int ques_num; // Stores the question number
ans_type ans[65]; // Stores the pointer to answers
int total_num_ans; // Stores the total number of answers
} ques_ans;
//int read_file( ques_ans *ques,char *fn)
void main()
{
FILE *fp;
int closed= TRUE;
char word[20],fn[20];
int num_ans = 0,num_words = 1,i,j;
ques_ans *ques,ques1;
ques->total_num_an s = 0;
//ques = &ques1;
//flush();
printf(" Enter the name of the file\n");
scanf("%s",fn);
if(( fp= fopen(fn,"r"))= = NULL)
{
printf("Cannot open the file\n");
return;
}
while(fscanf( fp,"%s", word) != EOF)
{
if (!strcmp(word, "{") && ( closed ==TRUE))
{
num_ans++;
closed = FALSE;
}
else if(!strcmp(word , "}") && ( closed ==FALSE))
{
closed = TRUE;
ques->ans[num_ans].total_num_word s = num_words;
num_words = 1;
}
else
{
strcpy(ques ->ans[num_ans].data[num_words], word);
num_words++;
}
}
ques-> total_num_ans = num_ans;
printf("The data collected is displayed below\n");
for(i=1; i<= ques->total_num_ans; i++)
{
printf("%d -> ",i);
for(j=1;j< ques->ans[i].total_num_word s;j++)
{
printf("%s ",ques->ans[i].data[j]);
}
printf("\n");
}
fclose(fp);
free(ques);
}
I am trying to read from a file, which stores answers for a particular question(The answers are stop listed ). Each answer in the file is enclosed in opening and closing braces.
Here is the sample file
{ tenets nutrigenomics jim kaput improper diets risk factors disease dietary chemicals alter gene expression andor genome diseases }
{ individuals nutritional needs a result own genetic }
I am trying to read from this file and store all the answers in form of arrays. That is, each array element contains one answer. Each answer in the array element is again stored as a array of strings.
I am reading this input file using 'fscanf' function, I am able to read the first two words( tenets and nutrigenomis) properly, but when i try to read the third word in my while loop the the following string is being read( " jim kaput improper "). This string is not terminated by a null character.
I know the code can be optimized further for better performance, but besides this if you see any other problem in the code please let me know.
#define TRUE 1
#define FALSE 0
#include<stdio. h>
typedef struct answers
{
int ans_num; // stores the answer number
char data[50][20]; // stores the actual answer, we havw assumed that the length of the answer will not exceed 1000 characters
int total_num_words ; // stores the total number of answers
} ans_type;
/*
This structure is used to store information about the questions. Module number, question number, answers
related to the questions, and the total number of answers associated with the question
*/
typedef struct question_answer s
{
char module; // Stores the character associated with the module
int ques_num; // Stores the question number
ans_type ans[65]; // Stores the pointer to answers
int total_num_ans; // Stores the total number of answers
} ques_ans;
//int read_file( ques_ans *ques,char *fn)
void main()
{
FILE *fp;
int closed= TRUE;
char word[20],fn[20];
int num_ans = 0,num_words = 1,i,j;
ques_ans *ques,ques1;
ques->total_num_an s = 0;
//ques = &ques1;
//flush();
printf(" Enter the name of the file\n");
scanf("%s",fn);
if(( fp= fopen(fn,"r"))= = NULL)
{
printf("Cannot open the file\n");
return;
}
while(fscanf( fp,"%s", word) != EOF)
{
if (!strcmp(word, "{") && ( closed ==TRUE))
{
num_ans++;
closed = FALSE;
}
else if(!strcmp(word , "}") && ( closed ==FALSE))
{
closed = TRUE;
ques->ans[num_ans].total_num_word s = num_words;
num_words = 1;
}
else
{
strcpy(ques ->ans[num_ans].data[num_words], word);
num_words++;
}
}
ques-> total_num_ans = num_ans;
printf("The data collected is displayed below\n");
for(i=1; i<= ques->total_num_ans; i++)
{
printf("%d -> ",i);
for(j=1;j< ques->ans[i].total_num_word s;j++)
{
printf("%s ",ques->ans[i].data[j]);
}
printf("\n");
}
fclose(fp);
free(ques);
}
Comment