Unexpected Ofstream file output

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
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
short ItemNo;
short Px;
char YourName[25];

ofstream * FileTo; 
FileTo = new ofstream();

if (FileTo==NULL)
{ 
cerr<<"Ooops we couldn't create file on Heap \n";
}

FileTo->open("My_CplusPlus_file.txt");

if (FileTo->bad()==1)
{cerr<<"Ooops a bad file \n";}

while (1)
{
cout<<"Please enter the item No. (Enter 0 to quit) \n";
cin>> ItemNo;

if (ItemNo==0)
{break;}

cout<<Please enter the price of the item purchased \n";
cin>> Px;
cout<<"Please enter your name \n";
cin.get(YourName, 25);

FileTo->write((const char*)&ItemNo, sizeof(ItemNo);
FileTo->write((const char*)&Px, sizeof(Px);
FileTo->write(YourName, 25);
}

FileTo->close();
delete FileTo;

} 



The output in the *txt file is below:

e
Karen ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌf Debbie ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌg Simon ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌh
Sharon ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ

The issues are:
1. Why am I getting these weird output displayed when i opened the text file in the folder where it is saved?...only the names seem to display okay...

2. How can I display the output in the *txt file in fine rows (e.g. Karen on a single row with Price and Item Nos in respective columns- i.e. tabular form)...

I think # include <iomanip> file and using setw() can do or perhaps using a 'for' within 'for' loops but not sure which method?

Many thanks
Last edited on
I appreciate people are busy seeing that the knowleadgeable ones are being inundated with queries from left to right...
However, if will be very well appreciated if someone respond to this please.

Anyway, I have taken the liberty to read the file using 'ifstream' as i thought perhaps i am not supposed to manually open the *txt file i wrote to the directory (please advise if my understanding is wrong)?

To add insult to injury my new read code is producing below errors (please note, these codes are only part of my main program):

--------------------Configuration: Menu - Win32 Debug--------------------
Compiling...
Menu.cpp
C:\Documents and Settings\Owner\My Documents\My Projects\Menu\Menu.cpp(98) : error C2248: 'init' : cannot access protected member declared in class 'std::basic_ios<char,struct std::char_traits<char> >'
c:\program files\microsoft visual studio\vc98\include\ios(79) : see declaration of 'init'
C:\Documents and Settings\Owner\My Documents\My Projects\Menu\Menu.cpp(98) : error C2664: 'void __thiscall std::basic_ifstream<char,struct std::char_traits<char> >::open(const char *,int)' : cannot convert parameter 2 from 'void (__thiscall std::bas
ic_ios<char,struct std::char_traits<char> >::*' to 'int'
Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
Error executing cl.exe.

Menu.obj - 2 error(s), 0 warning(s)
--------------------------------------------------------------------------------------------------

This is my read file code (please note it is in a different function to the write code as the overall project is a menu call, i hope this information helps)...

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
{
short Item, price;
char YourName [25];
ifstream * FileTo;
FileTo = new ifstream();

if (FileTo==NULL)
{
	cerr<<"Cannot create file on Heap\n";
}

FileTo->open("My_Cplusplus_File.bin", ios::init);

if (FileTo->bad!=0)
{
	cerr<<"Error opening file\n";
}

FileTo->read((char *)&Item, sizeof(Item));
FileTo->read((char *)&price, sizeof(price));
FileTo->read(YourName, 25);

while (FileTo->eof()==0)
{
	if(Item>75)
	{
		cout<<"Item:"<<Item<<"is greater than 75\n";
	}

	FileTo->read((char *)&Item, sizeof(Item));
	FileTo->read((char *)&price, sizeof(price));
	FileTo->read(YourName, 25);
}

FileTo->close();
delete FileTo;

}

if you want a text file you shall not use the functions read()/write(). they produce binary output. use << and >> instead.

I don't see a reason why you woule use new with the streams.

What is Menu.cpp and where's the line 98?

On line 32 of your first post theres a " missing.
Thanks coder777.

As requested please see the full code below and error:

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<iostream>
#include<fstream>
using namespace std;

int menu();
void option1();
void option2(int n);

int main()
{

	bool exit=false;

	for(;;)
	{
		int choice = menu();
		
		switch(choice)
		
		{
		case 1: 
			option1();
			break;
	
		case 2: 
			option2(2);
			break;
		
		default:
			cout<<"Please select again\n";
			break;
		}
		
		if (exit)
			break;	//end forever
	}
	
	return 0;
}


int menu()
{
short n;
cout<<"***MY SOFTWARE***\n\n";
cout<<"Enter 1 = Creating/Writting file using Output File Dtream (ofstream)\n";
cout<<"Enter 2 = Opening/Reading file using Input File Stream (ifstream)\n";
cin>> n;

return n;
}

void option1()
{

short Item, price;
char YourName [25];
ofstream * FileTo;
FileTo = new ofstream();
if (FileTo==NULL)
{
	cerr<<"Error creating file on the heap!\n";
}
FileTo->open("My_Cplusplus_File.bin", ios::out|ios::binary);
while (1)
{
	cout<<"Enter Item Number (Enter 0 to quit)"<<endl;
	cin>> Item;
	if(Item==0)
	{break;}
	cout<<"Enter the Price of the Item"<<endl;
	cin>> price;
	cin.get();
	cout<<"Please enter your name"<<endl;
	cin.get(YourName, 25);
	FileTo->write((const char*)&Item, sizeof(Item));
	FileTo->write((const char*)&price, sizeof(price));
	FileTo->write(YourName, 25);
}

FileTo->close();
delete FileTo;
}


void option2()
{
short Item, price;
char YourName [25];
ifstream * FileTo;
FileTo = new ifstream();

if (FileTo==NULL)
{
	cerr<<"Cannot create file on Heap\n";
}

FileTo->open("My_Cplusplus_File.bin", ios::init);

if (FileTo->bad!=0)
{
	cerr<<"Error opening file\n";
}

FileTo->read((char *)&Item, sizeof(Item));
FileTo->read((char *)&price, sizeof(price));
FileTo->read(YourName, 25);

while (FileTo->eof()==0)
{
	if(Item>75)
	{
		cout<<"Item:"<<Item<<"is greater than 75\n";
	}

	FileTo->read((char *)&Item, sizeof(Item));
	FileTo->read((char *)&price, sizeof(price));
	FileTo->read(YourName, 25);
}

FileTo->close();
delete FileTo;

}





Deleting intermediate files and output files for project 'Menu - Win32 Debug'.
--------------------Configuration: Menu - Win32 Debug--------------------
Compiling...
Menu.cpp
c:\documents and settings\owner\my documents\my projects\menu\menu.cpp(98) : error C2248: 'init' : cannot access protected member declared in class 'std::basic_ios<char,struct std::char_traits<char> >'
c:\program files\microsoft visual studio\vc98\include\ios(79) : see declaration of 'init'
c:\documents and settings\owner\my documents\my projects\menu\menu.cpp(98) : error C2664: 'void __thiscall std::basic_ifstream<char,struct std::char_traits<char> >::open(const char *,int)' : cannot convert parameter 2 from 'void (__thiscall std::bas
ic_ios<char,struct std::char_traits<char> >::*' to 'int'
Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
Error executing cl.exe.

Menu.exe - 2 error(s), 0 warning(s)

Many thanks
Please ignore as I may have now resolved the problem....

Cheers cheers again
Topic archived. No new replies allowed.