I am trying to read file in C language and store the data in arrays , It works well except I do not want the program to finish after closing the file
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
char TimeDat[24][8] ;
float TempArr [2][24];
float HumiArr[24];
float MotionArr[6][24];
void ReadDataFile(char *location,char device_location [10],int line);
void get_menu();
int main( )
{
while(1)
{
get_menu();
}
return 0;
}
void get_menu()
{
char chosen_name [10] ;
int menu_option;
printf("Choose your task : \n");
printf("1 - Read files by name : \n");
scanf ("%d",&menu_option);
if (menu_option==1)
{
printf("Enter Your file name : Bedroom , Kitchen , LivingRoom \n");
scanf ("%s",&chosen_name);
if (strcmp(chosen_name,"Bedroom") == 0)
{
char * Bedroom_file_location="Bedroom.txt";
ReadDataFile(Bedroom_file_location ,"Bedroom",24);
}
else if (strcmp(chosen_name,"Kitchen") == 0)
{
char * Kitchen_file_location="Kitchen.txt";
ReadDataFile(Kitchen_file_location ,"Bedroom",24);
}
else if (strcmp(chosen_name,"LivingRoom") == 0)
{
char * LivingRoom_file_location="LivingRoom.txt";
ReadDataFile(LivingRoom_file_location ,"Bedroom",24);
}
}
else if (menu_option==2 )
{
}
}
void ReadDataFile(char *location,char device_location [10],int line) {
printf("this is a %s \n",device_location);
int read_status=0;
int t,k;
FILE *fp; // assign to the file location
fp = fopen(location, "r");
if(fp == NULL)
{
printf("Error!\n");
//exit(1);
}
t=0;
while(line--){
fscanf(fp, "%s", &TimeDat[t]);
fscanf(fp, "%f%f", &TempArr[t][0], &TempArr[t][1]);
fscanf(fp, "%f", &HumiArr[t]);
for ( k=0; k<6; k++)
{
fscanf(fp,"%f", &MotionArr[t][k]);
}
printf(" :%s ",TimeDat[t]);
printf(" : %f , %f ",TempArr[t][0],TempArr[t][1]);
printf(" : %f ",HumiArr[t]);
printf(" : %f , %f , %f , %f , %f , %f",MotionArr[t][0],MotionArr[t][1],MotionArr[t][2],MotionArr[t][3],MotionArr[t][4],MotionArr[t][5]);
printf(" \n");
t++;
}
fclose(fp);
main();
}
my output is 100% correct but i am getting this message :
Process returned -1073741571 (0xC00000FD) execution time : 6.531 s
Press any key to continue.
I am tring to get back to the menu so the user could open another file for example or do some other options. I will appreciate your help Thanks in advence
[SOLVED] Basicly the problem was because of 2D array
it should be :
char TimeDat[24][8] ;
float TempArr [24][2];
float HumiArr[24];
float MotionArr[24][6];
Not
char TimeDat[24][8] ;
float TempArr [2][24];
float HumiArr[24];
float MotionArr[6][24];
User contributions licensed under CC BY-SA 3.0