Enter a filename and path to create the file, save the input from the keyboard to the file, and finally output the path to the file, the file name, and the contents of the file to the screen.

Source: Internet
Author: User

Enter a filename and path to create the file, save the input from the keyboard to the file, and finally output the path to the file, the file name, and the contents of the file to the screen.

#include <iostream> #include <fstream> #include <string> using namespace std;
int const SIZE=80;
void GetFileName (char arr[]);

void GetFilePath (char arr[]);
	int main () {char arr[size];
	String str;
	cout<< "Please enter file path and filename:" <<endl;
	Cin.getline (arr,size);
	Cin.getline (Arr,size, ' # ');//cin.getline (character pointer (char*), number of characters n (int), Terminator (char));
	Ofstream ofile (arr,ios_base::out); if (!ofile) {cout<< "error!"
		<<endl;
	return 0;
	} cout<< "Please enter content:" <<endl;
	Getline (Cin,str, ' # ');
	ofile<<str<<endl;
	GetFileName (arr);
	GetFilePath (arr);
	Ifstream ifile (arr,ios_base::in);
	cout<< "File content:" <<endl;
		while (!ifile.eof ()) {getline (IFILE,STR);
	cout<<str;
	} cout<<endl;
return 0;
    } void GetFileName (char arr[]) {int temp,j;
    int i=0;
    Char Filename[size];
        while (arr[i++]!= ') {if (arr[i]== ' \ \ ') {temp = i;
    }//i++;
    } int k=0; for (i=temp+1; arr[i]!= '); i++) {Filename[k++]=arr[i];
    } filename[k]= ' + ';

cout<< "file name:" <<fileName<<endl;}
    void GetFilePath (char arr[]) {int temp;
    int i=0;
    Char Filepath[size];
        while (arr[i++]!= ') {if (arr[i]== ' \ \ ') {temp = i;
    }} for (i=0; i<temp; i++) {filepath[i]=arr[i];
    } filepath[temp]= ' + ';
    cout<< "File path:" <<filePath<<endl;
System ("pause"); }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.