在许多编程语言中,可以使用变量字符串作为文件名输出。以下是几个示例:
filename = "output.txt"
data = "Hello, World!"
with open(filename, "w") as file:
file.write(data)
var filename = "output.txt";
var data = "Hello, World!";
var fs = require("fs");
fs.writeFile(filename, data, function(err) {
if (err) {
console.log(err);
} else {
console.log("File written successfully.");
}
});
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
String filename = "output.txt";
String data = "Hello, World!";
try {
FileWriter file = new FileWriter(filename);
file.write(data);
file.close();
System.out.println("File written successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
#include
#include
using namespace std;
int main() {
string filename = "output.txt";
string data = "Hello, World!";
ofstream file;
file.open(filename);
if (file.is_open()) {
file << data;
file.close();
cout << "File written successfully." << endl;
} else {
cout << "Error opening file." << endl;
}
return 0;
}
请注意,这些示例仅为了演示目的,实际使用时可能需要添加错误处理和适应特定需求的其他代码。
上一篇:变量字符串中如何转义反引号?
下一篇:变量字节在汇编中设为48。