M
MM
Hi there,
How can I change my code (below) so that I use an "input argument" to
specify the file name of the input file? For example, if I compile the code
and that the application then gets the name "splitdata", then I want to be
able to call my application with something like this (datafile.txt will then
be the name of the input file):
splitdata datafile.txt
In the code as I have it now, the name of the input file is specified in
line 13:
char tname[] = "Example.txt";
So, I want to skip this "hard coded" name specification. (The length of the
input file name is not known.)
Thanks in advance,
MM
=====================================================
=== Code, including line numbers (without line numbers is below this one)
===
=====================================================
1: #include <stdio.h>
2: #include <stdlib.h>
3: #include <string.h>
4:
5: #define DATASTART "\\Data:"
6: #define BLOCKSTART "Time"
7:
8: int main()
9: {
10: FILE *fh, *fp, *fq;
11: char hname[] = "header.dat"; // name of header output file
12: char fname[6+2+4+1]; // name of data block output files,
dblockNN.dat
13: char tname[] = "Example.txt"; // name of input file to split
14: char buf[1001]; // max line length is 1000 characters
15: char numdb[] = "NumDataBlocks=";
16: int i = 0;
17:
18: // open input file for reading
19: if((fq=fopen(tname, "r")) == 0) {
20: perror(tname);
21: exit(EXIT_FAILURE);
22: }
23:
24: // open header output file
25: if((fh=fopen(hname, "w")) == 0) {
26: perror(fname);
27: exit(EXIT_FAILURE);
28: }
29:
30: // print data to header file
31: // if start of data segment is found then close header file
32: while(fgets(buf, sizeof buf, fq) != 0) {
33: if(strncmp(DATASTART, buf, 6) == 0) {
34: fclose(fh);
35: break;
36: }
37: fputs(buf, fh);
38: }
39:
40: // write data block output files
41: while(fgets(buf, sizeof buf, fq) != 0) {
42: // lines starting with '#' and blank lines are skipped
43: if(buf[0] == '#' || buf[0] == '\n')
44: continue;
45:
46: // write each block to a separate file
47: if(strncmp(BLOCKSTART, buf, 4) == 0) {
48: if(i > 0)
49: fclose(fp);
50: sprintf(fname, "dblock%02d.dat", ++i);
51: if((fp=fopen(fname, "w")) == 0) {
52: perror(fname);
53: exit(EXIT_FAILURE);
54: }
55: }
56: fputs(buf, fp);
57: }
58:
59: // close files
60: fclose(fp);
61: fclose(fq);
62:
63: // open header output file again and print the number of data blocks
found last in the file
64: if((fh=fopen(hname, "a")) == 0) {
65: perror(fname);
66: exit(EXIT_FAILURE);
67: }
68: sprintf(numdb, "%s%d%s", numdb, i, "\n");
69: fputs(numdb, fh);
70: fclose(fh);
71:
72: return 0;
73: }
=========================
=== Code without line numbers ===
=========================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DATASTART "\\Data:"
#define BLOCKSTART "Time"
int main()
{
FILE *fh, *fp, *fq;
char hname[] = "header.dat"; // name of header output file
char fname[6+2+4+1]; // name of data block output files,
dblockNN.dat
char tname[] = "Example.txt"; // name of input file to split
char buf[1001]; // max line length is 1000 characters
char numdb[] = "NumDataBlocks=";
int i = 0;
// open input file for reading
if((fq=fopen(tname, "r")) == 0) {
perror(tname);
exit(EXIT_FAILURE);
}
// open header output file
if((fh=fopen(hname, "w")) == 0) {
perror(fname);
exit(EXIT_FAILURE);
}
// print data to header file
// if start of data segment is found then close header file
while(fgets(buf, sizeof buf, fq) != 0) {
if(strncmp(DATASTART, buf, 6) == 0) {
fclose(fh);
break;
}
fputs(buf, fh);
}
// write data block output files
while(fgets(buf, sizeof buf, fq) != 0) {
// lines starting with '#' and blank lines are skipped
if(buf[0] == '#' || buf[0] == '\n')
continue;
// write each block to a separate file
if(strncmp(BLOCKSTART, buf, 4) == 0) {
if(i > 0)
fclose(fp);
sprintf(fname, "dblock%02d.dat", ++i);
if((fp=fopen(fname, "w")) == 0) {
perror(fname);
exit(EXIT_FAILURE);
}
}
fputs(buf, fp);
}
// close files
fclose(fp);
fclose(fq);
// open header output file again and print the number of data blocks
found last in the file
if((fh=fopen(hname, "a")) == 0) {
perror(fname);
exit(EXIT_FAILURE);
}
sprintf(numdb, "%s%d%s", numdb, i, "\n");
fputs(numdb, fh);
fclose(fh);
return 0;
}
How can I change my code (below) so that I use an "input argument" to
specify the file name of the input file? For example, if I compile the code
and that the application then gets the name "splitdata", then I want to be
able to call my application with something like this (datafile.txt will then
be the name of the input file):
splitdata datafile.txt
In the code as I have it now, the name of the input file is specified in
line 13:
char tname[] = "Example.txt";
So, I want to skip this "hard coded" name specification. (The length of the
input file name is not known.)
Thanks in advance,
MM
=====================================================
=== Code, including line numbers (without line numbers is below this one)
===
=====================================================
1: #include <stdio.h>
2: #include <stdlib.h>
3: #include <string.h>
4:
5: #define DATASTART "\\Data:"
6: #define BLOCKSTART "Time"
7:
8: int main()
9: {
10: FILE *fh, *fp, *fq;
11: char hname[] = "header.dat"; // name of header output file
12: char fname[6+2+4+1]; // name of data block output files,
dblockNN.dat
13: char tname[] = "Example.txt"; // name of input file to split
14: char buf[1001]; // max line length is 1000 characters
15: char numdb[] = "NumDataBlocks=";
16: int i = 0;
17:
18: // open input file for reading
19: if((fq=fopen(tname, "r")) == 0) {
20: perror(tname);
21: exit(EXIT_FAILURE);
22: }
23:
24: // open header output file
25: if((fh=fopen(hname, "w")) == 0) {
26: perror(fname);
27: exit(EXIT_FAILURE);
28: }
29:
30: // print data to header file
31: // if start of data segment is found then close header file
32: while(fgets(buf, sizeof buf, fq) != 0) {
33: if(strncmp(DATASTART, buf, 6) == 0) {
34: fclose(fh);
35: break;
36: }
37: fputs(buf, fh);
38: }
39:
40: // write data block output files
41: while(fgets(buf, sizeof buf, fq) != 0) {
42: // lines starting with '#' and blank lines are skipped
43: if(buf[0] == '#' || buf[0] == '\n')
44: continue;
45:
46: // write each block to a separate file
47: if(strncmp(BLOCKSTART, buf, 4) == 0) {
48: if(i > 0)
49: fclose(fp);
50: sprintf(fname, "dblock%02d.dat", ++i);
51: if((fp=fopen(fname, "w")) == 0) {
52: perror(fname);
53: exit(EXIT_FAILURE);
54: }
55: }
56: fputs(buf, fp);
57: }
58:
59: // close files
60: fclose(fp);
61: fclose(fq);
62:
63: // open header output file again and print the number of data blocks
found last in the file
64: if((fh=fopen(hname, "a")) == 0) {
65: perror(fname);
66: exit(EXIT_FAILURE);
67: }
68: sprintf(numdb, "%s%d%s", numdb, i, "\n");
69: fputs(numdb, fh);
70: fclose(fh);
71:
72: return 0;
73: }
=========================
=== Code without line numbers ===
=========================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DATASTART "\\Data:"
#define BLOCKSTART "Time"
int main()
{
FILE *fh, *fp, *fq;
char hname[] = "header.dat"; // name of header output file
char fname[6+2+4+1]; // name of data block output files,
dblockNN.dat
char tname[] = "Example.txt"; // name of input file to split
char buf[1001]; // max line length is 1000 characters
char numdb[] = "NumDataBlocks=";
int i = 0;
// open input file for reading
if((fq=fopen(tname, "r")) == 0) {
perror(tname);
exit(EXIT_FAILURE);
}
// open header output file
if((fh=fopen(hname, "w")) == 0) {
perror(fname);
exit(EXIT_FAILURE);
}
// print data to header file
// if start of data segment is found then close header file
while(fgets(buf, sizeof buf, fq) != 0) {
if(strncmp(DATASTART, buf, 6) == 0) {
fclose(fh);
break;
}
fputs(buf, fh);
}
// write data block output files
while(fgets(buf, sizeof buf, fq) != 0) {
// lines starting with '#' and blank lines are skipped
if(buf[0] == '#' || buf[0] == '\n')
continue;
// write each block to a separate file
if(strncmp(BLOCKSTART, buf, 4) == 0) {
if(i > 0)
fclose(fp);
sprintf(fname, "dblock%02d.dat", ++i);
if((fp=fopen(fname, "w")) == 0) {
perror(fname);
exit(EXIT_FAILURE);
}
}
fputs(buf, fp);
}
// close files
fclose(fp);
fclose(fq);
// open header output file again and print the number of data blocks
found last in the file
if((fh=fopen(hname, "a")) == 0) {
perror(fname);
exit(EXIT_FAILURE);
}
sprintf(numdb, "%s%d%s", numdb, i, "\n");
fputs(numdb, fh);
fclose(fh);
return 0;
}