Pawandeep Singh
Pawandeep Singh

Reputation: 880

Beginner C++ pointer

I was reading a code snippet and I saw this

FILE * infile = fopen("input.wav","rb");

I want to ask 2 Questions here.

  1. What is FILE here. Is it a class, object or anything else.

  2. What is infile pointing to. I have seen pointers to int, char etc. But what is that pointing to.

Upvotes: 1

Views: 155

Answers (7)

Saad Kamran
Saad Kamran

Reputation: 45

as pointed out by others

1. FILE is a Struct. in simpler terms struct is a data type

2.infile is the pointer of FILE type and is pointing to a an audio file which will be in 1's and 0's now depending on the type of variable you use to extract data with the stream will progress according to that data type e.g if you use int it will move 4 bytes and if you use char it will move 1 byte.

ps i wouldnt recommend using .wav for uderstanding the concepts make a simple txt and try to extract chars strings int and stuff

Upvotes: 0

FrankS101
FrankS101

Reputation: 2135

What is FILE here. Is it a class, object or anything else?

  • FILE is an object that identifies a stream and contains the information needed to control it, i.e. the position indicator, a pointer to the buffer and all the state indicators.

  • fopen returns a pointer to a FILE object.

  • The memory allocation of the FILE objects is automatically managed, i.e. it is the responsibility of the library to free the resources when the stream has been closed using fclose (or when the program terminates normally).

  • If you include the < cstdio > header file, these three FILE objects are automatically created:

    1. stdin: standard input stream
    2. stdout: standard output stream
    3. stderr: standard error stream

What is infile pointing to. I have seen pointers to int, char etc. But what is that pointing to?

It's pointing to that FILE object.

if you want to read from that file pointer do something like this:

if (infile == NULL) perror ("Error opening file");
else { /* read */ }

Upvotes: 1

Grant Shannon
Grant Shannon

Reputation: 5055

FILE is an object type that identifies a stream and contains the information needed to control it, including a pointer to its buffer, its position indicator and all its state indicators.

reference: http://www.cplusplus.com/reference/cstdio/FILE/

infile points to the start address of this object, in memory

Upvotes: 0

samnaction
samnaction

Reputation: 1254

FILE

A structure containing information about a file which is in stdio.h

infile

Points to object returned by fopen and its type is FILE

Upvotes: 0

TobiasR.
TobiasR.

Reputation: 801

  1. FILE ist the Type of the Object to which infile is pointing to.
  2. infile is pointing to an Object of type FILE (which was retuned by fopen())

Upvotes: 0

Chris Tsiakoulas
Chris Tsiakoulas

Reputation: 186

FILE is object containing information to control a stream and it's used when you want to work with files (fopen, fclose) in c++. The "infile" pointer, points to the memory address where "input.wav" will be placed by the fopen.

Upvotes: 0

Hatted Rooster
Hatted Rooster

Reputation: 36463

  1. FILE is a type coming from C, it's a struct used for controlling in and output of files. It contains information about the file handled, certain C functions specialized in handling files use this as a return type or parameter to know what they're dealing with.

  2. infile is pointing to an object created by fopen of type FILE that contains information regarding the file you just opened input.wav.

Upvotes: 0

Related Questions