Stripping leading 0s off a text file.

Hi
Im a delphi programmer not C++ hense I need some help if possible. I have a text file called Input.txt which is laid out as so

<Char 14>,<Char 4>

ie 01234567899999,1999

I need to cycle through this file and spit out  a new file called outfile.txt with all the information minus the leading zeros.

ie if Input.txt was as follows

01111111111111,9999
00000333333333,0099

The output file would be

1111111111111,9999
333333333,0099

I am using Borlands C++ 5.02 and running this program in DOS.

Thanks for any help..
Stu
StuartB_99Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jkrConnect With a Mentor Commented:
#include <stdio.h>

#define BUF_MAX 256 // use a reasonable value here...
chat acBuf [ BUF_MAX];
char* pc;
FILE* pIn;
FILE* pOut;

pIn = fopen ( "in.txt", "r");
pOut = fopen ( "out.txt", "w");

while ( fgets ( acBuf, BUF_MAX, pIn))
{
 pc = acBuf;

 while ( *pc == '0') pc++;

 fputs ( pc, pOut);
}

Error checking omitted for brevity ;-)

Feel free to ask if you encounter problems!
0
 
jkrCommented:
Opps, I forgot 2 lines that should be at the end of the above code:

fclose ( pIn);
fclose ( pOut);

Sorry...
0
 
StuartB_99Author Commented:
Thanx jkr,
Im getting a declaration syntax error at the

chat acBuf [ BUF_MAX];

line


any ideas,

Stuart
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
StuartB_99Author Commented:
Thanx jkr,
Im getting a declaration syntax error at the

chat acBuf [ BUF_MAX];

line


any ideas,

Stuart
0
 
jkrCommented:
Sorry, typo:

chat acBuf [ BUF_MAX];

should of course read

char acBuf [ BUF_MAX];
0
 
StuartB_99Author Commented:
Excellent thanks,
However Im now getting

cannot convert 'char_ss_*' to 'char'

at

>>    pc = acBuf;

sorry to be a neusense, but I havent got a clue with C++ just Delphi..

Thanks

Stu
0
 
StuartB_99Author Commented:
Some more cash for your efforts jkr.....!!!!!
0
 
jasonclarkeCommented:
the declaration of pc should probably be:

char pc

if you plan to do this sort of stuff a lot, you could try getting perl, it makes this sort of thing much easier! The following is a complete perl program to achieve the same thing:

while (<>)
{
   s/^0+(.*)$/$1/;
   print "$_";
}
0
 
jasonclarkeCommented:
Ignore what I said about char pc, it is incorrect.  (Teach me to read the code better!).

The code looks alright, make sure you have:

    char *pc;

and NOT just:

    char pc;
0
 
StuartB_99Author Commented:
Thanks very much,

Job done..

You have saved the world (OK slight exageration maybe!!)
I realy appreciate it, is Pearl obtainable as a free download to try it out do you know??
0
 
jasonclarkeCommented:
perl is available for free from:
 
http://www.activestate.com/ActivePerl/
0
All Courses

From novice to tech pro — start learning today.