Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
class object
{ public:
char type,name[15],rating[6],time[8],recipe[];
int calories,tagno;
}ax;

class recipe
{ char type;

void log()
{
	setbkcolor(3);

       //	object ax(1);

	cout << "\n Enter the Tag of the dish for the recipe  ";
	gets(*tag);   

	char* txt = ".txt";
	strcat(*tag, txt);
	cout << *tag;

	clrscr();
	title();
	char ch;
	ifstream fin;
	fin.open(*tag, ios::in);

	fstream fout;
	fout.open("allfilesrecipe4.dat",ios::out|ios::in|ios::binary);

	if (!fin)
	{
		cout << "cannot open file"; getch(); exit(0);
	}
	int count=0;

	for(int i=0;fin;i++)
	{       char a3;
		fin.get(a3);
		//cout<<"<>";

		ax.recipe[i]=a3;

	       count++;
	}
///////////////////////////////////////////////////////////////////////////////
fout.seekp(0);
for(int j=0;j<=count;j++)
{
cout<<ax.recipe[j]; /////FIRST OUTPUT LOOP

	}
	getch();

	fout.write((char*)&ax,sizeof(ax));

	fin.close();
  
	again:
	object bx;
	fout.read((char*)&bx,sizeof(bx));
	fout.seekg(0);
clrscr();
	title();
		for(int k=0;k<=count;k++)
	{
	       cout<<bx.recipe[k];                                   **//////2ND OUTPUT LOOP**

	}
	getch();
       fout.close();
char c;
	cout << "\n       Enter 'h' to go back to home or press e to exit ";
	cin >> c;
	if (c == 'h')
	{
		clrscr(); home();
	}
	else if (c == 'a')
		goto again;
	else
		exit(0);

	getch();
}


What I have tried:

*IN THE ABOVE PROGRAM I AM GETTING VALID TEXT WHEN THE FIRST OUTPUT LOOP RUNS BUT WHEN I OPEN TH FILE THROUGH A DIFFERENT OBJECT THE TEXT IS NOT THE SAME

https://user-images.githubusercontent.com/55504233/65167042-6d2e2600-da5f-11e9-8147-3dcf8a85748c.png[^]

https://user-images.githubusercontent.com/55504233/65167042-6d2e2600-da5f-11e9-8147-3dcf8a85748c.png[^]
Posted
Updated 19-Sep-19 0:03am
v3
Comments
CHill60 18-Sep-19 13:15pm    
Have you tried debugging it to see what is happening?
jeron1 18-Sep-19 14:02pm    
I can't imagine this
if (!fin)

or this
for (int i = 0; fin; i++)

work the way you think. Maybe have a look at the following for reference information
http://www.cplusplus.com/reference/fstream/ifstream/
CPallini 19-Sep-19 3:58am    
How we could possibly try to fix it without having the input file(s)?
Stefan_Lang 19-Sep-19 4:14am    
On a sidenote: never call any class "object". Especially not in Windows. Just because MS did exactly that (ok, it's "CObject", but that isn't any better), it doesn't make it any less dumb: for one, in OO nomenclature, an "object" is an *instantiation* of a "class", and that is something entirely different. Second, when writing programs using OOP, the term "object" is already overused, and should be avoided to prevent confusion.

1 solution

There are multiple issues with your code.

1. naming a class "object" is bad. See my comment above.
2. Making all class members public means you're not interested in using it like you would use a class in OOP. What you have is a C struct, not a class. If that's what you intended, fine - but then don't use class, use struct!
3. Formatting doesn't appear to be your strongest point: you should strive to use consistent rules how you format your data structures and functional code. It doesn't matter all that much what rules you use, as long as it's consistent, and readable. At the moment, your code is neither. And this makes it more difficult for us to read and understand what your code is doing. Hint: there are plenty of language-sensitive editors that can help you in keeping your code nicely formatted - it's really not that much effort.
4. The class definition of recipe is incomplete. This makes it impossible for us to decide how it is initialized, and what members it has.
5. You function recipe::log() calls multiple functions that are not standard C++ functions and your code does not have any #include statements that would give us a hint where these functions may be coming from. While most can make an educated guess about setbkcolor() or clrscr(), the bets on title() are off. Yet another thing that takes our time figuring out, although it would be trivial for you to provide this vital information.

6. This line:
C++
ax.recipe[i]=a3;
will break your program! You have not provided a size for this array, which means technically it is size 0, and any attempt to access an element at any offset is undefined behaviour! what happens afterwards in your code doesn't really matter, because anything can happen as long as you don't fix that!


Sorry for boldface, I didn't mean to shout - just wanted to put an emphasis on the thing that I consider the most likely cause of your problem. You still should take the other points to heart.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900