473,402 Members | 2,072 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,402 software developers and data experts.

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 16175
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********@yahoo.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**********@ntlworld.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********@yahoo.com> wrote in message
news:49**************************@posting.google.c om...
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********@yahoo.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**********@ntlworld.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********@yahoo.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.net
Nov 13 '05 #10

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

Similar topics

0
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. ...
10
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...
28
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();...
8
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...
6
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
by: Dic4000 | last post by:
ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË? Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷. #include<iostream> #include<conio.h> #include<fstream> using namespace std; int main()
3
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
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,...
1
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>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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...
0
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,...

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.