473,382 Members | 1,431 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,382 software developers and data experts.

Some basic Q for help!

1.
In a program, there will be several files needed to be accessed. Is it
necessary to design a class that handles reading/writing files?

2.
For a struct data type,

struct mystruct
{
int int1;
char b;
int int2;
}

How to using >> and << for writing and reading objects of struct Date?

3.
Needs to read from 1000 ints from a binary file of integers.

Code 1:

ifstream instream;

int B[1000];

unsigned k = 0;
while(instream.eof() && m<1000){
instream >> B[k];
++k;
}
Code 2:

void read4bytes(void *p)
{
instream.read((char*)p, 4);
}

unsigned k = 0;
int* ptr;
while(k<1000)
{
read4bytes(ptr);
B[k] = *ptr;
++k;
}

Is Code 1 higher level of reading files compared to Code 2?
Is Code 1 faster compared to Code 2?
Should always use coding like Code 1 for reading files?

Thanks for your help!
Jul 22 '05 #1
2 1145
Birt wrote:
1.
In a program, there will be several files needed to be accessed. Is it
necessary to design a class that handles reading/writing files?

That's the purpose of fstream. If you mean for semantic parsing (fill
objects with data), you need to provide your own...
2.
For a struct data type,

struct mystruct
{
int int1;
char b;
int int2;
}

How to using >> and << for writing and reading objects of struct Date?
Just like you would any other class

struct mystruct{
....
friend std::ostream& operator<<(std::ostream& os, const mystruct& s);
friend std::istream& operator>>(std::istream& is, mystruct& s);
};
3.
Needs to read from 1000 ints from a binary file of integers.

Code 1:

ifstream instream;

int B[1000];

unsigned k = 0;
while(instream.eof() && m<1000){
instream >> B[k];
++k;
} I may be wrong about this, but unless your ints are separated by spaces,
this code will not work as you expect.

Code 2:

void read4bytes(void *p)
{
instream.read((char*)p, 4);
}

unsigned k = 0;
int* ptr;
while(k<1000)
{
read4bytes(ptr);
B[k] = *ptr;
++k;
}
This only works if you serialzed all your ints in hexadecimal form (or
through ofstrea::write.
Is Code 1 higher level of reading files compared to Code 2? I have no idea of what you mean.
Is Code 1 faster compared to Code 2? I doubt it. Code 2 is more specialized (read exactly four bytes,
rinterpret the read bytes as hex ints.
Code 1 has to go through some hoops to get to the data.

BTW, I don't think it will make much difference to whatever algorith,
you want to feed...
Should always use coding like Code 1 for reading files?

Depends on the application.

JLR
Jul 22 '05 #2
Jorge Rivera <jo*****@rochester.rr.com> wrote in message
news:HB*******************@twister.nyroc.rr.com...
Needs to read from 1000 ints from a binary file of integers.

Code 1:

ifstream instream;

int B[1000];

unsigned k = 0;
while(instream.eof() && m<1000){
instream >> B[k];
++k;
}

I may be wrong about this, but unless your ints are separated by spaces,
this code will not work as you expect.


Code 2:

void read4bytes(void *p)
{
instream.read((char*)p, 4);
}

unsigned k = 0;
int* ptr;
while(k<1000)
{
read4bytes(ptr);
B[k] = *ptr;
++k;
}


This only works if you serialzed all your ints in hexadecimal form (or
through ofstrea::write.


Thanks Jorge for your help!

Why did you think Code 1 only works when the ints in the file is separated
by spaces? How to change Code 1 to get rid of such a requirement on
"spaces"?

"serialzed all your ints in hexadecimal form"? Does this mean that you can
serialize ints in other forms? What are the other forms? How to serialize
in other forms? ofstream::write() can only be used in hexadecimal form?
Jul 22 '05 #3

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

Similar topics

3
by: fdsl ysnh | last post by:
--- python-list-request@python.orgдµÀ: > Send Python-list mailing list submissions to > python-list@python.org > > To subscribe or unsubscribe via the World Wide Web, > visit >...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
1
by: Tom Rahav | last post by:
Hello all! I develop application in Visual Basic .NET and ORACLE database. My question is how do I "send" script file to the database using visual basic .net. Other words, is there any way to...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
1
by: marklinehan | last post by:
Hi, my name is Mark Linehan. About 20 years ago or so I started learning how to program on the Commodore 64 computer (anyone remember those?) heheheh. I taught myself basic on this little machine...
2
by: windandwaves | last post by:
Hi Folk When learning PHP, the hardest is always learning the basics. Once you understand the concept, you learn a lot quicker. For a friend, I put together some basic, basic examples. One...
2
by: ste | last post by:
VS2005, .NET2 I have a dll that is using TraceSources with no problems. The dll is linked to a webservice. The webservice creates its own traceSource and every thing looks fine.. however...
35
by: Justin Weinberg | last post by:
My thoughts on this.... http://msdn.microsoft.com/vbasic/Future/default.aspx?pull=/library/en-us/dnvs05/html/vb9overview.asp My thoughts: 1. Regarding Implicit types, I don't use type...
13
by: usenet | last post by:
How and where can one find out about the basics of VB/Access2003 syntax? I am a died in the wool C/C++/Java Linux/Unix programmer and I am finding it difficult to understand the program format...
1
by: zeeshansohail | last post by:
Hi, to all of the fellows. I m new to Visual Basic6.0. I want that if someone can help me in learning VB. I mean, if some LINKS can be told to me for VB 6.0 help. I am specifically want to learn VB...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

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.