tags:

views:

277

answers:

11

Let's take an example. A "file1" -file has a content "file1", its own name. A "file2"-file has a content "file2", again its own name. The pattern continues until we have covered 77 files. What is the easiest way to do the 77 files?

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"

RUNNING: ". ./filename.some_ending"

How to compile?

  1. Use gcc for C++/C like "gcc filename.c", and then run ". ./a.out"
  2. Use javac for Java like "javac filename.javac", and then run it with "java class" (error?!)
  3. Fortran?
  4. ... more?
+18  A: 
#!/bin/bash
for i in {1..77}
do
   echo file$i > file$i
done
Dave
You beat me by 25 seconds T.T
GMan
The {1..77} construct doesn't work for me with GNU bash, version 2.05b.0(1)-release (powerpc-apple-darwin8.0). "for i in `seq 1 77`" should work more universally.
Adam Rosenfield
+2  A: 

Java version, for fun.

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();
        }
    }
}
jjnguy
+2  A: 

Perl:

#!/usr/bin/env perl
for ( my $i = 1; $i <= 77; ++$i )
{
    open( my $fh, '>', 'file' . $i );
    print { $fh } ( 'file' . $i );
    close( $fh );
}
eternaleye
There's rarely need to use C-style loops in Perl. "for my $i (1..77)" is equivalent, plus you don't have to worry about fencepost errors.
Dave Sherohman
+5  A: 

Python:

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

I added the extension, assuming the file's gonna have it.

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
ldigas
+2  A: 

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;
}
Ankit
You don't need to call out.close()
GMan
@GMan: aren't you supposed, to close the file pointer?
Ankit
+1  A: 

Python version for those who prefer readable over terse:

filenames = ("file%(num)d" % vars() for num in range(1, 78))
for filename in filenames:
    open(filename, 'w').write(filename)
bignose
Personally, I think the other python version is more readable.
Ankit
+2  A: 

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;
}
Gautier Hayoun
A: 

C#, why not:

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();
            }
        }
    }
}
julianz
A: 

Ruby:

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

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.
Cesar Romero