finding a line number in text file and replacing it?

Ok I have two text files of 20 different numbers called test and test2. So what I want to do is generate 5 random numbers 1-20 (which i did) and these numbers are line numbers in the text files. So for example if it generates 3, 6, 8, 9, 15 these are all line numbers in the text files. Then I want to swap the numbers at those lines in the text files. So for example if the number 16 was in line 3 in test.txt then it would swap with the number 2 in line 3 in test2.txt. Then I am trying to print the swaps in a new text file called fileout.txt. I'm very confused on how to go about this and I've tried many different things. Any advice you could give me on swapping these numbers is appreciated! Below is my file where I generate the numbers and read in the two files

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
	int n;
	int counter = 0;
	srand(time(NULL));

	while (counter != 5)
	{
		for (n = 1; n < 2; n++)
		{
			n = rand() % 20 - 1;
			cout << n << endl;
		}
		counter++;
	}
	ifstream filein("test.txt");
	ifstream filein("test.txt");
	ofstream fileout("fileout.txt");



	system("pause");
}
Last edited on
The most easiest way would be to read data from both files into seperate vectors like vec_test1 and vec_test2

Once you have all the information in the vectors you can swap them however you want and once you have done what you wanted close the ifstreams and open ofstreams for exactly the same filenames and overwrite the original files.

During the swapping process keep the information what exactly you are swapping so once its done you can use this information to create "fileout.txt" that would represent those swaps.
Hello! Thanks for the quick reply. Is there anyway to do this without vectors? I'm not sure what they are
Hi, sry for late reply :D
Hard to tell about another way, probably there is but I don't really know every way iostreams can be used.

I told you this because I feel like this really is the easiest. Vectors are not that hard to use.
Just read this and see the example in the end and you should more or less understand how to use it
http://en.cppreference.com/w/cpp/container/vector

Its similar like build in array but it can change its size (usually using push_back(...) member function) and it keeps track of its size (using member function size())
You can access individual elements exactly like elements in a build in array using operator[]()

So just create an empty vector and read every those numbers from file. Once you read a number just add it to vector using push_back for example
1
2
3
4
5
6
7
std::ifstream ifs{"text.txt"};

std::vector<int> numbers;
for(int n; ifs >> n;)
{
        numbers.push_back(n);
}


After this loop your vector is filled with all those int numbers in your file
Last edited on
thanks again I'll try it your way with vectors! :) I understand it better now that you explained it
Last edited on
Topic archived. No new replies allowed.