Computing.Net > Forums > Programming > how to creat a NEW .dat file in C?

how to creat a NEW .dat file in C?

Reply to Message Icon

Original Message
Name: Jack Bram
Date: November 23, 2002 at 20:19:24 Pacific
Subject: how to creat a NEW .dat file in C?
OS: Win2000
CPU/Ram: 160MB
Comment:

hello, does anyone know how to creat a new .dat file by using C?


Report Offensive Message For Removal


Response Number 1
Name: Don Arnett
Date: November 23, 2002 at 22:29:05 Pacific
Subject: how to creat a NEW .dat file in C?
Reply: (edit)

To open a new file, use

FILE fp;

fp = fopen("filename", "w");
if (fp == NULL) {
/* the open failed */
exit();
}

The "w" says to open the file in "write" mode, which causes it to make a new file, even overwriting the file if it already exists.

If you want a .dat file, just set the filename to include a .dat ("filename.dat")


The normal way to write the above code is:

if ((fp = fopen("filename","w")) == NULL) {
fprintf("Failed to open the file\n");
exit();
}

You'd have to include stdio.h (for the fopen() and fprintf() and stdlib.h (for the exit())


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: how to creat a NEW .dat file in C?

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge