Command line argument - File manipulation 
September 28th, 2006, 03:25 PM
| | | |
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. | 
September 28th, 2006, 04:15 PM
| | | | re: Command line argument - File manipulation
Dnia Thu, 28 Sep 2006 22:27:52 +0800, magix napisał(a): Quote:
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)
| Is it possible to have a row like this?
name country 1234 &4534
?
--
SirMike - http://www.sirmike.org
C makes it easy to shoot yourself in the foot; C++ makes it harder, but
when you do, it blows away your whole leg. - Bjarne Stroustrup | 
September 29th, 2006, 12:25 AM
| | | | re: Command line argument - File manipulation
"magix" <magix@asia.comwrote in message
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. | 
September 29th, 2006, 12:35 AM
| | | | re: Command line argument - File manipulation
SirMike wrote: Quote:
C makes it easy to shoot yourself in the foot; C++ makes it harder,
but when you do, it blows away your whole leg. - Bjarne Stroustrup
| Sorry for not being constructive, but this made me giggle. | 
September 29th, 2006, 08:55 AM
| | | | re: Command line argument - File manipulation
Dnia Thu, 28 Sep 2006 23:48:11 GMT, Brian C napisał(a): Quote:
SirMike wrote: Quote:
C makes it easy to shoot yourself in the foot; C++ makes it harder,
but when you do, it blows away your whole leg. - Bjarne Stroustrup
| >
Sorry for not being constructive, but this made me giggle.
| Do you mean it's not a truth? ;)
--
SirMike - http://www.sirmike.org
C makes it easy to shoot yourself in the foot; C++ makes it harder, but
when you do, it blows away your whole leg. - Bjarne Stroustrup |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|