473,465 Members | 4,339 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trouble with FILE

Hallo allemaal,
I created a program where I opened a file to read from. For various
reasons I had to split up this program in several modules. Variables
declared in one module are made accessible to other modules using an h-
file. The troubleshooter in this case are some file pointers to open
several files at the same time for reading or writing. I can declare,
let's say, an integer in a module as "int Counter;" and then as
"extern int Counter;" in the h-file. But for one or another reason
that doesn't work for "FILE *fp1;". The documentation I have has only
examples where FILE is only used in one module.

Who can give me a hint? Many thanks!

--
___
/ __|__
/ / |_/ Groetjes, Ruud
\ \__|_\
\___| URL: Ruud.C64.org
Oct 27 '08 #1
20 1442
Ruud wrote:
Hallo allemaal,
I created a program where I opened a file to read from. For various
reasons I had to split up this program in several modules. Variables
declared in one module are made accessible to other modules using an h-
file. The troubleshooter in this case are some file pointers to open
several files at the same time for reading or writing. I can declare,
let's say, an integer in a module as "int Counter;" and then as
"extern int Counter;" in the h-file. But for one or another reason
that doesn't work for "FILE *fp1;". The documentation I have has only
examples where FILE is only used in one module.

Who can give me a hint? Many thanks!
Hint #1: You have made a mistake.

Hint #2: You have not shown us any of your code, nor have you
described in what way "that doesn't work," so the best diagnosis
anyone can offer is based entirely on guesswork.

Hint #3: I guess your problem is described in Hint #1.

--
Er*********@sun.com
Oct 27 '08 #2
Ruud <Ru************@apg.nlwrites:
Hallo allemaal,
Goede avond,
I created a program where I opened a file to read from. For various
reasons I had to split up this program in several modules. Variables
declared in one module are made accessible to other modules using an h-
file. The troubleshooter in this case are some file pointers to open
several files at the same time for reading or writing. I can declare,
let's say, an integer in a module as "int Counter;" and then as
"extern int Counter;" in the h-file. But for one or another reason
that doesn't work for "FILE *fp1;". The documentation I have has only
examples where FILE is only used in one module.

Who can give me a hint? Many thanks!
There is nothing which prevent to have a global FILE* variable, so I wonder
what's the precise problem you're meeting.

But note that usually, global variables are a bad idea.

Yours,

--
Jean-Marc
Oct 27 '08 #3
Ruud <Ru************@apg.nlwrites:
Hallo allemaal,
I created a program where I opened a file to read from. For various
reasons I had to split up this program in several modules. Variables
declared in one module are made accessible to other modules using an h-
file. The troubleshooter in this case are some file pointers to open
several files at the same time for reading or writing. I can declare,
let's say, an integer in a module as "int Counter;" and then as
"extern int Counter;" in the h-file. But for one or another reason
that doesn't work for "FILE *fp1;". The documentation I have has only
examples where FILE is only used in one module.
I don't quite understand your description. The usual approach is to have

extern FILE *fp1;

in a header (.h) file which is included into your source (.c) files, and
then

FILE *fp1;

in exactly one of your .c files. Also, make sure that <stdio.his
included in the header file, so that the FILE type is defined.

When you say "that doesn't work", what exactly do you mean? What
happens? If there's an error message, what exactly does it say?

By the way, you might find

http://www.catb.org/~esr/faqs/smart-questions.html

helpful in framing questions to get good answers.
Oct 27 '08 #4
I created a program where I opened a file to read from. For various
reasons I had to split up this program in several modules. Variables
declared in one module are made accessible to other modules using an h-
file. The troubleshooter in this case are some file pointers to open
several files at the same time for reading or writing. I can declare,
let's say, an integer in a module as "int Counter;" and then as
"extern int Counter;" in the h-file. But for one or another reason
that doesn't work for "FILE *fp1;". The documentation I have has only
examples where FILE is only used in one module.

Who can give me a hint? Many thanks!
You need to make sure your other modules have stdio.h and stdlib.h included
so they know what FILE means. that way you can do extern file *fp in the
other modules.
Oct 28 '08 #5
On Oct 27, 9:03 pm, Ruud <Ruud.Baltis...@apg.nlwrote:
Hallo allemaal,
You're posting in comp.lang.c, you're not e-mailing your friend.
Have you realized this yet?
I created a program where I opened a file to read from. For various
reasons I had to split up this program in several modules. Variables
declared in one module are made accessible to other modules using an h-
file. The troubleshooter in this case are some file pointers to open
several files at the same time for reading or writing. I can declare,
let's say, an integer in a module as "int Counter;" and then as
"extern int Counter;" in the h-file. But for one or another reason
that doesn't work for "FILE *fp1;". The documentation I have has only
examples where FILE is only used in one module.

Who can give me a hint? Many thanks!
A hint for? What's your problem? You say something doesn't work.
That's about it.
Oct 28 '08 #6
vi******@gmail.com writes:
On Oct 27, 9:03 pm, Ruud <Ruud.Baltis...@apg.nlwrote:
Hallo allemaal,

You're posting in comp.lang.c, you're not e-mailing your friend.
Have you realized this yet?
Probably, the translation from Dutch is something like "Hi everybody".

Yours,

--
Jean-Marc
Oct 28 '08 #7
On 27 Oct, 19:03, Ruud <Ruud.Baltis...@apg.nlwrote:
I created a program where I opened a file to read from. For various
reasons I had to split up this program in several modules.
ok
Variables
declared in one module are made accessible to other modules using an h-
file.
this is often a sign of bad design. Try to keep your global variables
(variables with external linkage) down to a bare minimum. I tend to
only
have a control flags (diagnostics_enabled) and logging streams as
global
variables. And even the logging stream can be hidden in a function.
The troubleshooter in this case
I'm not sure what you mean by "troubleshooter". The way I use
the term a troubleshooter *fixes* problems not causes them!

are some file pointers to open
several files at the same time for reading or writing. I can declare,
let's say, an integer in a module as "int Counter;" and then as
"extern int Counter;" in the h-file. But for one or another reason
that doesn't work for "FILE *fp1;". The documentation I have has only
examples where FILE is only used in one module.

Who can give me a hint? Many thanks!
/*** filestream.h */
#ifndef FILESTREAM_H
#define FILESTREAM_H
extern FILE *log_stream;
void log (void);
#endif
/*** pippo.c */
#include <stdio.h>
#include "filestream.h"

void log (void)
{
fprintf (log_stream, "log message\n");
}

/**** main.c */
#include <stdio.h>
#include "filestream.h"

int main (void)
{
log_stream = fopen ("log.txt", "w"); /* check for error */
log();
return 0;
}

but better would be to pass the stream to log

void log (FILE* log_stream);
or hide it in pippo

void log (void)
{
static FILE* log_stream = 0;

if (log_stream == 0)
{
log_stream = fopen ("log.txt", "w");
/* check for error */
}

fprintf (log_stream, "log message\n");
}

or pippo.c could have an open function. This makes the design
kind of OO.

--
Nick Keighley

smart pointers are no picnic, as are virtually all
automatic devices with something like "smart",
"simple" or "fast" in their name.
(C++ Frequently Questioned Answers (FQA))
Oct 28 '08 #8
On 27 Oct, 19:03, Ruud <Ruud.Baltis...@apg.nlwrote:
Hallo allemaal,

I created a program where I opened a file to read from. For various
reasons I had to split up this program in several modules. Variables
declared in one module are made accessible to other modules using an h-
file. The troubleshooter in this case are some file pointers to open
several files at the same time for reading or writing. I can declare,
let's say, an integer in a module as "int Counter;" and then as
"extern int Counter;" in the h-file. But for one or another reason
that doesn't work for "FILE *fp1;". The documentation I have has only
examples where FILE is only used in one module.

Who can give me a hint? Many thanks!

dammit. I forgot to define log_stream

add this line to pippo.c. Not nested in a function

FILE* log_stream;

Oct 28 '08 #9
Nick Keighley said:

<snip>
/*** filestream.h */
#ifndef FILESTREAM_H
#define FILESTREAM_H
extern FILE *log_stream;
void log (void);
#endif
I'd avoid using log as a function name if I were you.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 28 '08 #10
Hallo allemaal,
@vipps...@gmail.com:
Jean-Marc just won the jackpot: "hallo allemaal" just means "hi
everybody".
For everybody else: thank you very much for the massive response!
Eric wrote:
Hint #1: You have made a mistake.

Hint #2: You have not shown us any of your code, nor have you
described in what way "that doesn't work," so the best diagnosis
anyone can offer is based entirely on guesswork.

Hint #3: I guess your problem is described in Hint #1.
Rereading my own post I only can say you're right. Being Dutch has its
advantages but not being a native English speaker makes it sometimes
difficult to put things on paper in English, certainly when it matters
a more technical subject. And in particular if you even have troubles
to phrase things in your own language.
So believe me, I appreciate it very much if you help me.
The program what I'm writing is a rewrite of my Multi Processor
Assembler, originally written in Borland Pascal. I want to convert to
C using Borland C. The main module, MP_ASM.C, calls module MP_ASM01.C
and, depending on what processor is choosen, MP_ASM02.c, MP_ASM03.C or
MP_ASM04.C. But these last three modules can also call module
MP_ASM01.C. The main module opens the ASM file and start reading from
it. Then it gives control to one of the other modules. These can read
from the ASM file and, if needed, even let MP_ASM01.C read from the
ASM file. Beside the already mentioned files I have MP_ASM09.C that
only contains the needed global variables.

As more then one module needs to read from the same file, I declared
"FILE *ft;" in MP_ASM09.C and "extern FILE *ft;" in MP_ASM.H.

MP_ASM01.C:

FILE *ft, *ft2, *ft3,
*ftN, *ftN2, *fh,
*ftl;
MP_ASM.H:

extern FILE
*ft, *ft2, *ft3,
*ftN, *ftN2, *fh,
*ftl;
The reaction of Borland C:
Borland C++ Version 3.1 Copyright (c) 1992 Borland International
mp_asm09.c:
Error mp_asm09.c 64: Declaration syntax error
*** 1 errors in Compile ***
rb_unit.c:
mp_asm01.c:
mp_asm.c:

Available memory 4113592
Press any key to continue . . .
What I really don't understand is the fact that I have declared more
globals in the same way without any problem.
Jean-Marc wrote:
But note that usually, global variables are a bad idea.
I know this as well but I will have a look later how to get rid of all
the globals.
As said before, any help is welcome and appreciated!
--
___
/ __|__
/ / |_/ Groetjes, Ruud ( = kind regards, Ruud :)
\ \__|_\
\___| URL: Ruud.C64.org

Oct 29 '08 #11
Ruud <Ru************@apg.nlwrites:
@vipps...@gmail.com:
Jean-Marc just won the jackpot: "hallo allemaal" just means "hi
everybody".
Not too difficult for a Belgian -- even if my mother language is french and
my dutch is not as good as it was.
As more then one module needs to read from the same file, I declared
"FILE *ft;" in MP_ASM09.C and "extern FILE *ft;" in MP_ASM.H.

MP_ASM01.C:

FILE *ft, *ft2, *ft3,
*ftN, *ftN2, *fh,
*ftl;
MP_ASM.H:

extern FILE
*ft, *ft2, *ft3,
*ftN, *ftN2, *fh,
*ftl;
Still incomplete files... try to show a minimal program reproducing the
problem.

Guess: you don't

#include <stdio.h>

in MP_ASM.H

Yours,

--
Jean-Marc
Oct 29 '08 #12
Ruud <Ru************@apg.nlwrites:
<snip>
As more then one module needs to read from the same file, I declared
"FILE *ft;" in MP_ASM09.C and "extern FILE *ft;" in MP_ASM.H.

MP_ASM01.C:

FILE *ft, *ft2, *ft3,
*ftN, *ftN2, *fh,
*ftl;
MP_ASM.H:

extern FILE
*ft, *ft2, *ft3,
*ftN, *ftN2, *fh,
*ftl;
The reaction of Borland C:
Borland C++ Version 3.1 Copyright (c) 1992 Borland International
mp_asm09.c:
Error mp_asm09.c 64: Declaration syntax error
*** 1 errors in Compile ***
rb_unit.c:
mp_asm01.c:
mp_asm.c:
*Please* post the code. The error seems to be in mp_asm09.c but you
show us mp_asm01.c above (and oply a fragment at that). There is no
error there so how can that help? I can guess 1000 reasons why you
get this error but why not just post what is really in the file?

--
Ben.
Oct 29 '08 #13
In article <9e**********************************@8g2000hse.go oglegroups.comRuud <Ru************@apg.nlwrites:
....
As more then one module needs to read from the same file, I declared
"FILE *ft;" in MP_ASM09.C and "extern FILE *ft;" in MP_ASM.H.

MP_ASM01.C:

FILE *ft, *ft2, *ft3,
*ftN, *ftN2, *fh,
*ftl;
MP_ASM.H:

extern FILE
*ft, *ft2, *ft3,
*ftN, *ftN2, *fh,
*ftl;
You show it as declared in MP_ASM01.C, not in MP_ASM09.C.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Oct 29 '08 #14
Ruud wrote:
[...]
As more then one module needs to read from the same file, I declared
"FILE *ft;" in MP_ASM09.C and "extern FILE *ft;" in MP_ASM.H.

MP_ASM01.C:

FILE *ft, *ft2, *ft3,
*ftN, *ftN2, *fh,
*ftl;
MP_ASM.H:

extern FILE
*ft, *ft2, *ft3,
*ftN, *ftN2, *fh,
*ftl;
The reaction of Borland C:
Borland C++ Version 3.1 Copyright (c) 1992 Borland International
mp_asm09.c:
Error mp_asm09.c 64: Declaration syntax error
Again, it would have been better if you had provided actual,
complete code instead of little snips and abstracts. However,
I think I can guess what is wrong here, and it's what Nate
Eldredge suspected: You are using the identifier `FILE' without
including the <stdio.hheader that declares it.

Unlike `int' and `double' and so on, which are built into
the C language and require no further definition, things like
`FILE' are "derived types" that must be declared before you can
use them. The <stdio.hheader contains the declaration for
`FILE', in a form that eventually resolves to some kind of
collection of primitive types, quite often a `struct' containing
several primitive elements. In any module that uses `FILE',
you must arrange to include <stdio.hfirst so the compiler will
know what `FILE' means.

There are two widely-used ways to get this to happen:

1) Since MP_ASM.H depends on having `FILE' declared, it
should #include <stdio.hitself.

2) In every file that has #include "MP_ASM.H", make sure
that #include <stdio.happears before it.

Personally, I favor style (1), but there are people who
prefer (2). Chacun à son goût.

--
Er*********@sun.com
Oct 29 '08 #15
Ben Bacarisse wrote:
Ruud <Ru************@apg.nlwrites:

<snip>
>As more then one module needs to read from the same file, I declared
"FILE *ft;" in MP_ASM09.C and "extern FILE *ft;" in MP_ASM.H.
.... snip ...
>
*Please* post the code. The error seems to be in mp_asm09.c but you
show us mp_asm01.c above (and oply a fragment at that). There is no
error there so how can that help? I can guess 1000 reasons why you
get this error but why not just post what is really in the file?
I don't think this is wise. It sounds too large for posting in the
newsgroup. Instead, try mounting the code files (possibly in a
zip) on some system and posting the access URL in the group.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 30 '08 #16
CBFalconer <cb********@yahoo.comwrites:
Ben Bacarisse wrote:
>Ruud <Ru************@apg.nlwrites:

<snip>
>>As more then one module needs to read from the same file, I declared
"FILE *ft;" in MP_ASM09.C and "extern FILE *ft;" in MP_ASM.H.
... snip ...
>>
*Please* post the code. The error seems to be in mp_asm09.c but you
show us mp_asm01.c above (and oply a fragment at that). There is no
error there so how can that help? I can guess 1000 reasons why you
get this error but why not just post what is really in the file?

I don't think this is wise. It sounds too large for posting in the
newsgroup.
The error was on line 62, I think. I'd settle for the first 62 lines
and any headers.

--
Ben.
Oct 30 '08 #17
Hallo allemaal,
First regarding the codefragment from MP_ASM01.C: the code is indeed
from MP_ASM09 but I made a typo :((( I'm terribly sorry because I
understand very well that this caused loss of your valuable time.
Bonjour Jean-Marc,

Une advantage d'etre Hollandais est on peut apprendre Francais à
l'ecole :)
One advantage of being Dutch is that you can learn French at school :)
(although it is a bit rusty after 30 years)

Guess: you don't

#include <stdio.h>

in MP_ASM.H
You were very close, it was missing in MP_ASM09.C. In some way a very
stupid error, my only excuse is that in Pascal I can use 'file'
without including any, so called, unit.
Thank you very much!
And for the others: thank you you very much as well, especially for
your Patience!
--
___
/ __|__
/ / |_/ Groetjes, Ruud
\ \__|_\
\___| URL: Ruud.C64.org


Oct 30 '08 #18
Ruud <Ru************@apg.nlwrites:
Guess: you don't

#include <stdio.h>

in MP_ASM.H

You were very close, it was missing in MP_ASM09.C.
It is far better to write your header files in such a way that they don't
depend on having other headers included before them. BTW, a trick to
ensure this is the following: each implementation file (.c) provides
definitions for declarations comming from only one header file and that
header file is included first.

Yours,

--
Jean-Marc
Oct 30 '08 #19
Ruud wrote:
....
>Guess: you don't

#include <stdio.h>

in MP_ASM.H

You were very close, it was missing in MP_ASM09.C. In some way a very
stupid error, my only excuse is that in Pascal I can use 'file'
without including any, so called, unit.
MP_ASM.H uses FILE. Therefore, MP_ASM.H is the place where you should
#include <stdio.h>. Don't force the user of a header file to have to
remember to #include something that the header file itself needs.
Following this rule consistently will save you a lot of grief in the future.
Oct 30 '08 #20
Hallo Jean-Marc, James,

BTW, a trick to ensure this is the following:....
Thanks, for this info! That simplifies things indeed. Never thought
about that because this trick is not possible in Pascal.
--
___
/ __|__
/ / |_/ Groetjes, Ruud
\ \__|_\
\___| URL: Ruud.C64.org


Oct 31 '08 #21

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

Similar topics

4
by: somaboy mx | last post by:
hi, I'm on winXPpro / apache 1.2 / php 4.4.x I'm experimenting with writing and reading from textfiles via php. I can create a file with fopen, write to it, but I'm having trouble reading...
2
by: James | last post by:
Hi I am having some trouble getting a asp page to load. Im a noob to the asp side. I have followed knowledege base Article 301305. I am running 2000 adv, IIS 5.0 I have the following...
1
by: Andre Ranieri | last post by:
I'm having trouble programatically inserting an Excel file into an Image column in our CRM package's SQL 2000 database. The function appears to work ok, but when I attempt to access the file through...
3
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : ...
1
by: Westbrook, Christopher L (WESTBCL04) | last post by:
I am having some trouble, and I can't figure out why. I'm trying to use the simple keylogger project from source forge, and then create a web interface to open the resulting files, so people from...
2
by: spidey12345 | last post by:
what i need this program to do is to read paragraphs like "st blah blh test ere se sit blha eere w" and then it will reformat to "st blah...
4
by: Rico | last post by:
Hello, I have an MDE application where I use a bound object frame to display an image. This frame is updatable and bested on the contents of an OLE field. My problem is, some images display as...
0
grassh0pp3r
by: grassh0pp3r | last post by:
Hello, I'm trying to make a very simple comments page on my site using PHP and am having problems somewhere. I am very new to PHP. I was able to create one that works with comments appended, but...
3
by: suryawati | last post by:
Dear All, I am running a java serial program ( Black Box on commapi package ) on a Linux. But when Main Menu can displayed, automatically program stop for running and I can't do anything with...
2
blazedaces
by: blazedaces | last post by:
I'm designing a GUI for autonomous robot interaction, but that's not so important. To figure out the ins and outs of a certain aspect of PyQt I usually just do what I did when I learning Qt, I grab...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...

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.