ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/freemol/trunk/smi23d/src/mengine/split.c
Revision: 3
Committed: Mon Jun 9 21:38:26 2008 UTC (12 years, 5 months ago) by tjod
File size: 2108 byte(s)
Log Message:
test

Line User Rev File contents
1 tjod 3 #include <stdio.h>
2     #include <stdlib.h>
3     #include <string.h>
4    
5     #define TRUE 1
6     #define FALSE 0
7    
8     int FetchRecord(FILE *, char *);
9    
10     int main(int argc, char *argv[])
11     {
12     int i,ncount,fcount;
13     int MAXLINE = 15625;
14     char filename[80],line[2000],outfile[80];
15     char astring[2000],pubID[15];
16     char tmpname[80],tag[5];
17     FILE *infile,*wfile;
18    
19     // check command line for filename
20     if (argc > 1)
21     strcpy(filename,argv[1]);
22     else
23     {
24     printf("Enter filename:");
25     gets(filename);
26     }
27     // build outfile name
28     for (i=0; i < strlen(filename); i++)
29     {
30     if (filename[i] != '.' && filename[i] != '\0')
31     tmpname[i] = filename[i];
32     else
33     break;
34     }
35     tmpname[i] = '\0';
36     //
37     infile = fopen(filename,"r");
38     if (infile == NULL)
39     {
40     printf("Error opening file\n");
41     exit(0);
42     }
43     fcount = -1;
44     strcpy(outfile,tmpname);
45     strcat(outfile,".new");
46     wfile = fopen(outfile,"w");
47     while (FetchRecord(infile,line))
48     {
49     fprintf(wfile,"%s\n",line);
50     }
51     fclose(infile);
52     if (wfile != NULL)
53     fclose(wfile);
54     return TRUE;
55     }
56     /* ============================================= */
57     int FetchRecord(FILE *fp, char *buffer)
58     {
59     int ch;
60     char *ptr;
61    
62     ptr = buffer;
63     do
64     {
65     ch = getc(fp);
66     if (ch >= ' ')
67     *ptr++ = ch;
68     else if (ch == '\n')
69     {
70     *ptr = 0;
71     return TRUE;
72     } else if (ch == '\r')
73     {
74     ch = getc(fp);
75     if (ch != '\n')
76     ungetc(ch,fp);
77     *ptr = 0;
78     return TRUE;
79     } else if (ch == EOF)
80     {
81     *ptr = 0;
82     return (ptr != buffer);
83     } else
84     *ptr++ = ch;
85     } while (ptr < buffer+1999);
86    
87     do
88     {
89     ch = getc(fp);
90     } while (ch !='\n' && ch != '\r' && ch != EOF);
91     if (ch == '\r')
92     {
93     ch = getc(fp);
94     if (ch != '\n')
95     ungetc(ch,fp);
96     }
97     *ptr = 0;
98     return TRUE;
99     }