Simple file I/O - need help

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
?

=?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
 
S

srikrishanmalik

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



Maybe You can do this:

keep a vector of string saved_line_num.
While skipping a line push the second field in saved_line-num,
somthing like strtok or tokenize will help.
when you get a line which is NOT to be skipped and the vector is not
empty, get rid of the second field and insert the first element of
vector in second field. - delete the element from the vector and write
the line to file2.

I u get a good line and the vector is empty, you just copy it to
file2.

Thanks
Sri


Then when you get a line
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Maybe You can do this:

keep a vector of string saved_line_num.
While skipping a line push the second field in saved_line-num,
somthing like strtok or tokenize will help.
when you get a line which is NOT to be skipped and the vector is not
empty, get rid of the second field and insert the first element of
vector in second field. - delete the element from the vector and write
the line to file2.

I u get a good line and the vector is empty, you just copy it to
file2.

Ok, perhaps I was not clear enough about what I needed. This is actually
*extremely* simple I think - I just forgot how to do it:

Consider this minimal example:

----------
string read_line, txt;
unsigned int line,n1,n2,n3,n4;
string read_line = "TEXT, 183, 201, 212, 213, 202\n"

/* THIS DOESN'T WORK - NEED HELP WITH IT */
read_line >> txt >> line >> n1 >> n2 >> n3 >> n4; // <<<<=== WRONG!

/* VERIFICATION OF CORRECTLY READ INPUT: */
cout << txt << line << n1 << n2 << n3 << n4; // <<<=== NEED HELP!
----------

"read_line" is a line I read with "while( getline( infile, read_line )
)"... etc. Here's the error message by the compiler:

error: no match for 'operator>>' in 'read_line >> txt'



QUESTION: How do I extract the parts: "txt >> line >> n1 >> n2 >> n3 >>
n4" from the string read_line? I guess it's basic stuff, I just can't
remember it... TIA.


Regards,
Martin
 
L

LR

Martin said:
(e-mail address removed) wrote:
Ok, perhaps I was not clear enough about what I needed. This is actually
*extremely* simple I think - I just forgot how to do it:

Consider this minimal example:

----------
string read_line, txt;
unsigned int line,n1,n2,n3,n4;
string read_line = "TEXT, 183, 201, 212, 213, 202\n"

/* THIS DOESN'T WORK - NEED HELP WITH IT */
read_line >> txt >> line >> n1 >> n2 >> n3 >> n4; // <<<<=== WRONG!

/* VERIFICATION OF CORRECTLY READ INPUT: */
cout << txt << line << n1 << n2 << n3 << n4; // <<<=== NEED HELP!
----------

"read_line" is a line I read with "while( getline( infile, read_line )
)"... etc. Here's the error message by the compiler:

error: no match for 'operator>>' in 'read_line >> txt'



QUESTION: How do I extract the parts: "txt >> line >> n1 >> n2 >> n3 >>
n4" from the string read_line? I guess it's basic stuff, I just can't
remember it... TIA.


Look into std::eek:stringstream and std::istringstream.

LR
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

LR said:
Look into std::eek:stringstream and std::istringstream.

I found the solution and completed my program:

sscanf(read_line.c_str(), "TEXT,%d,%d,%d,%d,%d",\
&line, &n1, &n2, &n3, &n4); //(with modifications)

Thanks to those that replied.


Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top