输入文件名及路径创建该文件,并把从键盘输入的内容保存到该文件,最后将该文件的路径、该文件名及文件中的内容输出到屏幕。

 输入文件名及路径创建该文件,并把从键盘输入的内容保存到该文件,最后将该文件的路径、该文件名及文件中的内容输出到屏幕。

#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<<"请输入文件路径和文件名:"<<endl;
	cin.getline(arr,SIZE);
	//cin.getline(arr,SIZE,'#');//cin.getline(字符指针(char*),字符个数N(int),结束符(char));
	ofstream ofile(arr,ios_base::out);
	if(!ofile)
	{
		cout<<"error!"<<endl;
		return 0;
	}
	cout<<"请输入内容:"<<endl;
	getline(cin,str,'#');
	ofile<<str<<endl;
	getFileName(arr);
	getFilePath(arr);
	ifstream ifile(arr,ios_base::in);
	cout<<"文件内容:"<<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++]!= '\0')
    {
        if (arr[i]=='\\')
        {
            temp = i;
        }
	//	i++;
    }
	int k=0;
    for (i=temp+1; arr[i]!='\0'; i++)
    {
        fileName[k++]=arr[i];
    }
    fileName[k]='\0';
    cout<<"文件名称: "<<fileName<<endl;
}

void getFilePath(char arr[])
{
    int temp;
    int i=0;
    char filePath[SIZE];
    while(arr[i++]!= '\0')
    {
        if (arr[i]=='\\')
        {
            temp = i;
        }
		
    }

    for (i=0; i<temp; i++)
    {
        filePath[i]=arr[i];
    }
    filePath[temp]='\0';
    cout<<"文件路径: "<<filePath<<endl;
    //system("pause");
}


  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值