简体   繁体   English

如何创建77个文件,其内容是每个文件的名称?

[英]How can I create 77 files the content of which is the name of each file?

Let's take an example. 让我们举个例子。 A "file1" -file has a content "file1", its own name. “ file1”文件具有自己的名称“ file1”。 A "file2"-file has a content "file2", again its own name. “ file2”文件具有内容“ file2”,同样是其自己的名称。 The pattern continues until we have covered 77 files. 模式一直持续到我们覆盖了77个文件为止。 What is the easiest way to do the 77 files? 做77个文件的最简单方法是什么?

Since I tend to have hard time in compiling, I sum up some details. 由于我往往很难编译,因此我总结了一些细节。

Intructions how to compile the codes 指示如何编译代码

PERMISSIONS: "chmod 700 filename.some_ending" 权限:“ chmod 700 filename.some_ending”

RUNNING: ". ./filename.some_ending" RUNNING:“。./filename.some_ending”

How to compile? 怎么编译?

  1. Use gcc for C++/C like "gcc filename.c", and then run ". ./a.out" 将gcc用于C ++ / C,例如“ gcc filename.c”,然后运行“ ../a.out”
  2. Use javac for Java like "javac filename.javac", and then run it with "java class" (error?!) 对Java使用Javac(例如“ javac filename.javac”),然后使用“ java class”运行它(错误?!)
  3. Fortran? Fortran?
  4. ... more? ... 更多?
#!/bin/bash
for i in {1..77}
do
   echo file$i > file$i
done

蟒蛇:

for i in range(1,78): open("file" + str(i), "w").write("file" + str(i))

C++: C ++:

#include <sstream>
#include <fstream>

using namespace std;

int main()
{
     for (int i = 1; i <= 77; i++) {
          stringstream s;
          s << "file" << i;
          ofstream out(s.str().c_str());
          out << s.str();
          out.close();
     }
     return 0;
}

C: C:

#include <stdio.h>

int main() {
    char buffer[8];
    FILE* f;
    int i;
    for(i = 1; i <= 77; i++){
        sprintf(buffer, "file%d", i);
        if((f = fopen(buffer, "w")) != NULL){
            fputs(buffer, f);
            fclose(f);
        }
    }
    return 0;
}

Java version, for fun. Java版本,很有趣。

import java.io.*;

public class HA {
    public static void main(String[] args) throws Exception {
        String file = "file";
        for (int i = 1; i <= 77; i++){
            PrintStream out = new PrintStream(new File(file + i));
            out.print(file + i);
            out.close();
        }
    }
}

Perl: Perl:

#!/usr/bin/env perl
for ( my $i = 1; $i <= 77; ++$i )
{
    open( my $fh, '>', 'file' . $i );
    print { $fh } ( 'file' . $i );
    close( $fh );
}

I added the extension, assuming the file's gonna have it. 我添加了扩展名,并假设该文件将具有该扩展名。

Fortran: Fortran:

character(11) file_name
do i=1,77
  write(file_name,"('file',i2.2,'.txt')")i
  open(unit=1, file=file_name, status='replace')
  write(1,"(a)")file_name
  close(unit=1)
enddo
end

Python version for those who prefer readable over terse: 对于那些喜欢简洁而不是简洁的人的Python版本:

filenames = ("file%(num)d" % vars() for num in range(1, 78))
for filename in filenames:
    open(filename, 'w').write(filename)

C#, why not: C#,为什么不呢?

using System.IO;

namespace FileCreator
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 77; i++)
            {
                TextWriter f = new StreamWriter("file" + i);
                f.Write("file" + i);
                f.Close();
            }
        }
    }
}

红宝石:

77.times { |i| File.open("file#{i+1}", "w") { |f| f.puts "file#{i+1}" } }

Delphi/Free Pascal 德尔福/帕斯卡

program Create77Files;

{$APPTYPE CONSOLE}

uses
  Classes, SysUtils;

var
  I: Integer;
  S: string;
begin
  for I := 1 to 77 do
  begin
    S:= 'file' + IntToStr(I);
    with TStringStream.Create(S) do
    begin
      SaveToFile(S);
      Free;
    end;
  end;
end.

Groovy:

77.times{n->n++;new File('file'+n).withWriter{it.writeLine('file'+n)}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在每个文件夹中创建一个文件? - How can I create a file in each folder? 如何创建包含内容(文件/目录)的ext3文件系统并将其打包为二进制文件? - How can I create a ext3 filesystem with content (files/dirs) and pack them in a binary file? 如何根据目录名更改多个文件的内容? - How can I change content of several files according to their directory name? 如何下载前一天名称的文件 - How can I download a file which has a name of the previous day 如何使用文件名日期范围 tar gz 文件? - How can I tar gz files with file name date range? 我如何创建一个名称中包含输入的文件 - How can i create a file that contains an enter in its name 如何将一批文件重命名为每个文件第一行中存在的名称 - How to rename a batch of files to a name that exists in the first line of each file 如何打印在 for 循环中评估的每个文件的名称(linux) - How can I print the name of each file that is being assessed in a for loop (linux) 如何根据每个文件上运行管道的结果告诉find打印文件列表? - How can I tell find to print a list of files based on the results of running a pipeline on each file? 如何编写一个遍历文件夹并使用给定参数创建新文件/文件内容的脚本 - How can I write a script that iterates through a folder and creates new files/file content with given parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM