Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

'read': Identifier Not Found, Short Code Plz Help


  • Please log in to reply
6 replies to this topic

#1 chopficaro

chopficaro

  • Members
  • 68 posts
  • OFFLINE
  •  
  • Local time:03:51 PM

Posted 02 September 2008 - 10:58 AM

im just trying to get the hang of binary file io by making a simple program that can write a structure into a file, then read it, but the first error message i get is

'read': identifier not found

heres the code:

#include <iostream>
#include <fstream>
using namespace std;

struct grades
{
	int grade1;
	int grade2;
	int grade3;
	int grade4;
} mygradesi,mygradeso;

int main ()
{
	int answer;
	do
	{
		cout<<"do u want to 1, write to the file or 2, read from the file, or 3, exit the program?";
		cin>>answer;
		if (answer==2)
		{
			struct grades * mygradesptri;
			mygradesptri=&mygradesi;
			ifstream myfilei;
			myfilei.open ("binary.bin", ios::in|ios::binary);
			if (myfilei.is_open())
			{
				read(mygradesptri, sizeof(grades));
				cout<< mygradesi.grade1<< mygradesi.grade2<< mygradesi.grade3<< mygradesi.grade4;
			}
			else cout << "Unable to open file for input";
		}
		if (answer==1)
		{
			struct grades * mygradesptro;
			mygradesptro=&mygradeso;
			ofstream myfileo;
			myfileo.open ("binary.bin", ios::out|ios::binary);
			cout>>"please enter 4 grades";
			cin<< mygradeso.grade1<< mygradeso.grade2<< mygradeso.grade3<< mygradeso.grade4;
			if (myfileo.is_open())
			{
				write (mygradesptro, sizeof(grades));
				myfileo.close();
			}
			else cout << "Unable to open file for output";
		}
	}while (answer!=3);
	return 0;
}


BC AdBot (Login to Remove)

 


#2 chopficaro

chopficaro
  • Topic Starter

  • Members
  • 68 posts
  • OFFLINE
  •  
  • Local time:03:51 PM

Posted 02 September 2008 - 11:44 AM

how silly of me, i did not specify the name of the class. i ran into a new problem though, read and write are supposed to take char *, but im not trying to pass a char, im trying to pass a structure pointer, how do i do that?

error says cannot convert grades * to char *

#include <iostream>
#include <fstream>
using namespace std;

struct grades
{
	int grade1;
	int grade2;
	int grade3;
	int grade4;
} mygradesi,mygradeso;

int main ()
{
	int answer;
	do
	{
		cout<<"do u want to 1, write to the file or 2, read from the file, or 3, exit the program?";
		cin>>answer;
		if (answer==2)
		{
			char * mygradesptri;
			mygradesptri=&mygradesi;
			ifstream myfilei;
			myfilei.open ("binary.bin", ios::in|ios::binary);
			if (myfilei.is_open())
			{
				myfilei.read(mygradesptri, sizeof(grades));
				cout<< mygradesi.grade1<< mygradesi.grade2<< mygradesi.grade3<< mygradesi.grade4;
			}
			else cout << "Unable to open file for input";
		}
		if (answer==1)
		{
			char * mygradesptro;
			mygradesptro=&mygradeso;
			ofstream myfileo;
			myfileo.open ("binary.bin", ios::out|ios::binary);
			cout>>"please enter 4 grades";
			cin<< mygradeso.grade1<< mygradeso.grade2<< mygradeso.grade3<< mygradeso.grade4;
			if (myfileo.is_open())
			{
				myfileo.write (mygradesptro, sizeof(grades));
				myfileo.close();
			}
			else cout << "Unable to open file for output";
		}
	}while (answer!=3);
	return 0;
}


#3 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:51 PM

Posted 02 September 2008 - 11:48 AM

It looks to me like you are using 'read' incorrectly. According to the API, you need to use the method in conjunction with the stream. My guess is that you really want to do this:
myfilei.read(mygradesptri, sizeof(grades));
Or something along those lines.

The way you are using it now:
'read': identifier not found
The compiler is taking read to be a variable of some sort, and it is not. It is a method (or a function, I should say).

#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:01:51 PM

Posted 02 September 2008 - 03:55 PM

how silly of me, i did not specify the name of the class. i ran into a new problem though, read and write are supposed to take char *, but im not trying to pass a char, im trying to pass a structure pointer, how do i do that?


You could use a UNION. Have you ever used one of those?

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 chopficaro

chopficaro
  • Topic Starter

  • Members
  • 68 posts
  • OFFLINE
  •  
  • Local time:03:51 PM

Posted 02 September 2008 - 04:14 PM

i think i see what ur saying, a union can store a char and a structure in the same space. but do i have to use a union every time i want to use binary file io?

#6 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:01:51 PM

Posted 02 September 2008 - 06:59 PM

Remember that C++ file i/o is character oriented. All input to/from files is done with characters.

What you really should do is add a function like writeToFile(ofstream outStream*) which does the writing for you. Inside the funciton, you can do http://www.cplusplus.com/reference/iostrea...ator%3C%3C.html to write out the integers.

C++ Does not provide an easy way for i/o on structures.

Union can do the pointer conversion for you though. So if you end your structure with something like char end=NULL; you can use string manipulation functions like those found in ofstream to do your dirty work.

I personally like the first method because it's cleaner and easier to read but it's a wee bit more code.

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#7 chopficaro

chopficaro
  • Topic Starter

  • Members
  • 68 posts
  • OFFLINE
  •  
  • Local time:03:51 PM

Posted 03 September 2008 - 05:42 PM

thanx for the help, i used casting.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users