?
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Hi all,
I decided to refresh my memory and program a C++ program which reads a
text-line from an input file until eof and at the same time it writes
this line to an output file (it copies the file 1 line at a time). That
works. Now I would like to skip some of the lines in the original file
which looks something like:
-----
....
OTHER KIND OF LINES HERE...
TEXT, 100, 54, 43, 32, 21
TEXT, 101, 54, 43, 32, 21
TEXT, 102, 12, 11, 12, 11
TEXT, 103, 54, 43, 32, 21
TEXT, 104, 54, 43, 32, 21
OTHER KIND OF LINES HERE...
....
-----
The 4 last numbers are "random". The first number after "TEXT" is a
"line number" (not really line number, but anyway) and some line numbers
determine whether or not a whole line should be excluded from the copy
(column 2). Example:
Suppose I want to delete the line with number 101 in column 2. Then line
102 should become line 101, line 103 become 102 etc. etc (i.e. I want to
"shift" the line-numbers). The above input file would look like this in
the output file:
-----
....
OTHER KIND OF LINES HERE...
TEXT, 100, 54, 43, 32, 21
TEXT, 101, 12, 11, 12, 11
TEXT, 102, 54, 43, 32, 21
TEXT, 103, 54, 43, 32, 21
OTHER KIND OF LINES HERE...
....
-----
See it? The line numbers was re-ordered... Can anyone help me a bit with
that? I think it should be pretty easy, somehow...
Here's the part of my code I want to change:
------------------------------------------
unsigned int line, n1, n2, n3, n4;
/* this can be a list (growing always) */
unsigned int delete_line_array = [101,150,155];
string txt;
linenumber = 0; /* reset */
unsigned int deleted_lines = 0; /* nothing deleted yet */
while( getline( infile, read_line ) )
{
linenumber++;
if( read_line.find("TEXT,") == 0 ) /* BEGINNING OF LINE */
{
//cout << "Found at line" << linenumber << endl;
//cout << read_line << endl;
// I need something like this, I think:
read_line >> txt >> line >> n1 >> n2 >> n3 >> n4;
if (line != delete_line_array[deleted_lines]) /* COPY LINE */
/* NO: outfile << read_line << endl; */
outfile << txt << (line-deleted_lines) ...
<< n1 << n2 << n3 << n4
else
/* DON'T WRITE THIS LINE OUT AND
ALSO SHIFT LINENUMBERS AFTER */
deleted_lines++; /* increment counter */
end
}
else
{
outfile << read_line << endl; /* COPY LINE TO OUTFILE */
}
}
infile.close();
------------------------------------------
Your help is appreciated... TIA.
Regards,
Martin
I decided to refresh my memory and program a C++ program which reads a
text-line from an input file until eof and at the same time it writes
this line to an output file (it copies the file 1 line at a time). That
works. Now I would like to skip some of the lines in the original file
which looks something like:
-----
....
OTHER KIND OF LINES HERE...
TEXT, 100, 54, 43, 32, 21
TEXT, 101, 54, 43, 32, 21
TEXT, 102, 12, 11, 12, 11
TEXT, 103, 54, 43, 32, 21
TEXT, 104, 54, 43, 32, 21
OTHER KIND OF LINES HERE...
....
-----
The 4 last numbers are "random". The first number after "TEXT" is a
"line number" (not really line number, but anyway) and some line numbers
determine whether or not a whole line should be excluded from the copy
(column 2). Example:
Suppose I want to delete the line with number 101 in column 2. Then line
102 should become line 101, line 103 become 102 etc. etc (i.e. I want to
"shift" the line-numbers). The above input file would look like this in
the output file:
-----
....
OTHER KIND OF LINES HERE...
TEXT, 100, 54, 43, 32, 21
TEXT, 101, 12, 11, 12, 11
TEXT, 102, 54, 43, 32, 21
TEXT, 103, 54, 43, 32, 21
OTHER KIND OF LINES HERE...
....
-----
See it? The line numbers was re-ordered... Can anyone help me a bit with
that? I think it should be pretty easy, somehow...
Here's the part of my code I want to change:
------------------------------------------
unsigned int line, n1, n2, n3, n4;
/* this can be a list (growing always) */
unsigned int delete_line_array = [101,150,155];
string txt;
linenumber = 0; /* reset */
unsigned int deleted_lines = 0; /* nothing deleted yet */
while( getline( infile, read_line ) )
{
linenumber++;
if( read_line.find("TEXT,") == 0 ) /* BEGINNING OF LINE */
{
//cout << "Found at line" << linenumber << endl;
//cout << read_line << endl;
// I need something like this, I think:
read_line >> txt >> line >> n1 >> n2 >> n3 >> n4;
if (line != delete_line_array[deleted_lines]) /* COPY LINE */
/* NO: outfile << read_line << endl; */
outfile << txt << (line-deleted_lines) ...
<< n1 << n2 << n3 << n4
else
/* DON'T WRITE THIS LINE OUT AND
ALSO SHIFT LINENUMBERS AFTER */
deleted_lines++; /* increment counter */
end
}
else
{
outfile << read_line << endl; /* COPY LINE TO OUTFILE */
}
}
infile.close();
------------------------------------------
Your help is appreciated... TIA.
Regards,
Martin