I need to workout on a text file.
This is the format
Parameters Left[y] Right[n]
Bonding_K 100 20
Bonding_Z 11 22
Lead_1 23 0
Die 33 12
this file goes in this format. I also have the same kind of destination file without that [y] [n] conditions. I want to change the contents which is indicated by [Y] and should not overwrite that is marked by[n]. For example i need to overwrite the destination file with Left data, particularly for looping parameters and not for bonding sequences.
How to do this? i feel difficulty to code this. Kindly please help me.
First let me point out that your subject seems to be C, and you are asking a C++ expert. Which language are you using? (Note this is complicated by the AllExperts site's occasional posting and display problems with + symbols being replaced with spaces, in website style).
As you are asking a C++ expert, I may use C++ or C idioms.
Now to these [n] and [y] tags in your example file. I am confused as to whether they only appear in the input file or not at all and are just annotations added by you for the purposes of your question.
I am further confused as to which bits you wish to leave alone and which you wish to replace as you have 2 [y] and 2 [n] tags - one each for rows and columns. Do you wish to change only the values 100 and 11 (both columns and rows marked by [y]), or all except 0 and 12 (both rows and columns marked by [n]) or some other combination?
I am also assuming the [y] and [n] tags do not just apply the words on the lines on which they appear (Left, Right, Looping parameters and Bonding Sequences) and that all text (except possibly for the [y] and [n] tags) appear in both input and output files.
You do not say if your files are sensitive to white space - i.e. can spaces and tabs read in be replaced with say a single space or tab in the output.
So as I am unsure of what language you are using, and what format your files are and which bits you intend to keep and which to change my answer will be high level and general in nature.
First open the input and output files using fopen (C) or std::fstream, std::ifstream, std::ofstream passing the parameters in the constructor or to the open member function (C++).
Next enter a loop reading one character at a time until the end of file is reached, whereupon you exit the loop, close the files and exit the program. In C you can use the getc to obtain a character or an end of file indicator. In C++ you can use the input stream get member function and check the stream's state after each use for errors or end of file.
Within the input processing loop start by just copying the input character to the output. You can use putc in C and the output stream put member function in C++.
Now you can start having fun. You will need to detect certain phrases - such as Bonding Sequences maybe. This will modify the state of the processing. I shall assume for the moment that after Bonding Sequences has been seen the second non-whitespace sequence of characters (i.e. the first number) is to be changed.
In order to detect phrases you read each character as before, but collect all non-whitespace characters into a string. You stop when you next get a whitespace character. You can probably overwrite the string the next time you receive a non-whitespace character in most cases. These strings I shall call tokens. If you need to keep the current token around for a while then you can make a copy of the token string. Whitespace includes space, tab and end of line. When you have received a whole token you examine it to see if it matches one you are interested in - Bonding for example.
So detecting Bonding you set a state that from the next line every second token read in is to be junked and replaced with a new value. This may be two states such that normal copying is performed after detecting Bonding except that when a newline is next seen from the input file another new state is set which causes your code to replace every second token on each line.
Of course exactly which triggers are used and which set of states you require depends on exactly how you wish to modify your file.
Things you might wish to consider:
- using the C function strtok to help split the data into tokens
- using a language such as Perl that is good at pattern matching and such file processing
- using a C++ (or C) regular expression library such as that provided by the Boost (http://www.boost.org/
) Regex library (http://www.boost.org/libs/regex/doc/index.html
In any case you will need to think carefully about exactly what you need to do and look for ways to do it. My general description is quite high level and uses the basic character by character approach. Using some of the thinks I suggested you consider may allow you to work on larger units such as lines.