Learn C programming in 11 days

Day 9 File

Simple file

 

/*********************************************************/

/*  file1.cpp - VC6.0                                    */

/*  Simple file input and output                         */

/*  J Luo, 19-May-2001                                   */

/*********************************************************/

 

#include<stdio.h>

#include<process.h>

 

void main()

{

     FILE *fin, *fout;

     char *inputFile, *outputFile;

     char temp[80];

     char ch;

 

     printf("Input file: ");

     scanf("%s", temp);

     inputFile = temp;

 

     fin=fopen(inputFile, "r" );

     if ( fin == NULL ) {

         printf("Can't open file: %s\n", inputFile);

         exit(0);

     }

 

     printf("Output file: ");

     scanf("%s", temp);

     outputFile = temp;

     fout=fopen(outputFile, "w" );

 

     if ( fout == NULL ) {

         printf("Can't open file: %s\n", outputFile);

         exit(0);

     }

 

     while ( 1) {

         ch=fgetc(fin);

         if ( feof(fin) ) break;

         putchar(ch);

         fputc(ch,fout);

     }

     fclose(fin);

     fclose(fout);

}

 

Command line arguments

 

/*********************************************************/

/*  file2.cpp - VC6.0                                    */

/*  Command line arguments                               */

/*  J Luo, 19-May-2001                                   */

/*********************************************************/

 

#include<stdio.h>

#include<process.h>

 

void main(int argc, char *argv[])

{

     FILE *fin, *fout;

     char ch;

 

     if ( argc<3 ) {

         printf("Usage: file1 inputfile outputfile\n");

         exit(0);

     }

     fin=fopen(argv[1], "r" );

     if ( fin == NULL ) {

         printf("Can't open file: %s\n", argv[1]);

         exit(0);

     }

 

     fout=fopen(argv[2], "w" );

     if ( fout == NULL ) {

         printf("Can't open file: %s\n", argv[2]);

         exit(0);

     }

 

     while ( !feof(fin) ) {

         ch=fgetc(fin);

         putchar(ch);

         fputc(ch,fout);

     }

 

     fclose(fin);

     fclose(fout);

}

 

Change EMBL format to GenBank format

 

/*********************************************************/

/*  gembl.cpp - VC6.0                                    */

/*  EMBL to GenBank                                      */

/*  J Luo, 19-May-2001                                   */

/*********************************************************/

 

#include<stdio.h>

#include<process.h>

#include<string.h>

 

void EMBL2GenBank(FILE *, FILE *);

void change(int, char *);

void print(int, char *, char *);

 

void main(int argc, char *argv[])

{

     FILE *fin, *fout;

 

     if ( argc<3 ) {

         printf("Usage: file1 inputfile outputfile\n");

         exit(0);

     }

     fin=fopen(argv[1], "r" );

     if ( fin == NULL ) {

         printf("Can't open file: %s\n", argv[1]);

         exit(0);

     }

 

     fout=fopen(argv[2], "w" );

     if ( fout == NULL ) {

         printf("Can't open file: %s\n", argv[2]);

         exit(0);

     }

 

     EMBL2GenBank(fin, fout);

 

     fclose(fin);

     fclose(fout);

}

 

void EMBL2GenBank(FILE *fin, FILE *fout)

{

     char ch;

     char line[80];

     int i, k;

 

     while ( 1 ) {

         i = 0;

         while ( !feof(fin) ) {

              ch = fgetc(fin);

              if ( ch == 10 ) {

                   line[i] = '\0';

                   break;

              }

              line[i] = ch;

              i++;

         }

         k = i;

         if ( feof(fin) ) break;

         change(k, line);

     }

}

 

void change(int k, char * line)

{

     if         ( line[0]=='I' && line[1]=='D' ) {

         print(k, "LOCUS", line);

     } else if ( line[0]=='A' && line[1]=='C' ) {

         print(k, "ACCSSESSION", line);

     } else if ( line[0]=='K' && line[1]=='W' ) {

         print(k, "KEYWORD", line);

     } else if ( line[0]=='D' && line[1]=='T' ) {

         print(k, "DATE", line);

     } else if ( line[0]=='D' && line[1]=='E' ) {

         print(k, "DESCRIPTION", line);

     } else if ( line[0]=='O' && line[1]=='S' ) {

         print(k, "ORGANISM", line);

     } else if ( line[0]=='S' && line[1]=='Q' ) {

         print(k, "SEQUENCE", line);

     } else if ( line[0]==' ' && line[1]==' ' ) {

         print(k, "  ", line);

     } else {

         ;

     }

}

 

void print(int k, char *s, char * line)

{

     int i;

     printf("%-12s", s);

     for (i=5; i<k; i++) printf("%c", line[i]);

     printf("\n");

}