file name :D

hi.
As I know to make a file we should write like this:

FILE *fp=fopen("temp.txt", "w+");

but I want to for n students, get the name of the students from the user & for each student make a file with his/her name

how can i do this?! Is there another way to make files?!
fopen is very "C". You should look into the C++ ofstream class.

http://cplusplus.com/reference/fstream/ofstream/

Notice the constructor that takes a file name string.
closed account (18hRX9L8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 /* Created By Usandfriends */
#include <iostream>
#include <string.h>
#include <fstream>

#define MAXCHARS 100

main()
{
	int counter,numberofnames;
	char nameofstudent[MAXCHARS];
	
	std::cout<<"How many students' files are we adding?  ";
	std::cin>>numberofnames;
	
	for(counter=0;counter<numberofnames;counter++)
	{
		std::cout<<"Name of student + file extension?  ";
		std::cin>>nameofstudent;
		std::cout<<"Creating file."<<std::endl;
		std::fstream myfile;
		myfile.open (nameofstudent, std::fstream::in | std::fstream::out | std::fstream::app);
		myfile.close();
		std::cout<<"File created."<<std::endl;
	}
	std::cout<<"All files are ready to use.";
	std::cin.ignore();
}


***Please use this code only as an EXAMPLE.***
Last edited on
thx! :)
Topic archived. No new replies allowed.