Advertisements

> I have a input.txt file separator by pipe '|' as -
>
> 40147|181|ORANGE|MIKE|XX||1000397900|3500
> 40148|373|ORANGE|BOB|XX||1078889400|4500
> 40149|673|ORANGE|TREY|XX||1095159900|5500
>
>
> I only wish to have all the FIRST(ID=40147) and LAST FIELD(PRICE=3500) to
> be printed for three lines as given above.

I think this would be a task for "awk", or its modern replacement if
there is one. But as I recall from old days, "awk" was good at splitting
lines into fields. If working in Windows, then there are lots of "Unix
utilities" around, including just installing Cygwin (one practical idea
is to map drive U: to such utilities, write "u::awk").

Using Boost is generally a good idea when you have it available, but in
this case I think it's shooting gnats with intercontinental ballistic
H-bomb missiles. Will kill them gnats, guaranteed, yes. But, overkill...

> using namespace std;
>
> struct FILE_INPUT {

It's a good idea to reserve ALL UPPERCASE for macro names.

That way you tend to avoid name collisions and unintended text
substitutions.

Conversely, it's not a good idea to use ALL UPPERCASE for anything else
-- in C and C++.

Java and C# conventions are a different matter, since neither Java nor
C# has a preprocessor.

It's quite a bit ironic in a way. Java got its convention for constants
from C, where constants had to be defined as macros, hence ALL
UPPERCASE. So the Java convention is really about using ALL UPPERCASE
for macros-that-denote-constants, but Java doesn't have a preprocessor,
and does have a facility for defining constants, so it's all misplaced.

> int ID;

Again, ungood ALL UPPERCASE name.

> int Asset_ID;
> char T_Name[];

Oh noes, that won't work! In standard C++ an array must have a specified
size. And unless it's dynamically allocated, size must be >0.

On Sun, 2014-01-19, Martijn Lievaart wrote:
> On Sat, 18 Jan 2014 04:24:27 +0100, Alf P. Steinbach wrote:
>
>> On 18.01.2014 03:39, Rahul Mathur wrote:
>>> Hi,
>>
>> Oh, hi!
>>
>>
>>> I have a input.txt file separator by pipe '|' as -
>>>
>>> 40147|181|ORANGE|MIKE|XX||1000397900|3500
>>> 40148|373|ORANGE|BOB|XX||1078889400|4500
>>> 40149|673|ORANGE|TREY|XX||1095159900|5500
>>>
>>>
>>> I only wish to have all the FIRST(ID=40147) and LAST FIELD(PRICE=3500)
>>> to be printed for three lines as given above.
>>
>> I think this would be a task for "awk", or its modern replacement if
>> there is one. But as I recall from old days, "awk" was good at splitting
>> lines into fields. If working in Windows, then there are lots of "Unix
>> utilities" around, including just installing Cygwin (one practical idea
>> is to map drive U: to such utilities, write "u::awk").
>
> In this case, the modern replacement of awk is ..... awk.

There was a "for three lines" requirement too, but it can be met by
piping things through 'sed 3q'. Or Perl, or possibly Awk itself.
> which is more or less guarenteed to be simpler than any C++ program doing
> the same task.

Yes. And next best is a C++ program using the same approach.
I got the impression that the original sucked up the whole input file
before extracting the information it wanted; that's slower and IME
more troublesome (harder to report syntax errors and so on).

On Mon, 2014-01-20, Martijn Lievaart wrote:
> On Mon, 20 Jan 2014 17:01:36 +0000, Jorgen Grahn wrote:
....
>>> which is more or less guarenteed to be simpler than any C++ program
>>> doing the same task.
>>
>> Yes. And next best is a C++ program using the same approach.
>> I got the impression that the original sucked up the whole input file
>> before extracting the information it wanted; that's slower and IME more
>> troublesome (harder to report syntax errors and so on).
>
> AFAIK splitting a string is still not in the standard library, so any C++
> solution is not as simple as a language that has a split/explode. (Please
> correct me if I'm wrong!)

I expect <regex> to have such things, but haven't checked. I keep my
own string splitter and some of the other Perl things I use the most,
e.g. 'while(<>)'.

Anyway, my comment above was not really about split; more about the
idiom of line-by-line text filters, versus pulling it all into some
data structure and /then/ processing it.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!