Printing text from text

Hello people !

I have a problem with a exercise I have to end. I have been searching for long so I finally ask for your help...

I must write a program that prints text from a given text file (anyone) to an other text file, with some conditions on the second one:

Each row has to contain between 10 and 80 symbols
'Print out the row count in first file and file 2'.

Thanks for you help,
Alex
Alright, can you show us what you have done so far?

Post your code, and make sure to do it between code tags - http://www.cplusplus.com/articles/jEywvCM9/
Hello,

I've made some tries, but there is always a problem, there is nothing in the second file and the first one is changed in wrong way. At the moment i made this:

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
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream inputFile ("my_file.txt",ios::in|ios::out);
    ofstream outputFile ("my_file2.txt",ios::out);
	char s;
	int k=0;
	inputFile.get(s);
	while (inputFile)
	{
        while (s!='\n')
        {
            inputFile.get(s);
        }
        if (s=='\n')
        {
            ++k;
            inputFile <<" "<< k;
        }
        inputFile.get(s);
	}
	while (!inputFile && !inputFile.eof())
    {
        inputFile.clear();
        inputFile.get(s);
        outputFile << s;
    }
	while (inputFile)
	{
	    inputFile.get(s);
        outputFile << s;

        while (!inputFile && !inputFile.eof())
        {
            inputFile.clear();
            outputFile << s;
            inputFile.get(s);
        }
	}
	outputFile.close();
	inputFile.close();
	cout<<"finished!"<<endl;
	return 0;

}


I made some modification and actually nothing happen. I'm disapointed :/
Last edited on
Could you post the text of the exercise.
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
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream inFile{"D:\\input.txt"};
    std::ofstream outFile{"D:\\output.txt"};

    size_t inputLines{};
    size_t outputLines{};
    if (inFile)
    {
        std::string line{};
        while(getline(inFile, line))
        {
            ++inputLines;
            if((line.size() > 10 && line.size() < 80))
            {
                if(inFile)
               {
                    ++outputLines;
                    outFile << line << "\n";
                }
            }
        }
    }
    std::cout << "Number of lines in input file: " << inputLines << "\n";
    std::cout << "Number of lines in output file: " << outputLines << "\n";
}
Sample File
this is a long line
short
this is a long line
short
this is a long line
short
this is a long line
short
this is a long line
short
this is a long line
short
this is a long line
short
this is a long line
short
this is a long line
short
this is a long line
short
Output
this is a long line
this is a long line
this is a long line
this is a long line
this is a long line
this is a long line
this is a long line
this is a long line
this is a long line
this is a long line
1
2
Number of lines in input file: 20
Number of lines in output file: 10
Last edited on
Thanks you !

Problem solved with this
Topic archived. No new replies allowed.