news:451bdbea$1_1@news.tm.net.my...
Quote:
Dear Guru,
>
I'm not so good in file manipulation, but would like to seek for help
here.
>
I have following basic code of command line argument as starting point
(the code below output the contents of a file:)
>
#include <fstream>
#include <iostream>
>
using namespace std;
>
int main ( int argc, char *argv[] )
{
if ( argc != 2 ) // argc should be 2 for correct execution
{
cout<<"Error: wrong file arguments.\n";
cout<<"usage: "<< argv[0] <<" <filename>\n";
}
else {
// We assume argv[1] is a filename to open
ifstream the_file ( argv[1] );
// Always check to see if file opening succeeded
if ( !the_file.is_open() )
cout<<"Error: could not open file '" << argv[1] << "'\n";
else {
char x;
// the_file.get ( x ) returns false if the end of the file
// is reached or an error occurs
while ( the_file.get ( x ) )
{
cout<< x;
}
}
// the_file is closed implicitly here
}
}
>
Let say I have following entries in my text file, i.e mytext.txt
>
Name Country ID
John USA 2233&5566
John USA 445566&5566
John USA 778899&5566
>
>
My question:
1. How can I modify above code to read mytext.txt, modify something, and
then generate mytext2.txt
Modifications are:
- check from 2nd line onward (1st line is header line), in ID section,
the delimiter is "&", if there is more than 4 digits before "&", remove
the last few, and keep only 4 digits before "&" (I'm not sure if I
need to count the spacing)
>
So, if use the above, the newly mytext2.txt will have following:
Name Country ID
John USA 2233&5566
John USA 4455&5566
John USA 7788&5566
>
>
Kindly advise. Many thanks in advance for your kind assistance.
>
Regards.
I'm sorry, but this sounds extremely like homework. Show us what you've
tried.