Read a file and get specific characters from a string using C++

I have two files, fileA and fileB. I want to get the 3rd to 13th digit from fileA and should be checked against all ranges in fileB.

fileA:

500000012345000 xxx
500000123456000 xxx
500000034567000 xxx

fileB:

12345,20000
34565,34568

output must be:

500000012345000 xxx
500000034567000 xxx

I am new to C++ so I only know how to read one file:

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
  #include <iostream>
  #include <fstream>
  #include <string>
  #include <conio.h>
  using namespace std;

  int main ()
  {
    string STRING;
    ifstream infile;
    infile.open ("E:\\sample_exts\\fileA.txt");
    int a=0;
    string previousLine="";
    while(a<1) // To get you all the lines.
   {


    getline(infile,STRING); // Saves the line in STRING.
    if (STRING != previousLine)
    {
        previousLine=STRING;
        cout<<STRING<<endl; // Prints our STRING.
    }

}
infile.close();
system ("pause");
getch();
}
To read from a second file, all you need to do is create another ifstream variable, and open a file. Its exactly the same as infile but another name. e.g:
1
2
3
4
5
6
7
8
9
10
ifstream infile;
ifstream anotherinfile;

infile.open ("E:\\sample_exts\\fileA.txt");
anotherinfile.open ("E:\\sample_exts\\fileB.txt");

//you can now read from infile, or anotherinfile.

infile.close();
anotherfile.close();


You can do this with more files if you wish, you just need to change the variable name (infile, anotherinfile, ect).

I'm going to point you to these pages for finding the info on how to check strings against each other.
Just ask if you need more help.

http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/string/string/compare/
http://www.cplusplus.com/reference/string/string/find/
Last edited on
Topic archived. No new replies allowed.