Need help with getline - How to read the previous line?

Hi

I need some help to get the below code modify to get the BOLD lines goes to 7 files. I came up with the below code but It did not work as expected. Note that last line comes with "CutHere" line but it does not matter.


My problem is that I expect it to generate 7 files (Desfile_1 - Desfile_7) but it only generate 4 files - 4 files with bold & italic marked lines.

I need to find a way to generate 3 more files with other BOLD lines.

It seems, it skips one set due to code line - "}while ((offset2 = line.find("CutHere", 0)) == string::npos);".

I am not sure how to read from the just one before (previous) line after creating 1st file in order to avoid skipping one set alternatively.

Any ideas to get the below code modified to generate 7 files???

Thanks in advance.

Contents of file "test";

aaaa
CutHere
bbbb
cccc
dddd

CutHere
eeee
ffff

CutHere
gggg
CutHere
hhhh
kkkk
oooo

CutHere
pppp
aaaa
xxxx

CutHere
ffff
CutHere
uuuu
llll
ssss
aaaa
qqqq
yyyy

22 CutHere


My code;

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
63
64
65
66
67
68
69
70
71
72
73
#include <fstream>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <sstream>
#include <time.h>
#include <dirent.h>
#include <ctype.h>


using namespace std;
void CutToFiles(string file);

int main ()
{

CutToFiles("test");

return 0;
}


void CutToFiles(string file)
{

unsigned long offset1;
unsigned long offset2;

string line;


int filecounter=0;

ifstream fileToSearch;

fileToSearch.open (file.c_str());
if(fileToSearch)
 {
        
        while(getline(fileToSearch,line))
        {
          
          if ((offset1 = line.find("CutHere", 0)) != string::npos)
                {
                        filecounter++;

                        std::stringstream filecounterSTR;
                        filecounterSTR << filecounter;

                        string Desfile="Desfile_"+filecounterSTR.str();

                          do
                          {
                                getline(fileToSearch,line);

				ofstream PutToFile;
				PutToFile.open (Desfile.c_str(), ios::app);
				PutToFile<<line<<endl;
			        PutToFile.close();

                          }while ((offset2 = line.find("CutHere", 0)) == string::npos);

                }
        }
 }



}



Mathew
Last edited on
Roughly the same logic, structured a bit differently:
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
std::string fine_name( const std::string& prefix, int n )
{
    std::ostringstream stm ;
    stm << prefix << n << ".txt" ;
    return stm.str() ;
}

inline bool not_cut_here( const std::string& line )
{
    static const std::string cut_here = "CutHere" ;
    return line.find(cut_here) == std::string::npos ;
}

void cut_2_files( const std::string& srce, const std::string& prefix_dest )
{
    std::ifstream file_in( srce ) ;
    std::string line ;

    // skip the first block
    while( std::getline( file_in, line ) && not_cut_here(line) ) ;

    for( int fn = 1 ; file_in ; ++fn ) // loop till eof
    {
        // open the next output file
        std::ofstream out_file( fine_name( prefix_dest, fn ) ) ;
        // write lines one by one till cut_here
        while( std::getline( file_in, line ) && not_cut_here(line) )
            out_file << line << '\n' ;
    }
}
Hi JLBorges,

Thanks. It works. But it creates empty file which is the last file.
I tried hard to add that code & none of them worked.

Any way to stop that?

Thanks in advance.

Mathew
Something like this would perhaps be the simplest:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// ...

for( int fn = 1 ; file_in ; ++fn ) // loop till eof
{
    std::vector<std::string> lines ;
    // add lines one by one till cut_here
    while( std::getline( file_in, line ) && not_cut_here(line) )
        lines.push_back(line) ;

    if( !lines.empty() ) // if there is atleast one line
    {
        // open the next output file
        std::ofstream out_file( fine_name( prefix_dest, fn ) ) ;
        // and write out the lines
        for( const std::string& s : lines ) out_file << line << '\n' ;
    }
}

Topic archived. No new replies allowed.