By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,146 Members | 1,610 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,146 IT Pros & Developers. It's quick & easy.

Command line argument - File manipulation

P: 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.

Sep 28 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

?

--
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
Sep 28 '06 #2

P: n/a
"magix" <ma***@asia.comwrote in message
news:45**********@news.tm.net.my...
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.
Sep 28 '06 #3

P: n/a
SirMike wrote:
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.
Sep 28 '06 #4

P: n/a
Dnia Thu, 28 Sep 2006 23:48:11 GMT, Brian C napisał(a):
SirMike wrote:
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
Sep 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.