ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I wrote a program that creates a text file with some letters using ofstream. Now I would like to format the text so that the letters are written in groups of 5 and the rows are all the same length( i.e. LINUXQUESTIONS becomes LINUX QUEST IONS). To do this the fprintf function seemed the most appropriate. in the following example I use integers, but it is just an example.
Code:
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdio>
using namespace std;
int main()
{
ofstream f;
f.open("testo.txt");
for(int i = 0; i < 21; i++)
{
f << i;
}
f.close(); //to simulate file already created
FILE *file;
int number;
file = fopen("testo.txt", "r+");
if (file==NULL) perror ("Error opening file");
else
{
do {
number = fgetc (file);
fprintf(file, "%-5.5i", number);
} while (number != EOF);
}
fclose(file);
return 0;
}
can this method work? or there is some function that I can use directly when creating the file with ofstream?
please help
or is there some function that I can use directly when creating the file with ofstream?
surely you could just write five characters, write a space, write another five, and so on until you run out of characters? That would be far easier than messing with the file after it's been created. The code you have there doesn't work (as you hopefully know); if you did want your approach to work, you'd have to use a temporary file (you're currently doing weird things by reading and writing) and you would again count five characters and then add a space, rather than playing with padding integers...
In the vein of the first suggestion, the following works:
Code:
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdio>
using namespace std;
int main() {
//Your code:
ofstream f;
f.open("testo.txt");
for(int i = 0; i < 21; i++) {
f << i;
}
f.close(); //to simulate file already created
//My code:
FILE *in;
FILE *out;
in = fopen("testo.txt", "r");
out = fopen("out.txt", "w");
if (in==NULL) {
perror ("Error opening file");
} else {
char character;
do {
for(int i=0;i<5 && character != EOF;i++){
character = fgetc (in);
fprintf(out, "%c", character);
}
fprintf(out, " ");
} while (character != EOF);
}
fclose(in);
fclose(out);
return 0;
}
I used C-style file input/output because you did (though not for the first bit?), but it would be "better" to use C++ style I/O if you're using C++ (which you are if you're using string)
Hope this helps,
Last edited by Snark1994; 09-21-2011 at 10:55 AM.
Reason: Strange indentations...
surely you could just write five characters, write a space, write another five, and so on until you run out of characters? That would be far easier than messing with the file after it's been created.
this is the piece of original code
Code:
void Vigenere::encode(string plaintext, string encoded_f){
ofstream f;
TextErase();
LoadText(plaintext); //load a text file in text (vector<string>)
f.open(encoded_f.c_str());
if(!f.is_open()){
cout << "Error creating file: " << encoded_f << " !" << endl;
return;
}
int* index_k = new int[key.length()]; //key indexs array
for(int i = 0; i < key.length(); i++){
for(int j = 0; j < 21; j++){
if(key[i] == ALPH[j])
index_k[i] = j;
}
}
int passcnt = 0;
for(int i = 0; i < text.size(); i++){
for(int j = 0; j < text[i].size(); j++){
int* index_t = new int[text[i].length()];
for(int k = 0; k < 21; k++){
if(text[i][j] == ALPH[k])
index_t[j] = k;
}
char ch = ALPH[(index_t[j] + index_k[passcnt])%21];
f << ch;
passcnt++;
if(passcnt > (key.length() - 1))
passcnt = 0;
delete[] index_t;
}
f << "\n";
}
f.close();
delete[] index_k;
return;
}
how can I change this to do what you say? by the way, you're completely right, this way seems to be easier, but after a day spent at the computer, the brain no longer works well...
I will admit to being both rather tired and in a state of mild intoxication, so I will have another look at it tomorrow... However, it looks like if you change it to:
Code:
void Vigenere::encode(string plaintext, string encoded_f){
// ...
int numberOfCharsPrinted = 0;
int passcnt = 0;
for(int i = 0; i < text.size(); i++){
for(int j = 0; j < text[i].size(); j++){
// ...
char ch = ALPH[(index_t[j] + index_k[passcnt])%21];
if (numberOfCharsPrinted == 5){
f << ' ';
numberOfCharsPrinted = 0;
}
f << ch;
passcnt++;
numberOfCharsPrinted++;
// ...
return;
}
...where "..." represents code which should be included but which I've omitted to make the changes that I've made more clear
Right, all you need to do is put another "numberOfCharsPrinted = 0;" after "f << '\n';"; if you look at your output it is putting a space after every 5 characters, but sometimes the linebreak occurs in the middle. Setting numberOfCharsPrinted to 0 after every linebreak means everything will come out in groups of 5.
Don't worry about asking questions, it is a real pleasure to be able to help someone, especially someone who's polite
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.