You are here:

C++/file in c

Advertisement


Question
Hi

i want to learn about file code,i wrote the fallowing code it doesn't have error but doesn't work
could you plz tell tell me what's the matter with it?

and my second question is that when we write:

fp=fopen("c:\\text","r");

we should create the "text" file in c directory?

and if yes.what kind of file we should make in c directory?(notepad,word...)

thanx
Bita

Answer
> it doesn't have error but doesn't work

that it compiles is no guarantee that it will not fail at run time.
on successful completion, fopen() return a FILE pointer. on failure, NULL is returned and the global variable errno is set to indicate the error.

> fopen("c:\\text","r");
> we should create the "text" file in c directory?

no file would be created. if the second argument (the 'mode' of opening) is "r" or "r+", the file is being opened for reading and therefore must be an already existing file.
see http://www.freebsd.org/cgi/man.cgi?query=fopen&apropos=0&sektion=3&manpath=FreeB

> what kind of file we should make in c directory?(notepad,word...)

as far as C or C++ is concerned, a file is just a sequence of bytes. it does not distinguish between different 'kinds' of files other than text files (escape sequences like '\n' may need translation) and binary files (no such translation is required).  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.