473,785 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FILE undeclared - but its not, according to me.

Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project:

$ls
Makefile file1 isource2.c isource2.h source1.c

$cat source1.c
#include <stdio.h>
#include "isource2.h "
main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};

$cat isource2.h
int getval(void);

$cat file1
50

$cat Makefile
testtool: source1.o isource2.o
gcc -o source1 source1.o isource2.o
source1.o: source1.c isource2.h
gcc -g -c source1.c
isource2.o: isource2.c
gcc -g -c isource2.c

$make
gcc -g -c source1.c
gcc -g -c isource2.c
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
isource2.c:5: (Each undeclared identifier is reported only once
isource2.c:5: for each function it appears in.)
isource2.c:5: `fp' undeclared (first use in this function)
make: *** [isource2.o] Error 1
Don't understand, please explain - this should work, according to me
it's straigt forward! Isn't it????

Greetz..
Nov 13 '05 #1
9 16219
W. Van Hooste wrote:
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project:

$ls
Makefile file1 isource2.c isource2.h source1.c

$cat source1.c
#include <stdio.h>
#include "isource2.h "
main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};

$cat isource2.h
int getval(void);

$cat file1
50

$cat Makefile
testtool: source1.o isource2.o
gcc -o source1 source1.o isource2.o
source1.o: source1.c isource2.h
gcc -g -c source1.c
isource2.o: isource2.c
gcc -g -c isource2.c

$make
gcc -g -c source1.c
gcc -g -c isource2.c
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
isource2.c:5: (Each undeclared identifier is reported only once
isource2.c:5: for each function it appears in.)
isource2.c:5: `fp' undeclared (first use in this function)
make: *** [isource2.o] Error 1
Don't understand, please explain - this should work, according to me
it's straigt forward! Isn't it????

Greetz..


Change it so the command....
$cat source1.c

reads.......... ..
#include <stdio.h>
#include "isource2.h "
extern int getval(void);

main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

Nov 13 '05 #2
wv********@yaho o.com (W. Van Hooste) writes:
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project: [...] $cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};


The type `FILE' is declared in the standard header <stdio.h>. Since you
don't include that header in `isource2.c', `FILE' is in fact undeclared.

Martin
Nov 13 '05 #3
Materialised wrote:
W. Van Hooste wrote:
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project:

$ls
Makefile file1 isource2.c isource2.h source1.c

$cat source1.c
#include <stdio.h>
#include "isource2.h "
main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};

$cat isource2.h
int getval(void);

$cat file1
50

$cat Makefile
testtool: source1.o isource2.o
gcc -o source1 source1.o isource2.o
source1.o: source1.c isource2.h
gcc -g -c source1.c
isource2.o: isource2.c
gcc -g -c isource2.c

$make
gcc -g -c source1.c gcc -g -c isource2.c
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
isource2.c:5: (Each undeclared identifier is reported only once
isource2.c:5: for each function it appears in.)
isource2.c:5: `fp' undeclared (first use in this function)
make: *** [isource2.o] Error 1
Don't understand, please explain - this should work, according to me
it's straigt forward! Isn't it????

Greetz..

Change it so the command....
$cat source1.c

reads.......... ..
#include <stdio.h>
#include "isource2.h "
extern int getval(void);

main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

And also include stdio.h in the 2nd source file.

Nov 13 '05 #4
W. Van Hooste wrote:
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project:

$ls
Makefile file1 isource2.c isource2.h source1.c

$cat source1.c
#include <stdio.h>
#include "isource2.h "
main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

$cat isource2.c
#include <stdio.h>
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};

$cat isource2.h
int getval(void);

$cat file1
50

$cat Makefile
testtool: source1.o isource2.o
gcc -o source1 source1.o isource2.o
source1.o: source1.c isource2.h
gcc -g -c source1.c
isource2.o: isource2.c
gcc -g -c isource2.c

$make
gcc -g -c source1.c
gcc -g -c isource2.c
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
isource2.c:5: (Each undeclared identifier is reported only once
isource2.c:5: for each function it appears in.)
isource2.c:5: `fp' undeclared (first use in this function)
make: *** [isource2.o] Error 1
Don't understand, please explain - this should work, according to me
it's straigt forward! Isn't it????

See above.
If you don't #include <stdio.h>, the type `FILE' is, indeed,
undeclared. Remember, a declaration has to be visible in each
translation unit in which it is referenced.

HTH,
--ag

--
Artie Gold -- Austin, Texas

Nov 13 '05 #5
Materialised <ma**********@n tlworld.com> writes:
W. Van Hooste wrote:
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
Here is my Example project:
$ls
Makefile file1 isource2.c isource2.h source1.c
$cat source1.c
#include <stdio.h>
#include "isource2.h "
main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}
$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};
$cat isource2.h
int getval(void);
$cat file1
50
$cat Makefile
testtool: source1.o isource2.o
gcc -o source1 source1.o isource2.o
source1.o: source1.c isource2.h
gcc -g -c source1.c
isource2.o: isource2.c
gcc -g -c isource2.c
$make
gcc -g -c source1.c gcc -g -c isource2.c
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
isource2.c:5: (Each undeclared identifier is reported only once
isource2.c:5: for each function it appears in.)
isource2.c:5: `fp' undeclared (first use in this function)
make: *** [isource2.o] Error 1
Don't understand, please explain - this should work, according to me
it's straigt forward! Isn't it????
Greetz..


Change it so the command....
$cat source1.c

reads.......... ..
#include <stdio.h>
#include "isource2.h "
extern int getval(void);

main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}


Not only does this not solve the OP's problem, but it is also completely
unnecessary to declare `getval' twice.

Martin
Nov 13 '05 #6

"W. Van Hooste" <wv********@yah oo.com> wrote in message
news:49******** *************** ***@posting.goo gle.com...
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project:

$ls
Makefile file1 isource2.c isource2.h source1.c

$cat source1.c
#include <stdio.h>
#include "isource2.h "
main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};

$cat isource2.h
int getval(void);

$cat file1
50

$cat Makefile
testtool: source1.o isource2.o
gcc -o source1 source1.o isource2.o
source1.o: source1.c isource2.h
gcc -g -c source1.c
isource2.o: isource2.c
gcc -g -c isource2.c

$make
gcc -g -c source1.c
gcc -g -c isource2.c
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
isource2.c:5: (Each undeclared identifier is reported only once
isource2.c:5: for each function it appears in.)
isource2.c:5: `fp' undeclared (first use in this function)
make: *** [isource2.o] Error 1
Don't understand, please explain - this should work, according to me
it's straigt forward! Isn't it????

Try to remove "#include <stdio.h>" in source1.c, add it to your header
file, and include your header file in the two source files.

--
Jeff
--je6543 at yahoo.com
Nov 13 '05 #7
Martin Dickopp <ex************ *@zero-based.org> wrote in message news:<bk******* ******@news.t-online.com>...
wv********@yaho o.com (W. Van Hooste) writes:
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project:

[...]
$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};


The type `FILE' is declared in the standard header <stdio.h>. Since you
don't include that header in `isource2.c', `FILE' is in fact undeclared.

Martin


You nailed it. I did not expect i needed to include stdio.h, and
assumed that this one ALWAY was loaded by default - not! I was not
even aware that i should include it more than once and thought that 1
include of stdio.h in the main source was more than enough. Man, was i
mistaken - but i absorb and learn ;-)...

Thanks,
W. Van Hooste.
Nov 13 '05 #8
Materialised <ma**********@n tlworld.com> wrote in message news:<bk******* *****@ID-204621.news.uni-berlin.de>...
Materialised wrote:


<snipped example code>
Don't understand, please explain - this should work, according to me
it's straigt forward! Isn't it????

Greetz..

Change it so the command....
$cat source1.c

reads.......... ..
#include <stdio.h>
#include "isource2.h "
extern int getval(void);

main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

And also include stdio.h in the 2nd source file.


rather sadly, this doesn't fullfill the OP's request for
an explanation.

goose,
why "extern" ?
Nov 13 '05 #9
On 15 Sep 2003 17:20:38 -0700, wv********@yaho o.com (W. Van Hooste)
wrote:
<snip>
$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};

As others have said, you need to #include <stdio.h> in the source file
(translation unit) where you use FILE. And also where you use any of
fopen, fscanf, printf and fclose, although your compiler likely won't
give an error and perhaps not even a warning in these cases, and may
even generate code which works, but isn't required to and you can't
rely on it. The same is true for most other stdio routines; a handful
like getchar and putchar happen to work with the implicit declaration
in C90 only or a declaration you can write yourself without library
types, but it's not worth the trouble, just use <stdio.h> always.

You should check that the returned FILE* from fopen is non-null (!=
NULL or != 0) before using it, and should check the return value from
*scanf to make sure input was successful. In principle you should
also check the return value from fclose, although there are very few
ways for closing a read-only file to fail and you can't do anything
about them anyway; and could check the return from *printf but it's
hard(er) and especially for stdout you can't do anything useful.

*scanf %u is technically wrong for signed int, you should use %d,
although in practice this will almost certainly work.

There's no need to cast the return value to int; it already *is* int.
And the return statement will do the same (implicit) conversions as
assignment anyway.

- David.Thompson1 at worldnet.att.ne t
Nov 13 '05 #10

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

Similar topics

0
1998
by: pruebauno | last post by:
Hello all, I am having issues compiling Python with large file support. I tried forcing the configure script to add it but then it bombs in the make process. Any help will be appreciated. Information: Architecture: PowerPc on AIX version 5 Compiler:
10
2540
by: Toke H?iland-J?rgensen | last post by:
Hello. I am quite new to the c++ language, and am still trying to learn it. I recently discovered how using include files would allow me to split up my code into smaller segments, instead of having class definitions etc. in one big file (yay, major discovery...). My problem is this: When I define a class in one include file, and then try to instantiate it in another, I get compile-time errors saying the type is invalid. If i move the...
28
3279
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr(); fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
8
6646
by: noe | last post by:
Hello all devs!! I’m a student and I’m developing my Final Project in the University. I have to develop a driver for Windows XP that work so: I have a file in the HD (NTFS file system) of my PC and I want to copy it to the floppy disk (FAT16 file system). But I need that the file data in the floppy disk is modified (added 1 respect to the original value). For example: I have-> HD file data: ‘hello’
6
9892
by: Peter Rothenbuecher | last post by:
Hello, when I try to compile the following code with g++ -o client client.c #include<sys/socket.h> #include<stdio.h> #include<stdlib.h> #define ADDRESS "mysocket"; #define MAXLEN 200;
6
3119
by: Dic4000 | last post by:
ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË? Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷. #include<iostream> #include<conio.h> #include<fstream> using namespace std; int main()
3
7172
by: fazulu deen | last post by:
Hi all, For the following code : file_ptr = fopen("pass_fail.txt", "a"); // error line 393 fdisplay(file_ptr, "Test Passed"); fclose(file_ptr);
116
4637
by: dmoran21 | last post by:
Hi All, I am working on a program to take input from a txt file, do some calculations, and then output the results to another txt file. The program that I've written compiles fine for me, however, when I run it, it stalls and does nothing. I'm wondering if there's something obvious that I'm missing. My code is below and any help would be appreciated. Thanks, Dave
1
2531
by: mingke | last post by:
Hi... I've been trying to make a program where the input is txt file, and the output will be saved in txt file. But, I keep getting error Message. My code looks like this: #include <stdlib.h> #include <stdio.h> int main (){ int i,j; float H ;
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
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
10356
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...
1
10100
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
9959
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7509
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
5396
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.