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
Dear Bita:

http://www.lysator.liu.se/c/rat/d9.html

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.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Titus B. Ledbetter, Jr.

Expertise

C , MFC, Object Oriented, Artificial Intelligence

Experience

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.

Education/Credentials
Master's Degree Computer Science from Johns Hopkins

©2016 About.com. All rights reserved.