C++ working with .txt file

Hello everyone, I'm having problems working with .txt files in C++..
In the C++ main function the name of an input .txt file shall be inputted, the name of an output file .txt as well as the desired width of the output block column format.
I'm able to input the file and print it out, but i can't set the width.. I imagine i should give the option to input the width from the start and then save the characters in the string according on the given width.. but since I'm quite new to c++ I don't actually know how to do it.. Hope someone can help me..
Thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <string>
#include<iomanip>

using namespace std;

int main(void)

{   char c;
    ifstream myfile;
	string fileLocation;
    cout << "Please enter the location of the file: ";
	getline(cin, fileLocation);
	myfile.open(fileLocation.c_str());

if (!myfile.is_open())
{
    cerr<<"error opening file"<< fileLocation<< endl;
}
else {
    cout<< "input file " << fileLocation<< "opened.." << endl;
   
    while(!myfile.eof())
    {
        c = myfile.get();
        cout.put(c);


    }
    myfile.close();
    cout<<"\n input file closed" << endl;

}
return 0;
}
By width do you mean the distance (in spaces) from one element to another? If that's the problem, you can use iomanip to format the spaces as you wish.
I mean the number of letters and spaces together.. For example the sentence "Today it is sunny" has width 17. I've tried the setw function, but it doesn't actually do what i want..
I used it like this:
1
2
cout << setw(40) <<  end l;
cout.put(c);

Use the string size from that, then subtract it from the total number of spaces you want to have. Of course, the string's length shouldn't be greater than the desired number.

std::cout<<std::setw(40-line.length)<<'\n'
The problem is that i don't know how many there are going to be from the start..
The users inputs a random text, and the desired width per line, the program should output it in that block format.. I'll post you the original task, maybe it will help you understand what I mean
Texts in newspapers, journals, books or also on screens often are given in (several) columns in block format, i.e. left and right adjusted and with if possible equal spaces in between the words of each line. Task here is to program a simple file based ASCII text block formatter.

In the C++ main function the name of an input file (file with ending .txt) shall be inputted, the name of an output file (file with ending .txt) as well as the desired width of the output block column format.

Following in a loop read the input file character by character, most senseful at least as many characters as specified by the block width. Then compute all needed information and output the newly formatted output text line into the output file. Count for example the number of read words, characters and white spaces of the newly to print text line. Calculate the number of needed spaces in between the single words of a line and insert - except before the last word - same number of spaces in between the words (i.e. don't fill all missing spaces in front of the last word of a line (see examples).

Since the input text might be arbitrary long and especially longer than bytes in the main memory exist do NOT read first the whole complete input text into a single array or a list of characters but work (output) line by line!
Input char by char then, use a counter variable, then input '\n' or setw when the counter is full, reset counter to 0, repeat. Use 2 counters, one for width, another one for length.

Last edited on
Could you please give me an example? I'm sorry if I'm disturbing, but as I said, I'm new to coding and I sometimes don't even know where to sart from..
Just make sure the strings are the same lenght and it will work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
std::string text = "Lorem ipsum, my country is Romania";
std::string text2 = "Lorem ipsum, my country is Romania";
int j = 0;

  for (auto i = 0; i != text.length(); ++i)
	{
		std::cout << text[i];
		++counter_wid;

		if (counter_wid == 4)
		{
			counter_wid = 0;
			std::cout <<"   ";

			while (counter_wid<4)
			{
				std::cout << text2[j];
				++j;
				++counter_wid;
			}

			counter_wid = 0;
			std::cout << '\n';
		}
		
	}

	std::cout <<"    ";
	for (; j < text2.length(); ++j)
		std::cout<< text[j]; //print the rest from text2 if it exists 

Last edited on
This confused me even more.. Anyway it doesn't matter..
Are you from romania?
Yes, I'm from Romania. How have you figured it out? *sarcasm* :P
Last edited on
Luck?

"Probabil ma descurc mai bine cu cititul decat cu codatul" :))
I don't know if I'm allowed to use another language than English here, but meh :P

Ma cam grabi cu codul, dar in genere merge. Logica e cam asa, foloseste o variabila sa vezi monitorizezi cate caractere ai pus in bloc pana acum, cand ai ajuns la limita care ai vrut-o, apoi insereaza spatiul si treci la blocul urmator. La blocul urmator poti sa folosesti alta variabila si alt spatiu maxim daca vrei. Dupa ce ai terminat cu blocul urmator baga newline, si iarasi incepi cu o linie noua.

Codul asta il facui acuma pentru texte egale, doar poate fi modificate daca ai doua texte inegale, sau poti pur si simplu sa adugi text gol sa le faci egale.

Sa ghicesc, tema la facultate? :)))

Last edited on
I don't know about that either so I'll keep writing english or you can pm me..
Btw yes, university task, I have time until tomorro 00.00 and I don't know how to do it, or better said, I know the theory about how, but I can' make it work, I don't even know how should I keep going on:))
You need to enable private message from your profile, otherwise you can't receive any.
I can now:D
Topic archived. No new replies allowed.