如何输入文件名?

时间:2014-03-16 22:56:17

标签: matlab input

我想从用户那里获取一个输入,并在输入后输入一个文件名,我将在我的代码上写。

如果你能帮助我,我将不胜感激。

提前致谢!

1 个答案:

答案 0 :(得分:3)

<强> MATLAB

filename = input('File name: ','s');
fileID = fopen(filename)
fprintf(fileID, 'That wasn''t so hard')
fclose(fileID)
  • input - 请求用户输入
  • fopen - 打开文件,或获取有关打开文件的信息
  • fprintf - 将数据写入文本文件

<强>的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