Created
September 24, 2020 10:30
-
-
Save vbuterin2/afcd429e05449828a877009e08b305d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <iostream> | |
#include <string> | |
#include <fstream> | |
int main() | |
{ | |
char directory[_MAX_PATH + 1]; | |
char filename[_MAX_PATH + 1]; | |
// 输入一个目录名,包含斜杠 | |
std::cout << "Please provide a directory Path (Including the ending slash): "; | |
std::cin.getline(directory, _MAX_PATH); | |
std::cout << "\nPlease provide a name for file: "; | |
std::cin.getline(filename, _MAX_PATH); | |
// 连接目录和文件名 | |
strcat_s(directory, filename); | |
// 定义输出文件流 | |
std::ofstream file_out(directory); | |
// 如果输出文件流为空指针,报错退出 | |
if (!file_out) { | |
std::cout << "Could not open."; | |
return -1; | |
} | |
// 目录成功创建 | |
std::cout << directory << "Was created\n"; | |
for (int i = 0; i <= 5; ++i) { | |
std::cout << "(Press ENTER to EXIT): Please enter a line of text you would like placed, in the document : "; | |
char nest[100], * p; | |
char* next = NULL; | |
// 使用std::cin.getline往nest字符数组中读入输入行 | |
std::cin.getline(nest, _MAX_PATH); | |
// 切割输入行 | |
p = strtok_s(nest, "\n|\t|\r", &next); | |
while (p != nullptr) { | |
// 切割输入行 | |
p = strtok_s(next, "\n|\t|\r", &next); | |
std::cout << " The line ("; | |
// 写入文件 | |
file_out << nest << std::endl; | |
std::cout << nest; | |
std::cout << ")Line was successfully added.\n"; | |
} | |
} | |
// 关闭文件 | |
file_out.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment