C语言实现保存文件到指定目录

目的:将输入内容保存在指定目录下的文件中

步骤:

①输入一个路径
②输入文字

效果如下:

在这里插入图片描述
代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<iostream>
#include<cstdio>
using namespace std;
#pragma warning(disable:4996)

int main()
{
		char s[70];
		char path[100];
		printf("Input a path:\n");
		scanf("%s",path);
		getchar();
		FILE* fp;
		//cin >> paths;
		if ((fp = fopen(path, "w")) == NULL) {
			printf("Open the file failure...\n");
			exit(0);
		}
		while (1) {
			printf("Input a string...\ns=");
			if (gets_s(s), strlen(s) < 70)
				break;
			printf("Too long, redo: ");
		}
		fputs(s, fp);
		fclose(fp);
		printf("\n");
		return 0;
}
发布了53 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/YUEXILIULI/article/details/103749771