searching, creating new file from old using c++

M

Michele and John

I would like to write a C++ program that searches for the variable "state !=
0" in a text file, and then go back 3 steps each time to read "count". The
program should create a new file with "state count". The data in the old
file is variable, but could be as follows:

old_file.txt
new_file.txt
state count
state count
0 22
1 83
0 83 <-
1 13
0 13
1 34
0 34
1 33
1 33
1 23
1 31
1 66
1 23 <-
3 63
1 66
3 37
0 45
3 88
1 22
3 66
1 98
3 36
0 13
0 73
0 53
0 63 <-
0 37
0 88
3 66
3 36
3 33
3 88
3 67
0 45
0 35
0 44

Any help will be greatly appreciated.

Regards,
Michele
 
M

Michele and John

Sorry, the formatting is messed up.

I would like to write a C++ program that searches for the variable
"state != 0" in a text file, and then go back 3 steps each time to read
"count". The
program should create a new file with "state count". The data in the old
file is variable, but could be as follows:

old_file.txt
state count
0 22
0 83 <-
0 13
0 34
1 33
1 31
1 23 <-
1 66
0 45
1 22
1 98
0 13
0 73
0 53
0 63 <-
0 37
0 88
3 66
3 36
3 33
3 88
3 67
0 45
0 35
0 44



new_file.txt
1 83
1 13
1 34
1 33
1 23
1 66
3 63
3 37
3 88
3 66
3 36


Any help will be greatly appreciated.

Regards,
Michele
 
J

Jack Klein

I would like to write a C++ program that searches for the variable "state !=
0" in a text file, and then go back 3 steps each time to read "count". The
program should create a new file with "state count". The data in the old
file is variable, but could be as follows:

[snip]

If you want to write a C++ program, why the blue blazes are you
cross-posting to comp.lang.c?

They are two different languages, you know, and C++ is completely
off-topic in comp.lang.c.

Kindly be more polite in the future.

Followup set.
 
N

Nick Keighley

Jack said:
I would like to write a C++ program that searches for the variable "state !=
0" in a text file, and then go back 3 steps each time to read "count". The
program should create a new file with "state count". The data in the old
file is variable, but could be as follows:

[snip]

If you want to write a C++ program, why the blue blazes are you
cross-posting to comp.lang.c?

They are two different languages, you know, and C++ is completely
off-topic in comp.lang.c.

Kindly be more polite in the future.

Followup set.

anyone remember the old anti-grumpiness campaign?
 
N

Nick Keighley

Michele said:
Sorry, the formatting is messed up.

I would like to write a C++ program that searches for the variable
"state != 0" in a text file, and then go back 3 steps each time to read
"count". The
program should create a new file with "state count". The data in the old
file is variable, but could be as follows:

old_file.txt
state count
0 22
0 83 <-
0 13
0 34
1 33
1 31
1 23 <-
1 66
0 45
1 22
1 98
0 13
0 73
0 53
0 63 <-
0 37
0 88
3 66
3 36
3 33
3 88
3 67
0 45
0 35
0 44



new_file.txt
1 83
1 13
1 34
1 33
1 23
1 66
3 63
3 37
3 88
3 66
3 36


Any help will be greatly appreciated.

Regards,
Michele
 
N

Nick Keighley

Michele said:
Sorry, the formatting is messed up.

I would like to write a C++ program that searches for the variable
"state != 0" in a text file, and then go back 3 steps each time to read
"count". The
program should create a new file with "state count". The data in the old
file is variable, but could be as follows:

old_file.txt
state count
0 22
0 83 <-
0 13
0 34
1 33
1 31
1 23 <-
1 66
0 45
1 22
1 98
0 13
0 73
0 53
0 63 <-
0 37
0 88
3 66
3 36
3 33
3 88
3 67
0 45
0 35
0 44



new_file.txt
1 83
1 13
1 34
1 33
1 23
1 66
3 63
3 37
3 88
3 66
3 36


Any help will be greatly appreciated.

<snip repeated initial post>

grep "1 " old_file.txt

(ok there are better REs but I can't be bothered to look it up)

if you *really* have to write a program take a look at strstr().
if you *really* have to do it in C++ then try comp.lang.c++. I'd
guess std::string would be a place to start.


--
Nick Keighley

"High Integrity Software: The SPARK Approach to Safety and Security"
Customers interested in this title may also be interested in:
"Windows XP Home"
(Amazon)
 
M

Michele and John

Nick,
Thanks for your advice.

Does strstr() have the ability to search for a string at the current line in
a text file, and if there is a match
1) copy the string at the current line n, and also
2) copy strings at a previous line? e.g. n-3

Please let me know your thoughts.

Regards,
Michele
 
N

Nick Keighley

please don't top post. I have re-arranged your post accordingly.
Also trim all but essentials from what you are replying to.

much shortened example

<also much shotened>


so why don't you use grep?

do you want C or C++? They are different languages. I have only
given a C answer.

Does strstr() have the ability to search for a string at the current line in
a text file, and if there is a match

no. as 20s with google woul have shown you. Use fgets() to read a line
and
strstr() to find a string within it.
1) copy the string at the current line n, and also
2) copy strings at a previous line? e.g. n-3

why do you need to do this to solve your problem. And what does
"eg. n-3" mean?
 
C

Christian Gollwitzer

Nick said:
much shortened example
but wrong
<also much shotened>

and equally wrong
so why don't you use grep?

Because your solution is wrong. It can't be done with grep alone. The
requirement was, that at encountering a state not equal to zero, the
value three lines above should be output. It's, however, trivial in
scripting languages like awk:

awk '$1 { print $1, old3 }
{ old3=old2; old2=old1; old1=$1; } ' old_file.txt

This can be easily translated into C++, which is left as an exercise to
the reader (OP)

Christian
 
M

Michele and John

Thanks everyone for your suggestions.

I implemented the solution using variations of the STL vector...

Regards,
Michele
 
M

Michele and John

Keith,
The parent article was posted only to comp.lang.c++. Why did you
cross-post your followup to comp.lang.c?

If you paid attention to detail, you would have noticed that I posted the
original article to both comp.lang.c++,comp.lang.c
Why did you cross-post to comp.lang.c?

Michele.

Keith Thompson said:
The parent article was posted only to comp.lang.c++. Why did you
cross-post your followup to comp.lang.c?

And please read <http://cfaj.freeshell.org/google/>.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top