Creating "transposed" files with random characters
I want to create a program that creates ".txt" files with random characters, but with the possibility of creating transposed "files". Next, I add an example of two ".txt" files as I intend to do.
FILE1
A|B|C|D|E|F|G|
H|I|J|K|L|M|N|
O|P|Q|R|S|T|U|
V|W|Z|1|2|3|4|
FILE2 (FILE1 transpossed)
A|H|O|V|
B|I|P|W|
C|J|Q|Z|
D|K|R|1|
E|L|S|2|
F|M|T|3|
G|N|U|4|
And now I add the code I have writen so that you can have a look and give me some ideas about how do it, what do modify and so on.
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>
#include<ctime>
using namespace std;
int main()
{
int rows, columns, element1;
char word[10];
ofstream myfile ("File 1.txt");
if(myfile)
srand(1);
for(rows=0;rows<10;rows++)
{
for(columns=0;columns<30;columns++)
{
element1 = rand() % 100000 + 1;
int len = rand () % 4 + 4;
word [len] = 0;
while (len) word [--len] = 'A' + rand () % 58;
myfile<<element1<<word;
myfile<<"|";
}
myfile<<endl;
}
myfile.close();
ofstream myfileS ("File 2.txt");
if(myfileS)
srand(1);
for(columns=0;columns<30;columns++)
{
for(rows=0;rows<10;rows++)
{
element1 = rand() % 100000 + 1;
int len = rand () % 4 + 4;
word [len] = 0;
while (len) wor开发者_如何学God [--len] = 'A' + rand () % 58;
myfileS<<element1<<word;
myfileS<<"|";
}
myfileS<<endl;
}
myfile.close();
system("pause");
return 0;
}
Thanks for your help!
SOLUTION OF MY DOUBT
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>
#include<ctime>
#include<sstream>
using namespace std;
int main()
{
int rows, columns, rowsMax, columnsMax;
int element1;
char word[10];
int a=0;
cout<<"Write the number of rows of your table: ";
cin>>rowsMax;
cout<<"Write the number of columns of your table: ";
cin>>columnsMax;
int answer;
cout<<"Do you want to create the transposed table??? (1 -> yes, 2 -> no): ";
cin>>answer;
string matriz[7][5]; // string matriz[rowsMax][columnsMax]; I should modify this in every query
string table ("Table1.txt");
ofstream myfile (table);
if(myfile.is_open())
srand(1);
myfile<<id<<"|"<<type<<"|"<<columnsMax<<endl;
for(rows=0;rows<rowsMax;rows++)
{
for(columns=0;columns<columnsMax;columns++)
{
element1 = rand() % 100000 + 1;
int len = rand () % 4 + 4;
word [len] = 0;
while (len) word [--len] = 'A' + rand () % 58;
myfile<<element1<<word;
myfile<<"|";
std::stringstream ss;
ss<<element1;
string mat;
mat +=ss.str();
mat +=word;
matriz[rows][columns]= mat;
}
myfile<<endl;
}
myfile.close();
while(answer==1)
{
string table ("Table");
table +=id;
table +="(transposed)";
table +=".txt";
ofstream myfile (table);
if(myfile.is_open())
myfile<<id<<"|2|"<<columnsMax<<endl;
for(int k=0;k<5;k++)
{
for(int j=0;j<7;j++)
{
myfile<<matriz[j][k]<<"|";
}
myfile<<endl;
}
answer=2;
}
system("pause");
return 0;
}
to create a transposed version of file1.txt, you'll have to keep its content in memory and visit that content in a transposed way.
when you write
if(myfileS.is_open()) srand(time(0));
you just conditionalize the srand of the fact that the file is open or not. Not what follows. I'd put braces around everything.
the best way to test the status of an IOStream is just to use it as a condition
if (myfileS)
精彩评论