TXT FILE

How to create a .txt FILE giving the user 4 options of saving it( different local disks C,D,E,F).

Many thanks!



1
2
3
4
5
6
7
8
9
10
11
12
13
14
  int main()
{    
    FILE *fp;
    
    char NF[100];
    
    cout<<" Enter file name = ";
    cin>>NF;
   
    fp = fopen("C:\\group.txt", "w+"); //that's for disk C
    
    if( fp == NULL ) {  cout<<" error W+ "<<endl; exit(0);  }
     else {  cout<<" w+ "<<endl; }
You need to pass the name entered by the user to the file function.
1
2
3
4
5
6
7
8
int main() {
    std::string filename;
    std::cout << "Enter file name: ";
    std::getline(std::cin, filename);

    std::fstream filestream(filename. std::ios::app);
    if (!fiestream) std::clog << "cannot open " << filename
}
Last edited on
Topic archived. No new replies allowed.