472,372 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,372 software developers and data experts.

searching, creating new file from old using c++

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
Apr 11 '06 #1
11 2095
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

"Michele and John" <ph**************@comcast.net> wrote in message
news:xJ********************@comcast.com...
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

Apr 11 '06 #2
On Mon, 10 Apr 2006 22:38:14 -0400, "Michele and John"
<ph**************@comcast.net> wrote in comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Apr 11 '06 #3

Jack Klein wrote:
On Mon, 10 Apr 2006 22:38:14 -0400, "Michele and John"
<ph**************@comcast.net> wrote in comp.lang.c:
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?
--
Nick Keighley

Apr 11 '06 #4

Michele and John wrote:
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

"Michele and John" <ph**************@comcast.net> wrote in message
news:xJ********************@comcast.com...
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


Apr 11 '06 #5
Michele and John wrote:
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)

Apr 11 '06 #6
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
"Nick Keighley" <ni******************@hotmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Michele and John wrote:
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)

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

Michele and John wrote:
"Nick Keighley" <ni******************@hotmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Michele and John wrote:
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 file is variable, but could be as follows:

old_file.txt
state count
0 22
0 34
1 33
1 66
0 45
much shortened example
new_file.txt
1 33
1 66

<also much shotened>

<snip>
grep "1 " old_file.txt

(ok there are better REs but I can't be bothered to look it up)
so why don't you use grep?
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.

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?
--
Nick Keighley

Apr 11 '06 #8
Nick Keighley wrote:
old_file.txt
state count
0 22
0 34
1 33
1 66
0 45
much shortened example but wrong
new_file.txt
1 33
1 66

<also much shotened>
and equally wrong
<snip>
grep "1 " old_file.txt

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

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
Apr 11 '06 #9
Thanks everyone for your suggestions.

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

Regards,
Michele
Apr 13 '06 #10
"Michele and John" <ph**************@comcast.net> writes:
Thanks everyone for your suggestions.

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


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/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 13 '06 #11
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" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org... "Michele and John" <ph**************@comcast.net> writes:
Thanks everyone for your suggestions.

I implemented the solution using variations of the STL vector...
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/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org

<http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this.

Apr 13 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michi | last post by:
Hello, I am creating a databse with a large number of text (or blob?) entries. I want users to be able to search these fields. An example would be a forum or journal. Indexing every word in...
0
by: Mike | last post by:
Sites using thumbnail preview for world wide web file navigation and searching. Below are list of sites that are either researching or providing thumbnail preview images for online web...
3
by: googleboy | last post by:
Hi there. I have defined a class called Item with several (about 30 I think) different attributes (is that the right word in this context?). An abbreviated example of the code for this is: ...
33
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following...
7
by: Michele and John | last post by:
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 ...
7
by: pbd22 | last post by:
Hi. I am somewhat new to this and would like some advice. I want to search my xml file using "keyword" search and return results based on "proximity matching" - in other words, since the search...
4
by: Hunk | last post by:
Hi I have a binary file which contains records sorted by Identifiers which are strings. The Identifiers are stored in ascending order. I would have to write a routine to give the record given...
20
by: Seongsu Lee | last post by:
Hi, I have a dictionary with million keys. Each value in the dictionary has a list with up to thousand integers. Follow is a simple example with 5 keys. dict = {1: , 2: , 900000: , 900001:...
3
by: Ahmad Jalil Qarshi | last post by:
Hi, I have a text file having size about 2 GB. The text file format is like: Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values For example...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.