read operation in c++

F

fazulu deen

Hi all,

I have to read two text file(opcode,jump)...

I guess the following statements are correct(but it is not reading
the data
from that files and also not checking the presence of the files,why
so??)

unsigned h, i=0;

FILE *fp = fopen("opcode.txt","r");

if(fp)

{

while(!feof(fp))

{

fscanf(fp,"%x", &h);

mem_opcode[i++] = h;

}

}

else

printf("Can not open %s file\n","opcode.txt");




unsigned h, i=0;

FILE *fp = fopen("jump.txt","r");

if(fp)

{

while(!feof(fp))

{

fscanf(fp,"%x", &h);

mem_opcode[i++] = h;

}

}

else

printf("Can not open %s file\n","jump.txt");



I have only included iostream


whether the above statements for read operation is correct??Anything i
missed....pls suggest
the modification....

regards,
fazal
 
K

kingfox

Hi all,
FILE *fp = fopen("opcode.txt","r");


I have only included iostream

whether the above statements for read operation is correct??Anything i
missed....pls suggest
the modification....

If you want use fopen(), you should include <cstdio> in .CPP file or
include <stdio.h> in .C file.
 
F

fazulu deen

If you want use fopen(), you should include <cstdio> in .CPP file or
include <stdio.h> in .C file.

I have included <cstdio>,but still it is not reading and opening the
file....I am using gcc 3.2.3
 
G

Guest

I have included <cstdio>,but still it is not reading and opening the
file....I am using gcc 3.2.3

Are you sure you have the files in the right directory?
 
F

fazulu deen

Are you sure you have the files in the right directory?

Yes i am having in my working directory....I am afraid why the
compiler is not able to identify if the file is not in the directory
also...then wat is the function of this??

FILE *fp = fopen("opcode.txt","r");
 
I

Ian Collins

fazulu said:
Yes i am having in my working directory....I am afraid why the
compiler is not able to identify if the file is not in the directory
also...then wat is the function of this??

FILE *fp = fopen("opcode.txt","r");
It looks like you have a C problem, try comp.lang.c for more comments.
The C++ "way" would be to use fstreams.
 
J

Jack Klein

It looks like you have a C problem, try comp.lang.c for more comments.
The C++ "way" would be to use fstreams.

I will use small words so you can follow:

IT IS NOT A C PROBLEM.

THE FUNCTION fopen() IS A PART OF THE C++ STANDARD LIBRARY. IT'S USE
IN A C++ PROGRAM IS PERFECTLY VALID, LEGAL, AND, if used correctly,
STRICTLY CONFORMING.

Believe it or not, the C standard says absolutely nothing at all about
the use of fopen() in a C++ program. The use of a function named
fopen() in a C++ program is completely undefined by ISO 9899, as is,
in fact, everything else that might be done in a C++ program.

It happens to be the C++ standard that decided to borrow much of the C
language and its standard library, without so much as a "by your
leave". It is up to the C++ standard to define what a library
function "inherited" from C does in a C++ program.

In fact the use of a function named fopen() IN A C++ PROGRAM is
completely off-topic in comp.lang.c.

The fact that some C++ user has a problem using a C++ library function
that happens to be "inherited" from C does not make it a C problem.
Not even if you prefer a C++ alternative not inherited from C.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
I

Ian Collins

Jack said:
I will use small words so you can follow:
Oh grow up, the OP's code could have been C or C++, so I pointed him at
clc to get some more input, that's all.
 
J

James Kanze

Oh grow up, the OP's code could have been C or C++, so I pointed him at
clc to get some more input, that's all.

You're both wrong:).

Seriously: fopen is a part of C++, and there are even times when
I'd use it. Say, for example, if I had to pass the resulting
FILE* to legacy code in C. Of course, that example is precisely
the reason it is part of C++; it's not there to be used in pure
C++ code. In this group, any answer to the OP's question should
start by converting his code to idiomatic C++, using ifstream,
etc. Using fopen may be conform to the standard, but it's not
good C++. (A bit like using gets() in standard C.)

As it happens, the problem he seemed to be having (the open
failing) is probably totally unrelated to either C or C++; if
fopen can't open the file, then ifstream won't be able to
either. Nor, most likely, their equivalent in Ada, Fortran, or
whatever other language.

Note that istream and FILE* do share a common idiom, and there
was one blatant error in his code---the way he used
feof()---which is common with istream as well. Neither feof() nor
ios::eof() are particularly useful until after the input has
failed, and their use in a while is almost always an error.
(This is less true of feof(), but the while in his code is still
wrong.)
 
D

Default User

Ian Collins wrote:

Oh grow up, the OP's code could have been C or C++, so I pointed him
at clc to get some more input, that's all.


Sorry, but you are incorrect. The original post said:
I have only included iostream

Clearly the OP intended the program to be C++, whether he was doing it
the best way or not. Sending him to clc was the WRONG thing to do.
Instructing him on the actual use of the C++ stream facilities would be
a legitimate answer.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,216
Latest member
DarrelLho

Latest Threads

Top