“Transposed .txt file" with random characters, is it possible? [duplicate]
Possible Duplicate:
Creating “transposed” files with random characters
Hey people,
I am writing a program that creates ".txt" files with random characters, and I want to create transposed ".txt" files with the same data that was writen previously in a random way. Here I add an example of the two ".txt" files as I intend to do. Think that every letter or number is a random string, for example "A" would be "ih65ds8ds69"
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|
Thes most simmilar thing I've got with the program I attach is:
FILE2 (FILE1 transpossed)
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|
So now I attach the code I have writen so that you can have a look and give me some ideas about how modify it to get what I intend.
#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)
sr开发者_开发知识库and(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) word [--len] = 'A' + rand () % 58;
myfileS<<element1<<word;
myfileS<<"|";
}
myfileS<<endl;
}
myfile.close();
system("pause");
return 0;
}
Thank you very much for all your help!!! :D
Use a 2-dimensional array -- and change the order of printing :).
Perhaps the easiest thing to do would be to load the strings from the input file into a 2-dimensional array (MxN). Then, creating the output file would involve simply iterating down the columns and across the rows.
Your code seems to generate random data for file 1 and file 2. Please post the code that generates the output that you posted. Without seeing what you have done, it's difficult to give you any specific advice.
EDIT: Nevermind, I missed the srand(1)
that you were using to force the same random data to be generated in both loops.
Instead of generating random data and immediately printing it to a file, fill a MxN array with random data values. Then, iterate through that array twice, printing the data as you go. One loop will iterate through columns before rows and the other rows before columns.
精彩评论