Writing to file

ghjnmghjmfhj
Last edited on
1. Indentation matters.
https://en.wikipedia.org/wiki/Indentation_style

People should be able to just look at the code and know from the shape of it just which bits of code are controlled by which loops, conditionals etc.

If you have to work at all at mentally parsing the code just to figure this out, you'll make mistakes.

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 <string>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{
  ifstream file1;
  ifstream file2;
  ofstream file3;
  ofstream file4;
  string str2;
  file1.open("input.txt");
  file1.open("source.txt");
  file2.open("output file.txt");
  file3.open("Not Found.txt");

  if (file1.is_open()) {
    while (!file1.eof(), !file2.eof()) {
      string str1;
      getline(file1, str1);
      stringstream sstr2(str1);
      string name1, name2, Key2, ExamNum2, ExamYear3;
      sstr2 >> name1 >> name2 >> Key2 >> ExamNum2 >> ExamYear3;

      getline(file2, str2);
      string str2;
      stringstream sstr1(str2);
      string SearchName, SearchName2, Key, ExamNum, ExamYear;
      sstr1 >> SearchName >> SearchName2 >> Key >> ExamNum >> ExamYear;

      if (Key2 == Key) {
        file3 << SearchName << SearchName2
              << Key << ExamNum << ExamYear
              << " match found " << endl;
        return (1);
      } else {
        file4 << "In line" << str1 << " there is no a match of: " << Key << endl;
        return (1);
      }
    }
  }
  return 0;
}


There are four big issues.
1. eof() does not mean what you think it means.
2. The comma operator in line 20 does not mean what you think it means.
3. the str2 on line 27 which you read into is NOT the str2 you construct a stringstream with on line 29. Look at line 28 and tell me what it's doing there.
4. You have return in both the if and else at lines 37 and 40. Regardless of anything else, all of this is happening exactly once.
It looks like you need to swap lines 27 and 28. Compare your line order for "str2" to that of "str1". Yeah, we've all made this mistake--one that tired eyes often don't spot.
let me just say i want to build up on the code below,instead of just using a single given string. it reads from a whole file "source" for all the matchs there is and then writes them to a output file.........

this code runs just fine
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
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{
      ifstream file1;
      ofstream file2;
      ofstream file3;
      file1.open("input.txt");
      file2.open("output file.txt");
      file3.open ("Not Found.txt");
      stringstream sstr1("Kennedy chewe 7483289 20174893 2017"); //Key string
      string SearchName,SearchName2,Key, ExamNum,ExamYear;
      sstr1 >>SearchName>>SearchName2>>Key>>ExamNum>>ExamYear;

      if(file1.is_open()) {
      while(!file1.eof()) {
      string str1;
      getline(file1, str1); //input string
      stringstream sstr2(str1);
      string name1, name2,Key2, ExamNum2,ExamYear3;
      sstr2 >>name1>>name2>>Key2>>ExamNum2>>ExamYear3;
      if (Key2 == Key) { //Comparison
      file2 << SearchName << SearchName2 << Key << ExamNum << ExamYear <<" match found " << endl;
      return (1);
      }
      else {
      file3 << "In line"<<str1<<" there is no a match of: " << Key << endl;
      }
    }
  }
   return 0;
}
You still need to work on your indentation.

Calling variables file1, file2, file3 isn't very meaningful.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;

struct person {
  string firstname;
  string surname;
  string gender;
  int    id;
};

int main()
{
  ifstream input;
  ifstream source;
  //ofstream output;
  //ofstream notfound;
  input.open("input.txt");
  source.open("source.txt");
  //output.open("output file.txt");
  //notfound.open("Not Found.txt");


  // foreach line of input....
  string input_line;
  while( getline(input,input_line) ) {
    stringstream i_line(input_line);
    person input_person;
    i_line >> input_person.firstname
           >> input_person.surname
           >> input_person.gender
           >> input_person.id;

    // search the source for a matching name
    source.clear();
    source.seekg(0);
    bool found = false;
    string source_line;
    while ( !found && getline(source,source_line) ) {
      stringstream s_line(source_line);
      person source_person;
      s_line >> source_person.firstname
             >> source_person.surname
             >> source_person.gender
             >> source_person.id;
      // found them!
      if ( source_person.firstname == input_person.firstname ) {
        found = true;
      }
    }

    // and output...
    if ( !found ) {
      cout << "No match for " << input_person.firstname << endl;
    } else {
      cout << "Matched " << input_person.firstname << endl;
    }
  }
  return 0;
}

$ g++ foo.cpp
$ ./a.out 
Matched Lucy
Matched Mark
Matched john
Matched Lisa
Matched Chitalu
No match for Frank
Matched Malika
Matched Raymod
Matched Lucy
Matched Jack
No match for Oka
Matched Emmanucle
Matched Brian
Matched Lisa
Matched Prince
Topic archived. No new replies allowed.