You are here:

C++/file in c



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:


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

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


Dear Bita:

Check the link above it will explain in more detail.

The reason nothing happened is probably that the file does not already exist.  If you want the code to work, you must first create a file using notepad, then run the program.  If you want to create a new file and write to it then you must use fp=fopen("c:text","w");
Use the link above to read about the other options you have of opening a file for read and write or write and read so you could do either or both.

The option "r" will not make a file in the directory if one does not alread exist.  In fact it will not change the file at all.  The option "w" will put a text file in the directory.  That is why you should edit and create it using notepad.


All Answers

Answers by Expert:

Ask Experts


Titus B. Ledbetter, Jr.


C , MFC, Object Oriented, Artificial Intelligence


I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Master's Degree Computer Science from Johns Hopkins

©2017 All rights reserved.

[an error occurred while processing this directive]