C++/C

Advertisement


Question
I need to workout on a text file.
This is the format

    Parameters       Left[y]     Right[n]

[y]Looping parameters
  Bonding_K          100          20
  Bonding_Z          11          22
[n]Bonding Sequences
  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.


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

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.