Connecting Tech Pros Worldwide Help | Site Map

Command line argument - File manipulation

  #1  
Old September 28th, 2006, 03:25 PM
magix
Guest
 
Posts: n/a
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.



  #2  
Old September 28th, 2006, 04:15 PM
SirMike
Guest
 
Posts: n/a

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
  #3  
Old September 29th, 2006, 12:25 AM
Jim Langston
Guest
 
Posts: n/a

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.


  #4  
Old September 29th, 2006, 12:35 AM
Brian C
Guest
 
Posts: n/a

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.
  #5  
Old September 29th, 2006, 08:55 AM
SirMike
Guest
 
Posts: n/a

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
Closed Thread