Question:
cstdio (stdio.h) ~c language ~ i don't understand?
inthisx_00
2011-12-15 04:26:23 UTC
http://www.cplusplus.com/reference/clibrary/cstdio/

cstdio (stdio.h)
C library to perform Input/Output operations

FILE
Object containing information to control a stream

The FILE is an Object. The fopen function return a pointer to FILE Object.

I wonder whether "FILE" is written in c++ language.Because c language has no OOP.

And can anyone explain what does the below code mean?
Why an Object can make a declaration of a Pointer?
I mean FILE is an object, and pFile is a Pointer.

FILE * pFile;
Three answers:
anonymous
2011-12-15 04:34:41 UTC
Stdio is the legacy C library for performing input / output to streams. A stream can be a file or the special streams, stdin, stdout and stderr.



stdin normally caputres keyboard input, stdout is the text output in the current Wndow, stderr writes to the OS error console.



FILE is not a C++ Object in this library, all the functions deal with low level C data structures. (Not ++). The term object in the help is in lower case, its just badly worded.





FILE *pFile; - declares a pointer variable called pFILE that points to a data structure of type FILE in memory.



The * donates that its a pointer variable.





A pointer simply holds and address in memory where the data can be found, it is the programmers responsiblity to make sure there is something in the memory location with the right structure and allocate / release the memory as required.



I guess you could say a struct is a bit like a class but its a lot less powerful. You can't have methods and the members of the struct aren't really properties in the true sense. Also you can't create sub classes.



A struct is really a template you place over some data in memory so that you can chop it up into logical variables so its not just random data.



You still have to manage the allocation and deallocation of memory manually for a struct, unlike an object derived from a class in c++.



e.g.



struct item

{

char name[50];

int quantity;

};



struct item* ip = (struct item*) malloc(sizeof(struct item)); //allocate memory



/* Do stuff with struct here */



free(ip); // release memory
anonymous
2011-12-15 05:23:00 UTC
No, "FILE" isn't an object, it's a type which can be implemented however the implementer of the compiler/c library wants. An object is something that has storage, for example:



int main(void)

{

int a;



return 0;

}



Defines a as an object of type int with automatic storage duration. "Object oriented programming" is a paradigm that can be used in both languages. C++ just encourages its use more than C.
D Bügger™
2011-12-15 05:15:05 UTC
cstdio is a c++ standard library header file which defines the functions traditionally defined in the Standard C library header for exclusive use within c++ language.Including this header also ensures that the names declared with external linkage in the Standard C library header are declared in the std namespace. In this implementation, the names may or may not also be declared in the global namespace, depending on the specific translation environment.

This works:-

#include

using namespace std;

int main (void)

{

std::printf(""HELLO!!)

}



But this won't work:-

#include //c library function

using namespace std;

int main (void)

{

std::printf("hello!");//note std used here causes error because it is not defined under stdio.h

return 0;

}



FILE is a structure in c language.File pointers are not used in C++ for handling file I/O instead we have the fstream class whose objects can be used to access i/o buffer..If you are writing C++ programs, you will usually want to use the C++ I/O system, but you are free to use the C file system if you like.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...