-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathycut.cpp
73 lines (68 loc) · 1.2 KB
/
ycut.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
void usage()
{
cerr << "Usage: ycut <num> <instream>\n";
}
int main(int argc, char *argv[])
{
int aimField = 0;
if( argc >= 2 )
{
string arg1(argv[1]);
stringstream ss(arg1);
ss >> aimField;
}
ifstream ifs;
istream *pis = NULL;
if( argc == 3 )
{
ifs.open(argv[2]);
pis = &ifs;
}
else if( argc == 2 )
{
pis = &cin;
}
else if( argc < 2 )
{
usage();
return 1;
}
if( argc > 3 )
{
cerr << "Too Many Arguments.\n";
usage();
return 1;
}
if( !pis->good() )
{
cerr << "Can not Open File.\n";
return 1;
}
if( aimField < 1 )
{
cerr << "Invalid Field Number.\n";
return 1;
}
string line;
while( getline(*pis, line) )
{
stringstream ss(line);
string str;
int cnt = 0;
while( ss >> str )
{
++cnt;
if(cnt == aimField)
{
cout << str << "\n";
break;
}
}
}
return 0;
}