Nov 20, 2012 at 1:48am UTC
This causes an error.
- 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 Nov 20, 2012 at 2:07am UTC
Nov 20, 2012 at 2:13am UTC
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.
Nov 20, 2012 at 2:18am UTC
thats teh problem!!! it needs to do number space nubmer space
Nov 20, 2012 at 2:23am UTC
....!!!
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 Nov 20, 2012 at 2:24am UTC
Nov 20, 2012 at 2:38am UTC
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 Nov 21, 2012 at 7:48am UTC
Nov 20, 2012 at 2:44am UTC
My next question is.. why does this way work, but not the other way...
Thanks tho :)
Last edited on Nov 20, 2012 at 2:45am UTC
Nov 20, 2012 at 2:48am UTC
Perhaps fout still has some remaining errors that haven't been fixed yet (stack)...
Nov 20, 2012 at 3:07am UTC
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 Nov 20, 2012 at 3:08am UTC
Nov 20, 2012 at 3:41am UTC
It does not. ¿what are you using to open the file?
Nov 20, 2012 at 3:57am UTC
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 Nov 20, 2012 at 3:58am UTC
Nov 20, 2012 at 4:21am UTC
@ne555
Sorry, not related.
How can you insert ¿ character?
Nov 20, 2012 at 4:44am UTC
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 Nov 20, 2012 at 4:44am UTC
Nov 20, 2012 at 4:50am UTC
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 Nov 20, 2012 at 4:51am UTC
Nov 20, 2012 at 4:53am UTC
The first one looks like a unicode file to notepad.
Nov 20, 2012 at 5:09am UTC
Perhaps ofstream still has some remaining errors that haven't been fixed yet (stack)...
Nobody knows.
Last edited on Nov 20, 2012 at 5:09am UTC