Command line argument - File manipulation

M

magix

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.
 
S

SirMike

Dnia Thu, 28 Sep 2006 22:27:52 +0800, magix napisa³(a):
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

?
 
J

Jim Langston

magix said:
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.
 
B

Brian C

SirMike said:
> 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top