写一个输入为用鼠标拖入的.txt文件的C++所写编译的.exe文件的框架
时间: 2023-11-28 13:47:47 浏览: 182
c++之txt文件的读取和写入
以下是一个读取用鼠标拖入的.txt文件并输出其内容的C++程序的框架:
```cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
// Check if a file was dragged onto the executable
if (argc != 2) {
cout << "Please drag and drop a .txt file onto the executable!" << endl;
return 1;
}
// Open the file
ifstream file(argv[1]);
if (!file.is_open()) {
cout << "Failed to open file!" << endl;
return 1;
}
// Read and output the file line by line
string line;
while (getline(file, line)) {
cout << line << endl;
}
// Close the file and exit
file.close();
return 0;
}
```
当你将以上代码保存为.cpp文件并编译后,你可以将它拖到一个.txt文件上来运行。
阅读全文