I'm new here. Trying to do something I think should be easy but can't get to work. I have two files which have just simple data in
FileA
KIC 757137 892010 892107 892738 892760 893214 1026084 1435467 1026180 1026309 1026326 1026473 1027337 1160789 1161447 1161618 1162036 3112152 1163359 1163453 1163621 3123191 1164590 and File B
KICID 1430163 1435467 1725815 2309595 2450729 2837475 2849125 2852862 2865774 2991448 2998253 3112152 3112889 3115178 3123191 � I'd like to read both files, and then print out the values that are the same, and ignoring titles. In this case I'd get that 1435467 3123191 are in both, and just these would be sent to a new file. so far I have
#include <cmath> #include <cstdlib> #include <string> #include <iomanip> #include <iostream> #include <fstream> #include <ctime> using namespace std; // Globals, to allow being called from several functions // main program int main() { float A, B; ifstream inA("FileA"); // input stream ifstream inB("FileB"); // second instream ofstream outA("OutA.txt"); // output stream while (inA >> A) { while (inB >> B) { if (A == B) { outA << A << "\t" << B << endl; } } } return 0; } And this just produces an empty document OutA I thought this would read a line of FileA, then cycle through FileB until it found a match, send to OutA, and then move onto the next line of FileA Any help would be appreciated?
inBto the start of the file for eachA. And skip over the titles before you start reading numbers.inB.seekg(0, std::ios_base::beg);to reset the file pointer to the begining of the file every time you would like to match a number. Or much better you could read the data of one file in a structure (eg:std::set) and read the second trying to match if exist or not. In this case you only need to read the files (both one time). Disk access is a really expensive operation.