input integer from text file and output text file

I want to input my integer data from a text file and then my program will run and the final result of this program will generate an output.txt file.

You can run this program first time manually. This is a simple sorting problem.

My program is working well if i input manually but now just want to input from a text file. If anyone can, please help me.


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
#include <iostream>
#include <array>
#include <vector>
#include <utility>

int main()
{
    std::vector< std::array<int,2> > v;
    std::size_t n;
    if (std::cin >> n && n > 0)
    {
        std::array<int,2> row;
        while (n-- && std::cin >> row[0] && std::cin >> row[1])
            v.emplace_back(row);

        // bubblesort the content
        std::size_t len = v.size();
        while (len-- > 0)
        {
            bool swapped = false;
            for (std::size_t i=0; i<len; ++i)
            {
                // std::array support multi-cell comparison.
                if (v[i] < v[i+1])
                {
                    // use library swap to swap entire row.
                    std::swap(v[i], v[i+1]);
                    swapped = true;
                }
            }

            // early exit if no swaps happened on the last pass
            if (!swapped)
                break;
        }

        // report final output.
        for (auto const& row : v)
            std::cout << row[0] << ' ' << row[1] << '\n';
    }
}


This is my sample input. Here 8 is the array size. And below 8 pair is the numbers of 2d array.

1
2
3
4
5
6
7
8
9
8
41 11
34 4
69 4
78 6
62 8
5 5
81 3
5 10


And this is my sample output

1
2
3
4
5
6
7
8
81 3
78 6
69 4
62 8
41 11
34 4
5 10
5 5
look at an example using <fstream>
or, the 10 cent version:

ifstream file("path\\filename.txt");
while (file >> number)
matrix[index++] = number; // One dimensional array example.
Consider:

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
#include <iostream>
#include <array>
#include <vector>
#include <utility>
#include <fstream>

int main() {

	std::ifstream ifs("input.txt");
	std::ofstream ofs("output.txt");

	if (!ifs || !ofs)
		return (std::cout << "Cannot open files\n"), 1;

	std::size_t n {};

	if (ifs >> n && n > 0) {
		std::vector< std::array<int, 2> > v(n);

		while (n-- && ifs >> v[n][0] && ifs >> v[n][1]);

		// bubblesort the content
		auto len {v.size()};

		while (len-- > 0) {
			bool swapped {};

			for (std::size_t i = 0; i < len; ++i)
				// std::array support multi-cell comparison.
				if (v[i] < v[i + 1]) {
					// use library swap to swap entire row.
					std::swap(v[i], v[i + 1]);
					swapped = true;
				}

			// early exit if no swaps happened on the last pass
			if (!swapped)
				break;
		}

		// report final output.
		for (auto const& row : v)
			ofs << row[0] << ' ' << row[1] << '\n';
	}
}

here show error on my ide. please see

http://prntscr.com/z0frkv

I use codeblocks
Last edited on
There's no way this guy wrote the original program.
but my ide is can not run this program
find the flags to use a newer language version or get a new IDE. 2011 was 10 years ago -- not supporting this is no longer optional or acceptable.
here is the details of my ide http://prntscr.com/z0kvn5 also check this http://prntscr.com/z0kzan
please can you sort my input.txt file and send it a text file to me?
https://drive.google.com/file/d/1JbOybnS3e4Rd82mY2DA2mLy8VYsyKUvk/view?usp=sharing
Thank you code is perfectly running. I solved my ide problem
Topic archived. No new replies allowed.