473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Best way to access this file?

Hello,

I have a text file I'm attempting to parse. There are about 50 fixed width
fields in each line / row. For example (shortened for brevity):

W1234Somebody East 101110001111010101
E1235Someone Else West 010111001001010101

I'm having problems pulling these fields into structures, in order to be
able to access each individually. I am currently opening as a sequential
file. Is there a better way?

My structure looks something like:

struct data{
char area[1];
char empNumber[4];
char name[16]
char region[5];
char options[20];
}

int index = 0;
data user[100];

I would like to read the entire file into memory. Please tell me if I'm
going about this the wrong way. So far after reading the file in, I'm
unable to access any individual items (ie. user[index].area) Also should
I stick with a sequential file, or should I consider binary access (seems
like it may be easier to address individual elements)?

(I don't want to include too many details here, but will be happy to provide
whatever is needed).

Thank you in advance,
Brian
Jul 22 '05 #1
9 2084
Brian wrote:
I have a text file I'm attempting to parse. There are about 50 fixed width
fields in each line / row. For example (shortened for brevity):

W1234Somebody East 101110001111010101
E1235Someone Else West 010111001001010101

I'm having problems pulling these fields into structures, in order to be
able to access each individually. I am currently opening as a sequential
file. Is there a better way?

My structure looks something like:

struct data{
char area[1];
char empNumber[4];
char name[16]
char region[5];
char options[20];
}

int index = 0;
data user[100];

I would like to read the entire file into memory. Please tell me if I'm
going about this the wrong way.
Which way are you going? As soon as you specify the way we can tell if
it's wrong or not.
So far after reading the file in, I'm
unable to access any individual items (ie. user[index].area)
WTF does "unable to access" mean?
Also should
I stick with a sequential file, or should I consider binary access (seems
like it may be easier to address individual elements)?
Binaryness and sequentialness of a file are two orthogonal traits.
(I don't want to include too many details here, but will be happy to provide
whatever is needed).


FAQ 5.8.

V
Jul 22 '05 #2
* Brian:

I have a text file I'm attempting to parse. There are about 50 fixed width
fields in each line / row. For example (shortened for brevity):

W1234Somebody East 101110001111010101
E1235Someone Else West 010111001001010101

I'm having problems pulling these fields into structures, in order to be
able to access each individually. I am currently opening as a sequential
file. Is there a better way?
That question seems to be meaningless.

My structure looks something like:

struct data{
char area[1];
char empNumber[4];
char name[16]
char region[5];
char options[20];
}
Are you sure? This should not compile due to lacking semicolon
at the end. Always copy and paste code, don't retype it.

Assuming it is accurate in other details:

Every field is 1 character too short to be used as a C zero-terminated
string. Perhaps that is an error, or perhaps it's intentional: that
you intend this structure to directly reflect the data. But if the
latter it's non-portable, because different compilers may add padding
in different ways (you're almost guaranteed that 'area' will occupy at
least two bytes of structure space, no matter which compiler).
int index = 0;
data user[100];


Do think about using C++'s facilities for _abstraction_.

E.g.,
class AreaCode{ ... };
class EmployeeNumber{ ... };
class ...

class FixedWidthFields
{
public:
FixedWidthFields( std::string line ) { ... }
FixedWidthFields(
AreaCode const& anAreaCode,
EmpoyeeNumber const& anEmployeeNumber,
...
)
{ ... }

AreaCode areaCode() const { return ...; }
EmployeeNumber employeeNumber() const { return ...; }
...
};
...
std::ifstream f( filename );
std::string s;
std::vector<

if( !f ) { throw std::runtime_error( "asølkjasd" ); }
std::getline( f, s );
FixedWidthFields fields( s );
// Create real user data representation from 'fields'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3
Thanks for your comments - points well taken. I'll be the first to
admit I have a long way to go, but I'm trying. Also have to admit I
haven't been through the group's faq in a long time. Will have another
look.

Brian

Victor Bazarov <v.********@comAcast.net> wrote in message news:<a9*****************@newsread1.dllstx09.us.to .verio.net>...
Brian wrote:
I have a text file I'm attempting to parse. There are about 50 fixed width
fields in each line / row. For example (shortened for brevity):

W1234Somebody East 101110001111010101
E1235Someone Else West 010111001001010101

I'm having problems pulling these fields into structures, in order to be
able to access each individually. I am currently opening as a sequential
file. Is there a better way?

My structure looks something like:

struct data{
char area[1];
char empNumber[4];
char name[16]
char region[5];
char options[20];
}

int index = 0;
data user[100];

I would like to read the entire file into memory. Please tell me if I'm
going about this the wrong way.


Which way are you going? As soon as you specify the way we can tell if
it's wrong or not.
> So far after reading the file in, I'm
unable to access any individual items (ie. user[index].area)


WTF does "unable to access" mean?
> Also should
I stick with a sequential file, or should I consider binary access (seems
like it may be easier to address individual elements)?


Binaryness and sequentialness of a file are two orthogonal traits.
(I don't want to include too many details here, but will be happy to provide
whatever is needed).


FAQ 5.8.

V

Jul 22 '05 #4
Alf,

First of all many thanks for your thoughtful reply to my "scattered"
post. I see it's quite obvious I don't know what I'm doing. I've
been "messing" with C and C++ for at least the past 10 years, but
until now have not really tried taking on a meaningful project. I
have
a (I would say) basic understanding of the syntax.

I would love to be able to apply the code you presented, but quite
honestly it's way over my head right now. Most of the manuals I have
on my shelf don't do a great job of walking me through these commands
(could be b/c I have old books on my shelf!)

Anyhow I "sort of" understand the way I'm attempting to solve this,
but even so there are many holes. For example even though I've
declared
the array of structures named 'user', when I try reading the file into
this array I come up with blanks (or whatever data already happens to
be
there).

Here's the code, if you have a moment to take a quick look and
(hopefully) point out my errors. The data file will be included at
the
end.

Also please let me know if you have any suggestions on books, etc..
that can help me with the "True" C++ syntax.

Thank you,
Brian

------------------------------------------------------------------
#include <iostream.h>
#include <string.h>
#include <fstream.h>
#include <conio.h>
using namespace std;

int main()
{
int counter = 0;
// create file object and open file
ifstream inFile;
inFile.open("BDNTest.prn", ios::in);

// Determine if the file was opened successfully

if (inFile.is_open())
{
string name = "";
getline(inFile, name, '\n');
while (!inFile.eof())
{
counter +=1;
cout << name << endl;
getline(inFile, name, '\n');
}
inFile.close();
}
else
cout << "File could not be opened" << endl;
cout << endl << "Total records: " << counter;

cout << "Now ready to read BDN File" << endl;

struct BDNData {
char area;
char EIN[5];
char station[4];
char name[17];
char fileNum[10];
char userType;
char orgCode[4];
char accessLevel;
char rest[78];
};
BDNData user[100];
int index=0;

//read BDN file into memory array
cout << "Address first array: " << &user[0] << endl;
cout << "Address second array: " << &user[1] << endl;
cout << sizeof(user[0]);
cout << endl << "user-0: " << user[0].name << user[0].EIN;

inFile.open("BDNTest.prn", ios::in);

while (!inFile.eof()) {
cout << "Array " << index << ": " << &user[index] << endl;
inFile >> &user[index];
cout << endl << "Name: " << user[index].name << endl;
cout << "For Index " << index << "Process complete" << endl;
getch();
index++;

}
inFile.close();
return 0;

}

----------------------------------------------------------------------

************************** BDNTEST.PRN *******************************
----------------------------------------------------------------------

W1212345JONESS KIMBE 123456789121
0000001011000000000001000000000000000
0000100111010000000000000100001000000000
W1092345ROESE DAVID 234567890121
6011111111110000011111001101100010000
0000110111011000000100100110101000011111
W1111345FUNNNYYWICT 121
7011111111110111011011101101101010000
0000111111011000000100100110101000011011
W4444345CRUISE BRIAN 123456789124
7000001000000000000000000000000000000
0000100000010000000000000100001001000000
W5055644D SMITTY 01234567 213
5000001000000000000000001000000100001
0000100000110000000000101100001000000000
W6666345SMITH SANDRA 987654321100
8000001000000000000000000000000000000
0000100000010000000000000100001001000001
W6789345BERTHA BIG 246802461100
8000001000000100000000000000000000000
0000100000010000000000000100001001000001
W2222345JAGGER MICK 126
0000001000000000000000001000000000000
0000100000000000000000000000000000000000
W1111345BROOKS GARTH 100
7000001000000000000000000000000000000
0000100000000000000000000100001000000000
W9876345LAUPER CYNTHIA
120F8000001000000000000000000000000000000
0000100000010000000000000100001000000001
-------------------------END OF FILE
-------------------------------------

Sorry about the text wrap!

al***@start.no (Alf P. Steinbach) wrote in message news:<41*****************@news.individual.net>...
* Brian:

I have a text file I'm attempting to parse. There are about 50 fixed width
fields in each line / row. For example (shortened for brevity):

W1234Somebody East 101110001111010101
E1235Someone Else West 010111001001010101

I'm having problems pulling these fields into structures, in order to be
able to access each individually. I am currently opening as a sequential
file. Is there a better way?


That question seems to be meaningless.

My structure looks something like:

struct data{
char area[1];
char empNumber[4];
char name[16]
char region[5];
char options[20];
}


Are you sure? This should not compile due to lacking semicolon
at the end. Always copy and paste code, don't retype it.

Assuming it is accurate in other details:

Every field is 1 character too short to be used as a C zero-terminated
string. Perhaps that is an error, or perhaps it's intentional: that
you intend this structure to directly reflect the data. But if the
latter it's non-portable, because different compilers may add padding
in different ways (you're almost guaranteed that 'area' will occupy at
least two bytes of structure space, no matter which compiler).
int index = 0;
data user[100];


Do think about using C++'s facilities for _abstraction_.

E.g.,
class AreaCode{ ... };
class EmployeeNumber{ ... };
class ...

class FixedWidthFields
{
public:
FixedWidthFields( std::string line ) { ... }
FixedWidthFields(
AreaCode const& anAreaCode,
EmpoyeeNumber const& anEmployeeNumber,
...
)
{ ... }

AreaCode areaCode() const { return ...; }
EmployeeNumber employeeNumber() const { return ...; }
...
};
...
std::ifstream f( filename );
std::string s;
std::vector<

if( !f ) { throw std::runtime_error( "asølkjasd" ); }
std::getline( f, s );
FixedWidthFields fields( s );
// Create real user data representation from 'fields'.

Jul 22 '05 #5
* Brian:

Here's the code, if you have a moment to take a quick look and
(hopefully) point out my errors. The data file will be included at
the end.
Code and test data is precisely the thing to get useful suggestions.

Also please let me know if you have any suggestions on books, etc..
that can help me with the "True" C++ syntax.
"Accelerated C++" by Koenig and (somebody help out here) Moi?

A copy of Bjarne Stroustrup's "The C++ Programming Language" would
also be handy, but start with AC++.


------------------------------------------------------------------
#include <iostream.h>
Non-standard header, use
#include <iostream>

#include <string.h>
Probably not the header you think it is. The one you've specified
here is (almost, modulo namespace issues) equivalent to writing
#include <cstring>
i.e. it's a header from the C library, while the C++ header that defines
the std::string type is
#include <string>
#include <fstream.h>
Non-standard header, use
#include <fstream>

#include <conio.h>
System-specific header, OK for system-specific program.

using namespace std;

int main()
{
int counter = 0;
// create file object and open file
ifstream inFile;
inFile.open("BDNTest.prn", ios::in);

// Determine if the file was opened successfully

if (inFile.is_open())
{
string name = "";
getline(inFile, name, '\n');
while (!inFile.eof())
Better
while( !infile )
because that also checks for error state.

{
counter +=1;
cout << name << endl;
getline(inFile, name, '\n');
}
inFile.close();
}
else
cout << "File could not be opened" << endl;
cout << endl << "Total records: " << counter;

cout << "Now ready to read BDN File" << endl;

struct BDNData {
char area;
char EIN[5];
char station[4];
char name[17];
char fileNum[10];
char userType;
char orgCode[4];
char accessLevel;
char rest[78];
};
Replace every character array with a std::string.

BDNData user[100];
Replace this array with a std::vector<user>.

int index=0;

//read BDN file into memory array
cout << "Address first array: " << &user[0] << endl;
cout << "Address second array: " << &user[1] << endl;
cout << sizeof(user[0]);
cout << endl << "user-0: " << user[0].name << user[0].EIN;

inFile.open("BDNTest.prn", ios::in);

while (!inFile.eof()) {
cout << "Array " << index << ": " << &user[index] << endl;
inFile >> &user[index];
Don't do that (whatever it is).

Use std::getline to get a string.

Then use e.g. str::substr to extract the fields in that string.

cout << endl << "Name: " << user[index].name << endl;
cout << "For Index " << index << "Process complete" << endl;
getch();
index++;

}
inFile.close();
return 0;

}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #6
* Alf P. Steinbach:

Better
while( !infile )


I meant,
while( infile )

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #7
Alf P. Steinbach wrote:
* Brian:
Also please let me know if you have any suggestions on books, etc..
that can help me with the "True" C++ syntax.

"Accelerated C++" by Koenig and (somebody help out here) Moi?


Moo. Barbara E. Moo.
[...]


V
Jul 22 '05 #8
Brian wrote:

Also please let me know if you have any suggestions on books, etc..
that can help me with the "True" C++ syntax.
Accelerate C++, by Koeing and Moo, is often recommended.
#include <iostream.h>
Nonstandard header, use <iostream>.
#include <string.h>
Standard, but probably not what you want. string.h is the header for
the C-style string functions, not the C++ std::string.
#include <fstream.h>
Nonstandard header, use <fstream>.
#include <conio.h>


Platform specific header, don't use it or anything from it on this
newsgroup.

Fix all that first. Oh, and don't top-post, your replies belong
following properly trimmed quotes.

Brian

Jul 22 '05 #9

"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* Alf P. Steinbach:

Better
while( !infile )


I meant,
while( infile )


BDNData user[100];

Replace this array with a std::vector<user>.


You also meant:

Replace this array with a std::vector<BDNData>.

:-)

-Mike
Jul 22 '05 #10

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

Similar topics

1
by: | last post by:
I am new to VB6 and need some advice... I am developing software which aims to capture a text feed from a serial port consisting of news stories in ASCII format and then save this text for...
3
by: Ryan N. | last post by:
Hello, I saw a brief blurb on this somewhere and am unable to recall where... In the context of Security, what are some best practices for handling -storing, locating, retrieving- database OLEDB...
3
by: MLH | last post by:
I'm developing an app that I'd like to backup each time I open it for modifications. Is it too late to do so after opening the file with Access? Here's what I've been trying... Trevor Best posted...
20
by: Greg | last post by:
I'm fairly new to access (using 2002) and am setting up a DB for work. along with each record the user also needs to make a flow diagram (previously, these reports were composed in word and they...
17
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access to...
9
by: Luke Vogel | last post by:
Hi all. This is a bit of a newbie type question. I am trying to figure out what is the best way to connect to a database; ado.net, odic others? I've found a couple of examples that show you...
8
by: Art | last post by:
Hi folks, I'm writing a traditional desktop app using VB.NET and am stumbling over what seems like a very basic question: My app does not need to be connected to a server or another computer....
9
by: JAKDND | last post by:
I'm trying to find out the best architecture would be for a web+access solution. We have a database of individuals' job details. There's a big table with an ID key field, name addess etc, and a...
3
by: Nemisis | last post by:
Guys, I would like to write a error handler, or something, that will allow me to write to a database when an error occurs on my site. I am trying to implement this in the global.asax file a the...
6
by: kamsmartx | last post by:
I'm new to programming and need some help figuring out an algorithm. I need to design some kind of algorithm which will help us define capacity for one of our portfolios....here's the problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.