file reading doubt

i am going to be searching for a record in seven different .dat files
is some thing like this possible
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 fstream f2;

  char record_name[7][29]={{"Flight_RecordSunday.dat"},{"Flight_RecordMonday.dat"},
                           {"Flight_RecordTuesday.dat"},{"Flight_RecordWednesday.dat"},
                           {"Flight_RecordThursday.dat"},{"Flight_RecordFriday.dat"},
                           {"Flight_RecordSaturday.dat"}};
  char day[7][15]={"Sunday.dat","Monday.dat","Tuesday.dat","Wednesday.dat",
                  "Thursday.dat","Friday.dat","Saturday.dat"};
  for(int i=0;i<7;i++)                      //To determine the day of the flight
   {
     f2.open(record_name[i],ios::in|ios::binary);
     while(f2.read((char*)&f,sizeof(f)))
     if(!strncmp(code,f.retcode(),sizeof(f.retcode())))
       {
       strcpy(day,day[i]);
       return true;
       }
      f2.close();
 
     }
 


the above code doesn't work so could someone explain me how to do this (if possible)

thank you in advance
EDIT:
this is how i actually implemented the code and trying to fina a way to reduce it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  fstream f2;

  for(int i=0;i<7;i++)                      //To determine the day of the flight
   {
   if(i==0)                 //searches through the flight records of eact day to
     {                              //see if the flight is on the perticular day
     f2.open("Flight_RecordSunday.dat",ios::in|ios::binary);
     while(f2.read((char*)&f,sizeof(f)))
     if(!strncmp(code,f.retcode(),sizeof(f.retcode())))
       {
       strcpy(day,"SUNDAY");
       return true;
       }
     }

   if(i==1)
     {
     f2.open("Flight_RecordMonday.dat",ios::in|ios::binary);
     while(f2.read((char*)&f,sizeof(f)))
     if(!strncmp(code,f.retcode(),sizeof(f.retcode())))
      {
       strcpy(day,"MONDAY");
       return true;
       }
     }

   if(i==2)
     {
     f2.open("Flight_RecordTuesday.dat",ios::in|ios::binary);
     while(f2.read((char*)&f,sizeof(f)))
     if(!strncmp(code,f.retcode(),sizeof(f.retcode())))
		 {
     strcpy(day,"TUESDAY");
       return true;
       }
     }

   if(i==3)
     {
     f2.open("Flight_RecordWednesday.dat",ios::in|ios::binary);
     while(f2.read((char*)&f,sizeof(f)))
     if(!strncmp(code,f.retcode(),sizeof(f.retcode())))
		 {
		 strcpy(day,"WEDNESDAY");
       return true;
       }
     }

   if(i==4)
     {
     f2.open("Flight_RecordThursday.dat",ios::in|ios::binary);
     while(f2.read((char*)&f,sizeof(f)))
     if(!strncmp(code,f.retcode(),sizeof(f.retcode())))
		 {
       strcpy(day,"THURSDAY");
       return true;
       }
     }

   if(i==5)
     {
     f2.open("Flight_RecordFriday.dat",ios::in|ios::binary);
     while(f2.read((char*)&f,sizeof(f)))
     if(!strncmp(code,f.retcode(),sizeof(f.retcode())))
		 {
		 strcpy(day,"FRIDAY");
       return true;
       }
     }

   if(i==6)
     {
     f2.open("Flight_RecordSaturday.dat",ios::in|ios::binary);
     while(f2.read((char*)&f,sizeof(f)))
     if(!strncmp(code,f.retcode(),sizeof(f.retcode())))
       {
       strcpy(day,"SATURDAY");
       return true;
       }
     }
   f2.close();
   }
Last edited on
I'm a bit confused of where the variable "f" came from in lines 12 and 13. You first cast it as a char* in line 12 and then you call a function for it on line 13. What is its data type exactly?
f is the name of a class but that section works perfectly
Is the compiler throwing errors or is the programing crashing in run-time? What's the exact error? From here, it looks like you're overwriting the object in memory which is threatening the integrity of the object on line 12. Maybe create a char* variable in your class and read your file into that?

Then you're copying day[i] to the first position of day I would presume. Are you trying to alter a day variable within the class or of the actual day array within your function?
the main intention of this section is to find the correct flight for the destination but that is not the problem

i wanted to know weather the code in the edited section can be reduced to the form as given in the first code braces? if yes then what am i doing wrong
It can be done as you have done. I believe the problem that you're ultimately running into is how exactly you're handling your data. I guess what really needs to be known is what exact problem are you getting that makes you say the code doesn't work. What are you expect and what are you getting?

These are the erroes i get


Error:  airport.cpp(524,26):Cannot convert 'char ( *)[15]' to 'char *'
Error:  airport.cpp(524,26):Type mismatch in parameter '__dest' in call to 'strcpy(char *,const char *)'


or line 15 as per above
the data type of day is
char day[11];

Your error is saying you're trying to convert a multidimensional array to a one dimnensional array. I believe you have declared two variables named day. On line 7 of the original code you posted and the declaration you just provided.
yep the errors occured as both had the same name added an 's' and the problem was solved
Topic archived. No new replies allowed.