If I want to make a file to save my inputs in it, like what the user enters.
How do I create this file, edit it, delete from it.
I was told to use <fstream.h> but I dont know how.
So can you give me the codes please.

Thank you

Hello Nadia, thank you for the question.

Sorry it has taken so long for me to get back with you, I've been on vacation for the holidays.

For file i/o, I never liked the C++ version of it. It is more convoluted, and slower than the C functions. I'll show you the C style way of reading and writing to/from a file. Here is a list of handy functions you can use:

fopen, opens a file
fclose, closes a file
fprintf, prints text to a file
fscanf,  reads text from a file
fread, reads non text tokens from a file
fwrite, writes non text tokens to a file
fseek, seeks to a position in a file
ftell, tells how many chars long a file is

Here is a simple code example:

int main()
// open a file for writing, the "w" specifies for writing
FILE *file = fopen("MyFile.txt", "w");
// Failure opening the file?
std::cout << "Failed to open the file\n";
return -1;
fprintf(file, "This is a simple text file");

// Open the file now and read in the data, "r" specifies reading
file = fopen("MyFile.txt", "r");
std::cout << "Failed to open the file\n";
return -1;
const unsigned int size = 1024;
char data[size];
// parse the data in the file and store in data
fread(data, sizeof(char), size, file);
// print out the file contents
std::cout << "This text was in the file: " << data << '\n';
return 0;

And there is an example for you. You can look up the documentation on any of these functions in the MSDN for free at msdn.microsoft.com

I hope this information was helpful.

- Eddie


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2017 About.com. All rights reserved.