how to sreen out numbers from line?

.....
I want to select specified lines from a txt file.which the first two characters is digital.for example:
......
Standard digital inputs status,
4064 DO 3210 N/A R 0 0 1 DP V-F-S-B
Standard digital outputs status, from 0 to 3
4057 DIX BA9876543210 N/A R 0 0 1 DP V-F-S-B
Expanded digital inputs status, from 0 to 11;
3100 DC link voltage [V] R 0.00 0.00 0.00 PV V-F-S-B
Drive DC link voltage
3110 Magnetizing curr [A] R 0.00 0.00 0.00 PV V-F-S-B
......

I want to keep lines:
4064 DO 3210 N/A R 0 0 1 DP V-F-S-B
4057 DIX BA9876543210 N/A R 0 0 1 DP V-F-S-B
3100 DC link voltage [V] R 0.00 0.00 0.00 PV V-F-S-B
3110 Magnetizing curr [A] R 0.00 0.00 0.00 PV V-F-S-B

how to do ?
my code is below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
main()
{     
      ifstream ifs("data.txt");
      ofstream ofs("data1.txt");
      string strTmp,s;
 	while(getline(ifs,strTmp))
	{  
	if(isalnum(strTmp[0])&&isalnum(strTmp[1]))
         { s+="\n";  
           s+=strTmp; }
        else continue;
    }		     
ofs<<s;
ifs.close();
ofs.close();
system("pause");

but it doesn't work.
can you tell why?
Last edited on
isalnum() Checks if parameter c is either a decimal digit or an uppercase or lowercase letter.
ref: http://www.cplusplus.com/reference/clibrary/cctype/isalnum/

strTmp[0] is the first character in strTmp, which from your sample is an alphanumeric character,
ditto with strTmp[1] etc.
but if I instead isalnum() of isdigit(), problem is still there.
but if I instead isalnum() of isdigit(), problem is still there.

and the problem is?

here's what I tried:

//input data.txt
Standard digital inputs status,
4064 DO 3210 N/A R 0 0 1 DP V-F-S-B
Standard digital outputs status, from 0 to 3
4057 DIX BA9876543210 N/A R 0 0 1 DP V-F-S-B
Expanded digital inputs status, from 0 to 11;
3100 DC link voltage [V] R 0.00 0.00 0.00 PV V-F-S-B
Drive DC link voltage
3110 Magnetizing curr [A] R 0.00 0.00 0.00 PV V-F-S-B


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
    ifstream ifs("data.txt");
    ofstream ofs("data1.txt");
    string strTmp,s;
    while(getline(ifs,strTmp))
   {
         //if(isalnum(strTmp[0])&&isalnum(strTmp[1]))
         if(isdigit(strTmp[0]))
         {
               s+="\n";
               s+=strTmp;
         }
         else continue;
    }
    ofs<<s;
    ifs.close();
    ofs.close();
    return 0;
}



//data1.txt

4064 DO 3210 N/A R 0 0 1 DP V-F-S-B
4057 DIX BA9876543210 N/A R 0 0 1 DP V-F-S-B
3100 DC link voltage [V] R 0.00 0.00 0.00 PV V-F-S-B
3110 Magnetizing curr [A] R 0.00 0.00 0.00 PV V-F-S-B


ref: http://www.cplusplus.com/reference/clibrary/cctype/isdigit/
edit: added "#include <string>"
Last edited on
the still problem is data1.txt is empty.
Last edited on
was the code you tried the same as mine? I tested it in both GCC and MS's C/++ compilers and teh result is the same :/
matsom, when you include fstream the string file is included along the way, no need to include both. Doesn't harm anything, but, just letting you know.
@WriteGreatCode, yeah, it worked on GCC but not MS C/++, it seems their <fstream> does not have #include <string> , so I guess its better to include it in case OP uses MS VC++ 2010, but like you said, it doesn't do any harm :)
matsom,I copied your code,but the data1.txt is empty.I use dev c++ platform.I tried VC ++,still empty.It seems easy.but ....
If you're willing to add a '#' statement at the start of the lines that you don't want then this will work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
int main ()
{
	using namespace std;
	string saInput [10] = "";
	ifstream iFile ("Data.txt");
	for (int i = 0; getline (iFile, saInput [i]); ++i) continue; //Read each line of the file
	iFile.close ();
	ofstream oFile ("Data1.txt");
	for (int i = 0; saInput [i] != "\0"; ++i)
	{
		if (saInput [i] [0] == '#') continue; //Is it a "comment" line?
		oFile << saInput [i] << endl;
	}
	oFile.close ();
	return 0;
}
My example (which was a modification of yours ;) does not handle input file failure, are you sure that data.txt is in the same folder?


edit: oh, and try not to use DevCpp/GCC 3.x.x anymore :)
Last edited on
one friend give me the code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#define MAXLEN 1000
FILE *f;
char ln[MAXLEN];
int main() {
    f=fopen("data.txt","r");
    if (NULL==f) {
        printf("Can not open file data.txt!\n");
        return 0;
    }
    while (1) {
        if (NULL==fgets(ln,MAXLEN,f)) break;
        if ('0'<=ln[0] && ln[0]<='9' && '0'<=ln[1] && ln[1]<='9') printf("%s",ln);
    }
    fclose(f);
}

but the data.txt doesn't change at all.

can any body modify to fulfill the problem,better to create a data1.txt?
Last edited on
good evening everyone
Topic archived. No new replies allowed.