Find Line Substring

I have a TextFile which is full of junk lines.
Lets say it looks something like this

[File/Name/{123-321-123}]
sjf2ij14ask1365t
421ij412y325sd
531v4ac4sdas6t46b14as31
5v2341v44aw4126fd72345
[File/Name/{321-123-321}]
sjf2ij14ask1365t
4easwfasdawe31251asd
531v4ac46qqwtd21t46b14as31
5v2341v44aw4126fddfase72345
[File/Name/{231-123-321}]
41cdasf351fdas
241sagr46t23cvd
214asfgv52d1fc



Soo I want to rewrite Lines which has words like "Name" "File" into another File.
I tired myself making this , but it seems that I dont understand fstream,ofstream and ifstream enough.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ifstream read("File.txt",ios_base::app);
ofstream write("File2.txt", std::ios::app);
string line;
string currentLine;
string ID="Name";
if (read.is_open()) {
	while (!read.eof()) {
		getline(read, line);
		currentLine= line;
		size_t found = currentLine.find(ID);
		if (found != string::npos)
		write<<currentLine<< endl;
			}
		}

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

int main()
{
   //ifstream in( "File.txt" );
   //ofstream out("File2.txt" );
   istringstream in( "[File/Name/{123-321-123}]\n"
                     "sjf2ij14ask1365t\n"
                     "421ij412y325sd\n"
                     "[File/Name/{321-123-321}]\n"
                     "sjf2ij14ask1365t\n"
                     "4easwfasdawe31251asd\n"
                     "[File/Name/{231-123-321}]\n"
                     "41cdasf351fdas\n" );
   ostream &out = cout;                     
   string ID="Name";
   
   for ( string line; getline( in, line ); )        
   {
      if ( line.find( ID ) != string::npos ) out << line << '\n';
   }
}

[File/Name/{123-321-123}]
[File/Name/{321-123-321}]
[File/Name/{231-123-321}]
This only works if I put manually Data as string.
It is needed for program to read File.txt (line by line) and if it finds String or Substring "name" to transcript that line into File2.txt
This only works if I put manually Data as string.


Look carefully.

I commented out two lines with streams referring to files.
I (temporarily) replaced them with streams allowing you to run them in cpp.sh and see the output.

I'm sure that you can go back to filestreams without thinking too hard ...
Yes I understand what you thought.But only works if I use StringStream not with Ifstream.
Novak323 wrote:
Yes I understand what you thought.
No, you clearly don't.


Just un-comment the two lines with ifstream and ofstream

Then comment out the lines associated with istringstream and ostream.

Then, lo and behold, you will no longer be using your stringstream nemesis and you will have stream "in" attached to an input file and "out" attached to an output file.

What's the problem with that?
Last edited on
But only works if I use StringStream not with Ifstream.

What do you base that claim on? (for it is false)


Overall, why do you write a whole program for a task like that? There are programs that already do that:
grep Name File.txt > File2.txt
Topic archived. No new replies allowed.