First-catch exception problem

First-chance exception at 0x5612EB74 (msvcr110d.dll) in Project1.exe: 0xC0000005: Access violation reading location 0x00468CA0.

If there is a handler for this exception, the program may be safely continued.


This is the error I faced when every time I try to access the accounts i set other than admin. It is a login page of the system.

In the same system, any time I want to use ifstream, it will also give me this error.

The code is something like this:
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
ifstream file(userfile);
	 // fstream file;
	//	file.open(userfile,ios::binary|ios::in);
	  check f;
	  int sof=getSizeofUserFile();
	  if(file.is_open()){
		  for(int i=0;i<sof;i++){
			  file.read((char*)&f,sizeof(f));
			  if (loginid==f.getUID() && password==f.getPW()){
				  int lllll=1;
				  abc=f;
				  if(f.getuTYPE()=='A'){
					   admin aaaaa;
					   aaaaa.setUID(f.getUID());
					   aaaaa.setPW(f.getPW());
					   aaaaa.setgender(f.getgender());
					   aaaaa.setFname(f.getFname());
					   aaaaa.setLname(f.getLname());
					   aaaaa.setNRIC(f.getNRIC());
					   aaaaa.setphoneNO(f.getphoneNO());
					   cout<<"Loading your data... You logged in as ADMIN\n\n\n"<<endl;
					   file.close();
					   aaaaa.mainmenu();
					   std::exit(0);
				  }
				  if(f.getuTYPE()=='S'){
					   staff aaaaa;
					   aaaaa.setUID(f.getUID());
					   aaaaa.setPW(f.getPW());
					   aaaaa.setgender(f.getgender());
					   aaaaa.setFname(f.getFname());
					   aaaaa.setLname(f.getLname());
					   aaaaa.setNRIC(f.getNRIC());
					   aaaaa.setphoneNO(f.getphoneNO());
					   aaaaa.setdepartment(f.getdepartment());
					   cout<<"Loading your data... You logged in as STAFF\n\n\n"<<endl;
					   file.close();
					   aaaaa.mainmenu();
					   std::exit(0);
				  }
				  if(f.getuTYPE()=='P'){
					   passenger aaaaa;
					   aaaaa.setUID(f.getUID());
					   aaaaa.setPW(f.getPW());
					   aaaaa.setgender(f.getgender());
					   aaaaa.setFname(f.getFname());
					   aaaaa.setLname(f.getLname());
					   aaaaa.setNRIC(f.getNRIC());
					   aaaaa.setphoneNO(f.getphoneNO());
					   cout<<"Loading your data... You logged in as Normal User\n\n\n"<<endl;
					   file.close();
					   aaaaa.mainmenu();
					   std::exit(0);
				  }


			  }
		  }if (lllll==0){
			  cout<<"Wrong ID Password combination. Return 0"<<endl;
			  file.close();
			  getch();
			  std::exit(0);
			  
	  }


The full code can be downloaded here: pastebin link at following reply

Access with administrator on VS is not working.

Please help...
Sorry for my poor English as it is not my first tongue.
Last edited on
Sorry, but I'm not going to download a zip file. There are plenty of sites where you can post the source files without zipping them.

What is f and (given the way you're using it) what do you think line 8 accomplishes?
I'm new, and I don't know about posting source code on other cites, i'll google and do it in next min.

f is 'check' class that inherited from 'users' class to read the object, get the ID and Password of the object.

file.read((char*)&f,sizeof(f));
this line is read the object and (save?) it in f, with its object size.
Last edited on
You must never, ever, ever, ever read/write non-POD types to a file like that. Never ever ever. (I even submit that you should never read any variable that is larger than 1 byte in size from a file that way -- but that's another topic)

I assume your 'check' class contains one or more strings, which would cause the above read line to explode.
I don't know about posting source code on other cites,

One such site is http://pastebin.com/


f is 'check' class

And what is the definition of the check class?
OK, now i go to pastebin after this post

Definition of check class.
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
class users{
protected:
	string UID; //use for both login and search
	string PW; //use for login
	char uTYPE; //user's type (a - admin, s - staff, p - passenger)
	string Fname; //assume that only record down people's first and last name]
	string Lname; //with no space allowed
	string NRIC; //identification number
	char gender; //( m - male , f - female )
	string phoneNO;
	string department; //only use by staff, NULL for admin and passenger
public:
	users();
	void setUID(string);
	void setPW(string);
	void setuTYPE(char);
	void setFname(string);
	void setLname(string);
	void setNRIC(string);
	void setgender(char);
	void setphoneNO(string);
	string getUID();
	string getPW();
	char getuTYPE();
	string getFname();
	string getLname();
	string getNRIC(); 
	char getgender();
	string getphoneNO(); 
	virtual void mainmenu()=0;
};

class check:public users{ //purly for the user name check purpose
public:
	check();
	void mainmenu();
	string getdepartment();
	void setdepartment(string);
	~check();
};
Yup, users (and therefore check) is a non-POD type because it contains a string (which is a non-POD type).

Therefore you cannot use read()/write() to read/write that class to a file. It simply does not work.



Also, you're probably misusing inheritance here. Classes should be nouns. 'User' is a good class... but what is a 'Check'? If you want to check something (ie: a verb) then it should probably just be a function.

classes -> nouns
functions -> verbs
@Disch
So I should store the file in other way, so I can read in other way?
like

fout<<UID<<" "<<PW<<" ".....;

?
You cannot use fstream::read or fstream::write to store or retrieve a check from a file. These functions are only for use with trivial types. Anywhere there is indirection involved (as there is with std::string) you've got a problem.
Or in another way, if I change all the string to char array, will it work?
Yes, using << and >> to do file I/O can work because that converts everything to text rather than writing it as binary data.


But it's not as easy as you might think. Remember that >> stops at whitespace... so if you write a string that says "foo bar"... when you try to read it back it will be read back as "foo", leaving the "bar" in the file and destroying the rest of the object read.

If none of your strings have whitespace then you can just use << to dump it all to a file and >> to reload it. But if you need whitespace, you'll have to be more creative.
None of the string consist of white space.

However, if I save it in non-binary, isn't that seekg/p function will not be working? Then editing will not be possible?
You are correct you cannot really seek with text files. At least not in such a simplistic manner.

But your problem is bigger than that. Strings (and therefore your object which contains several strings), is going to be a variable size in a file. So even if you store it in binary you won't be able to seek to a specific object or overwrite a single object. You'll have to scan over the file by examining each object, determining its size, and seeking past it (or put an index at the start which has the position of each object in the file).

Overwriting data is an even bigger challenge, as you have to "shift over" all the data after the object you're writing if the object is not the exact same size as the object that was there previously.



Alternatively, you could use fixed-length strings (ie: a char array). This would allow you to dump them to a file in binary, would have the objects be fixed size in the file, would allow you to seek/edit, etc. Downside is you wouldn't be able to have strings longer than a predetermined maximum.



If any of this is unclear or doesn't seem to make sense... I strongly recommend you get a hex editor and look at the files you're creating. To understand binary files, you really need to view them in a hex editor. There's really no other way.


EDIT:

This article I wrote a while back has some info which may be a good starting point:

http://www.cplusplus.com/articles/DzywvCM9/
Last edited on
Thanks~ Although I still have a small question.
If I create a date class that include (int, int, int), can I use it as a data type of another obj and write/read to/from a file?
Long answer:

POD types are "plain old data". Fundamental types like int, char, double, etc are all POD types. IE, the variables that the language gives you. Anything that you have to include a header for (like strings, vectors, etc) are not [necessarily] POD types. Also, pointers are not considered POD.

So basically... POD types:
- int
- char
- short
- double
- long
- etc

NON-POD types:
- std::string
- std::vector
- pointers



Classes/structs that you make are POD unless ANY of the below are true. If any of the below are true, your class is NOT POD:

1) Your class contains any members which are not POD
2) Your class derives from a non-POD type
3) Your class contains any virtual functions
4) Your class has virtual inheritance



reading/writing something to a binary file with read()/write() functions only works if the thing you're reading/writing is a POD type.

-------------------
Short answer:

yes, that simple date class would be fine.
Last edited on
Thanks~ All doubt cleared~
Problem resolved.
Topic archived. No new replies allowed.