-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadLineFromNtoM.cpp
More file actions
67 lines (60 loc) · 1.6 KB
/
Copy pathreadLineFromNtoM.cpp
File metadata and controls
67 lines (60 loc) · 1.6 KB
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
#include <iostream>
#include <stdlib.h>
#include <assert.h>
#include <fstream>
#include <cstring>
using namespace std;
bool getLineFromNtoM(char *newFileName, char *sourceFileName, int startLine, int endLine)
{
assert(NULL != newFileName && NULL != sourceFileName && startLine > 0 && endLine >= startLine);
ifstream fdSource;
fdSource.open(sourceFileName,ios::in);
if(!fdSource.is_open())
{
printf("open file %s failed.\n",sourceFileName);
}
ofstream fdNew;
fdNew.open(newFileName,ios::out | ios::trunc);
if(!fdNew.is_open())
{
printf("open file %s failed.\n",newFileName);
}
int startLinei = 1;
string str;
while(startLinei < startLine && !fdSource.eof())
{
getline(fdSource, str);
startLinei++;
if(startLinei == startLine)
break;
}
if (startLinei == startLine)
{
while(getline(fdSource, str))
{
fdNew << str << endl;
startLinei++;
if(startLinei == endLine +1)
break;
}
}
fdSource.close();
fdNew.close();
}
int main(int argc, char *argv[])
{
if (argc < 5)
{
printf("input parameters number are not enough.\n");
printf("for example: ./xxx newFileName sourceFileName startLine endLine\n");
exit(1);
}
char newFileName[50];
strcpy(newFileName,argv[1]);
char sourceFileName[50];
strcpy(sourceFileName,argv[2]);
int startLine = atoi(argv[3]);
int endLine = atoi(argv[4]);
getLineFromNtoM(newFileName,sourceFileName,startLine,endLine);
return 0;
}