473,778 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

please help me view the code

dear all :
please help me to slove the problem
as below code , it can't run .
i want to read a file and display.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <assert.h>

int main()
{
std::ifstream data_file("2252 135.dat",std::i os::binary);
char *data_ptr;

while (data_file.eof( ))
{
data_file.read( data_ptr,2);
std::cout << *data_ptr ;
{
data_file.close ();
}

#thanks
Jul 23 '05 #1
3 1356
ab*****@yahoo.c om.tw wrote:
char *data_ptr;

You use uninitialized pointer.

--
SirMike
the code is my strength
http://www.sirmike.grudziadz.com
Jul 23 '05 #2
ab*****@yahoo.c om.tw wrote:

dear all :
please help me to slove the problem
as below code , it can't run .
i want to read a file and display.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <assert.h>

int main()
{
std::ifstream data_file("2252 135.dat",std::i os::binary);
char *data_ptr;
This is a pointer. It can (but doesn't at the moment) point
to some char array. But as said: At the moment it doesn't
point to anywhere specific.

while (data_file.eof( ))
{
data_file.read( data_ptr,2);
And here you tell read() to put the data into a specific
memory location. You do this by providing read() with the
memory address of where to put the data, by passing data_ptr.
Thus data_ptr better holds the memory address of some memory
where the data can be put. At the moment data_ptr doesn't point
to any usable memory. That's why you have problems.

std::cout << *data_ptr ;
{
data_file.close ();
}


You don't need a pointer variable at all.
All read() wants from you is that you pass the address of some
memory area, where it can write the data to:

char data[2];

while( data_file.eof() )
{
data_file.read( data, 2 );
}

Some additional hints.
Using eof() the way you do, is almost always a sure sign of
desaster. in C++ eof() is not ment to be used this way. eof()
is used to figure out, why a previous read has failed. The
paradigm used in C++ for reading files is:

while( reading_data_wo rked ) {
process_data
}

// now figure out, why the above loop terminated. If it was
// because of eof(), then the file was read completely. If it was
// not because of eof() then the read operation has failed because
// of some error

if( ! eof() )
process_error

That's the way file reading is done in C++. It has the nice side effect
that it handles all types of file read problems easily (no disk, disk error,
network terminated, transmission error ...). To support this paradigm, look
at the return values of all type of read functions. All of them have a return
value which can immediatly be used to distinguish between 'read-worked' and
'read-failed'.

while( data_file.read( data, 2 ) )
std::cout << *data;

if( !data_file.eof( ) )
std::cout << "Error during read\n";

What about the 'magical constant' 2 in the argument list to read? What does
it denote? Well, it denotes the size of the buffer (and thus maximum amount
of data to be read). Wouldn't it be better, to not have to specify that number
literally, but instead have the compiler to figure it out?

char data[2];
while( data_file.read( data, sizeof( data ) )
...

Now, if you are going to change the buffer size from 2 to eg. 3 characters, you
simply do
char data[3];
and the number passed to read will be adjusted by the compiler

Another hint: When dealing with binary files, it is better to use unsigned char
and not plain vanilla char. The reason is: char may or may not be signed or
unsigned. It depends entirely on the compiler what gets choosen. If you explicitely
specify unsigned char you break free of this uncertainity. Besides: this most
often is indeed what you want to do: working with raw bytes. unsigned char is
perfect for that.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #3

"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:42******** *******@gascad. at...
ab*****@yahoo.c om.tw wrote:

while( data_file.eof() )
{
data_file.read( data, 2 );
}

Some additional hints.
Using eof() the way you do, is almost always a sure sign of
desaster. in C++ eof() is not ment to be used this way. eof()
is used to figure out, why a previous read has failed. The
paradigm used in C++ for reading files is:


Not only that, but his logic is reversed! Even if eof() could test for
end-of-file *before* the read, the above says "while end-of-file, read from
the file", which makes no sense at all. :-)

[By the way, I've looked through some of my old notes and books from college
(yes, I kept them :-)), and it seems that the "while not end of file"
concept is prevalent in examples. I suppose that, when moving from
pseudo-code to real code, a lot of programmers simply assume that the eof()
function will substitute directly for the pseudo-code. Obviously, in
standard C++, it won't. But I bet that's why so many (including me) start
out writing "while (!myfile.eof()) ", and only change it after seeing posts
like these.]

-Howard

Jul 23 '05 #4

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

Similar topics

7
2529
by: hugo.elias | last post by:
Hi all, I hope nobody minds me posting this question to this group, but I couldn't find any group closer to the Subject. Can anyone clear up where you draw the lines when dividing up an application into Model, View and Controller parts? For example: I have some classes: class FlatWorld;
28
3307
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical make-believe challenge in order to avoid confusing the issue further. Suppose I was hosting a dinner and I wanted to invite exactly 12 guests from my neighborhood. I'm really picky about that... I have 12 chairs besides my own, and I want them all...
6
3025
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect an html authoring tool to help you accomplish? 2. What do you expect from online help for a html authoring tool? 3. What audience do you think a freeware html authoring tool is directed towards?
4
1179
by: J | last post by:
I am a newbie in vb.net and gdi+, in my windows app, I using System.drawing to display image file, now when user view a multipage tiff file, they moved to 3rd page, then chose a new tiff file, it's display 3rd page instead of first page, I tried something but not works, so what I need to do? please help. Thank in advance.
1
9654
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
1
1707
by: Griff | last post by:
Hi I'm not sure of the best way to go about achieving my goal and would appreciate any advice. What I would like to do is to generate a control that can be dropped onto a web page. For example, a control that provided catalogue information. As I envisage this, the control would be given a single argument (the end user identifier) and it would return the required catalogue information as an HTML page "fragment".
1
54537
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and we instruct our experts to ignore any such PMs completely Be sure to give the version of Access that you are working with and the Platform and OS if applicable.
22
3278
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of seats as 0 and the flight no. is also repeating. If any can tell why is this please help me. #include<stdio.h> #include<ctype.h> #include<conio.h>
3
1854
by: Alami | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of seats as 0 and the flight no. is also repeating. If any can tell why is this please help me. #include<stdio.h> #include<ctype.h> #include<conio.h> #include<memory.h>
16
2781
by: gnawz | last post by:
I have a pagination function I am using in a file called functions.php as below<? //Pagination functions function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET) && (int)$_GET > 0) { $page = (int)$_GET; } else { $page = 1; } // start fetching from this row number $offset = ($page - 1) * $itemPerPage; return $sql . " LIMIT $offset, $itemPerPage"; } /* Get the links to navigate between...
0
9464
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10292
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.