A newbie here.

I try to:
Using MS Visual Studio 2005;
Programming some simple C programs;
Want to open a text file and read whatever it the file into a member of a struct.

First of all, which type is this "FILE" anyway?
Secondly, does the definition of "stdin" include this "FILE"?

I know for most of you, those two questions are very stupid, but I really appreciate any help you offer generously.


Recommended Answers

All 5 Replies

FILE (all caps) normally refers to C file i/o functions, such as fopen(), fgets(), fread(), fwrite() and several others. The word "file" (all lower case) generally describes what is actually stored on the disk -- each file has a filename and size.

>>Secondly, does the definition of "stdin" include this "FILE"?
stdin is a specific type of open FILE, normally refers to the keyboard. stdout and stderr normally refer to the monitor (what you see on the screen). I say normally because they can be changed to refer to standard disk files or to something else, like sockets, but that is somewhat rare.

Dry definition of FILE:

an object type capable of recording all the information needed to control a stream, including its file position indicator, a pointer to its associated buffer (if any), an error indicator that records whether a read/write error has occurred, and an end-of-file indicator that records whether the end of the file has been reached;

More legalese on streams:

Input and output, whether to or from physical devices such as terminals and tape drives, or whether to or from files supported on structured storage devices, are mapped into logical data streams, whose properties are more uniform than their various inputs and outputs. Two forms of mapping are supported, for text streams and for binary streams.

OK. I understand it better now.
Here comes my understanding:

Say, A very simple pseudo C program goes like:

FILE *stream;
char buffer[100];
int ch;
if ((stream=fopen("polygonData.txt","r")) == NULL)
printf("The file 'polygonData.txt' can not be opened.\n");
printf("The file 'polygonData.txt' was opened.\n"); 

The variable "stream" is a pointer of FILE type, which, in turn, a struct. The function "open" returns a valid value to stream if it opens the file "polygonData.txt" successfully. And in the "for" loop, the function getc receives the pointer of FILE type (aka stream) as its argument.

Am I right or there are still more to it. Please do not hesitate to point out anything wrong with my comments.


what you want is fgetc, not getc(). http://www.die.net/doc/linux/man/man3/fgetc.3.html

why not use the fgets() simpler function -- does everything you have in your loop

while( fgets(buffer,sizeof(buffer),stream) != NULL)
  // do something with the string


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.