Print to file...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;
int main()
{
	srand(clock());
	ofstream fout("test.txt");
	int byte;
	cout<<"How many bytes?";
	cin>>byte;
	for (int i=0; i<byte/2; i++)
		fout<<rand()%10<<" ";
	system("pause");
	return 0;
}


Why doesnt this work if byte is a large number like 1000?
fout<<rand()%10<<" ";


This causes an error.
<<rand()%10<<
- Ok, and no need to say more, but
<<" ";

You used only a character. (A regular char value) -> not string
fout doesn't accept one-size string.
Please change to :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;
int main()
{
	srand(clock());
	ofstream fout("test.txt");
	int byte;
	cout<<"How many bytes?";
	cin>>byte;
	for (int i=0; i<byte/2; i++)
		fout<<rand()%10<<"  "; //Added 1 space charcter
	system("pause");
	return 0;
}


Hope this helps :)
Last edited on
How would i make it print only 1 space and not 2... I need it to be only 1 space between each numbers.

The file size of the output file matters.
I don't know, but do you really want :
‰′″‰′‱‷‱‵‵‷‰‶‱‵‶‷″‱‰‵‸‶‰‰‷‷‷‱‷‶‷″‸″‷‷‴′‱‸‶‹‴‹‵‸‹‹‶‱‴‴‶‸′′‴‷‶‸′‷″″″‰′‷‵‱‴″′‹‴‹‶‴‷″‵″″‰‴″‱‴‵‹‰‷‶‹‶‹‷‰″′‴‱′‵‱‹‷‰‰‴‸‴‷‶‱‸′‰‴‱‵‷‰‴′‶‷″‴‸

???????????????????????????????????????????????
thats teh problem!!! it needs to do number space nubmer space
....!!!

Idea 0 :
1
2
3
4
5
6
7
8
9
10
11
12
13
{
	srand(clock());
	ofstream fout("test.txt");
	int byte;
        int temp; //Lucky variable
	cout<<"How many bytes?";
	cin>>byte;
	for (int i=0; i<byte/2; i++)
		{temp = rand()%10;

		fout<<temp<<" "; }
	system("pause");
	return 0;

Output :
‰′″‰′‱‷‱‵‵‷‰‶‱‵‶‷″‱‰‵...


Idea 1 :
1
2
3
4
5
6
7
8
9
10
11
12
13
{
	srand(clock());
	ofstream fout("test.txt");
	int byte;
        int temp; //Lucky variable
	cout<<"How many bytes?";
	cin>>byte;
	for (int i=0; i<byte/2; i++)
		{temp = rand()%10;

		fout<<temp<< ' '; }//string-> char
	system("pause");
	return 0;

Output :
‴″‱‴‵‹‰‷‶‹‶‹‷‰″′‴‱′‵‱‹‷...


Idea 2 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
	char string[5];
	string[0] = 32;
	string[1] = 0;
	srand(clock());
	ofstream fout("test.txt");
	int byte;
        int temp; //Lucky variable
	cout<<"How many bytes?";
	cin>>byte;
	for (int i=0; i<byte/2; i++)
		{temp = rand()%10;

		fout<<temp<< string; }
	system("pause");
	return 0;

Output :
″‱‴‵‹‰‷‶‹‶‹‷‰″′‴‱′‵‱‹...

??????????????????????????????????????????????????????
Last edited on
Idea 3 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
       char string[5];

	srand(clock());
	ofstream fout("test.txt");
	int byte;
	cout<<"How many bytes?";
	cin>>byte;
	int temp;
	for (int i=0; i<byte/2; i++)
	{temp = rand()%10;
          itoa(temp,string,10);
		fout << string << " ";

	}
	system("pause");
	return 0;

Output :
″‱‴‵‹‰‷‶‹‶‹‷‰″′‴‱′‵‱‹..



Idea 4 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
       char string[5];

	srand(clock());
	ofstream fout("test.txt");
	int byte;
	cout<<"How many bytes?";
	cin>>byte;
	int temp;
	for (int i=0; i<byte/2; i++)
	{temp = rand()%10;
          itoa(temp,string,10);
          strcat(string, " ");
		fout << string; //Avoid printing directly " " character

	}
	system("pause");
	return 0;


Output :
″‱‴‵‹‰‷‶‹‶‹‷‰″′‴‱′‵‱‹..

??????????????????????????????????????????


Idea 5 :
1
2
3
4
5
6
7
8
9
10
11
12
	srand(clock());
	ofstream fout("test.txt");
	int byte;
	cout<<"How many bytes?";
	cin>>byte;
	int temp;
	for (int i=0; i<byte/2; i++)
	{temp = rand()%10;
		fout << " " << temp; 
	}
	system("pause");
	return 0;


Output :
8 9 8 7 5 7 5 5 0 2 3 0 2 1 7 1 5 5 7 0 6 1 5 6 7 3 1 0 5 8 6 0 0 7 7 7 1 7 6 7 3 8 3 7 7 4 2 1 8 6


Yes!!!!!!!!!!Success!!!!!!!!

(Hope it helped.) :)
Last edited on
My next question is.. why does this way work, but not the other way...

Thanks tho :)
Last edited on
Perhaps fout still has some remaining errors that haven't been fixed yet (stack)...
after some testing, i found that the output can be read in binary...

Though i have no idea why the fout suddenly switches over from normal txt to binary format... Wish someone would shed light on this
Last edited on
¿what the hell happened here?

fout doesn't accept one-size string.
¿? yes, it does.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;
int main()
{
	srand(clock());
	ofstream fout("test.txt");
	int byte;
	cout<<"How many bytes?";
	cin>>byte;
	for (int i=0; i<byte/2; i++)
		fout<<rand()%10<<" ";
	system("pause");
	return 0;
}



The question is.. can you tell me the reason why this code outputs a binary file if byte is a large number...

and outputs in normal text if byte is a small number like 5
It does not. ¿what are you using to open the file?
the code output becomes binary format if you input byte as a large number... try it yourself.

Open the gibberish file using visual studios
Last edited on
@ne555
Sorry, not related.
How can you insert ¿ character?
the code output becomes binary format if you input byte as a large number... try it yourself.


No, it doesn't.


Open the gibberish file using visual studios

The shortcomings of Notepad. If you actually add the file to your project and open it from the solution explorer you will see something entirely different than if you open it via Notepad from the editor (which it will do by default.)
Last edited on
how come this makes a unreadable file in notepad?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;
int main()
{
	srand(clock());
	ofstream fout("test.txt");
	int byte;
	cout<<"How many bytes?";
	cin>>byte;
	for (int i=0; i<byte/2; i++)
		fout<<rand()%10<<" ";
	system("pause");
	return 0;
}


while

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;
int main()
{
	srand(clock());
	ofstream fout("test.txt");
	int byte;
	cout<<"How many bytes?";
	cin>>byte;
	for (int i=0; i<byte/2; i++)
		fout<<" "<<rand()%10;
	system("pause");
	return 0;
}


or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;
int main()
{
	srand(clock());
	ofstream fout("test.txt");
	int byte;
	cout<<"How many bytes?";
	cin>>byte;
	for (int i=0; i<byte/2; i++)
		fout<<rand()%10<<"  ";
	system("pause");
	return 0;
}

this makes a readable file
Last edited on
The first one looks like a unicode file to notepad.
my question is, why...?
Perhaps ofstream still has some remaining errors that haven't been fixed yet (stack)... 

Nobody knows.
Last edited on
Topic archived. No new replies allowed.