Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Extracting a field from a data file

Posted on 2004-10-15
8
Medium Priority
?
178 Views
Last Modified: 2011-04-14
I am now writing a program to extract fields from a data.
Now I am experiencing problems to store the bytes read from the file into appropriate fields.
Since the file is not properly aligned, so I have to skip 0x09 and 0x0A character and 0x32 (which is space) as well everytime I read a byte from the file.

When I read the byte and pass it to the function to store in a field, the byte is not stored in appropriate fields since each field has different number of bytes.

I tried solving this problem by using
fstream extract_data;
...
...
extract_data << record.data1 << rercord.data2 << record.data2 ...
This works fine except when the particular record has a "blank" field.

What other options can I go for?
0
Comment
Question by:icysmarty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12325630
That depends on what you want to shift your focus on. If you can easily change the data layout, introduce a special value that represents a blank (NULL) field. Or, change yur parsing code to cope with that. I'd go with the latter, especially if you e.g. know that two consecutive delimiters in fact do mean a blank field.
0
 

Author Comment

by:icysmarty
ID: 12325708
I don't get you.
What do you mean by two consecutive delimiters mean a blank field?

0
 
LVL 86

Expert Comment

by:jkr
ID: 12325737
Well, let's say you have

SomePerson|1978/07/21|$2000|A nice guy

and

SomeOtherPerson|1972/03/21||Huh?

you'd see that the fields re delimited by "|" and that "||" means "no content"...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:icysmarty
ID: 12325752
Then how do I know if it is the end of field.
Say
SomePerson|1978/07/21|$2000|A nice guy
Whoever|1978/07/21|$30000|A bad guy

The first field, SomePerson and Whoever have different length of field. How to detect the end of field, if I am reading it byte by byte from a data file?

For blank, I can keep a counter for the space, whenever I encounter more than 2 space, that means that field is a blank field right/
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 12325758
>> How to detect the end of field

By looking for the delimiter, which is "|" in the above sample.
0
 
LVL 1

Expert Comment

by:gugario
ID: 12325803
Icysmart was suggesting that he was using the fstream >> operator, which to my knowledge always ignores whitespace (just like cin).  So, although your example of having | as the delimitor is good, I don't think it's the case here.

Icysmart, could you be more specific about what kind of input data you're reading from the file, and in what kind of variables you are trying to store it?
0
 

Author Comment

by:icysmarty
ID: 12325822
I decided not to use fstream >> operator cause it does not work out for my case.
I am reading a data file with 7 fields and storing them in a record of stuct type.
I create an array for each field and read the data file byte by byte.
The data file is like following

1st
 40    abc.de    john_smith   30,000 23 5
2nd
  50   xxyy.d     barbara     20,000 25 2
...
...

Obviously after 1st there will be a tab and line feed.. so I need to check for this conditions.. but there are too many conditions to be included in order to make the program runs perfectly. That's why I am asking if there are other c++ functions that I do not aware of.
0
 

Author Comment

by:icysmarty
ID: 12326526
>> By looking for the delimiter, which is "|" in the above sample

I got it. The program works. I didn't know that two delimiters means a blank field. Thank you very much.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question