我想从用户那里获取一个输入,并在输入后输入一个文件名,我将在我的代码上写。
如果你能帮助我,我将不胜感激。
提前致谢!
答案 0 :(得分:3)
<强> MATLAB 强>
filename = input('File name: ','s');
fileID = fopen(filename)
fprintf(fileID, 'That wasn''t so hard')
fclose(fileID)
<强>的Python 强>
filename = raw_input('File name: ')
with open(filename, 'w') as f:
f.write('That wasn\'t so hard')
<强> C 强>
#include <stdio.h>
int main() {
char filename[81];
FILE* f;
while (1) {
printf("File name: ");
if (scanf("%80s") == 1) break;
}
f = fopen(filename, "w");
if (!f) {
perror("opening file");
return 1;
}
fprintf(f, "That wasn't so hard\n");
fclose(f);
return 0;
}
sh script
#!/bin/sh
echo -n "File name: "
read filename
echo "That wasn't so hard" > $filename