473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Command line argument - File manipulation

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
4 3304
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Joe | last post by:
I'm using Python 2.4 on Windows XP SP2. I'm trying to receive a command line argument that is a newline (\n) Here is the command line to use sample.py "\n" Here is a sample.py script
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
2
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
3
by: Shailesh Humbad | last post by:
I want to set a command-line argument for a Windows service that I programmed in VB.Net. This ought to be available from the service installer classes, but I can not find any such property. What...
34
by: Roman Mashak | last post by:
Hello, All! I'm implementing simple CLI (flat model, no tree-style menu etc.). Command line looks like this: <command> <param1> <param2> ... <paramN> (where N=1..4) And idea is pretty simple: ...
8
by: Andrew Robert | last post by:
Hi Everyone. I tried the following to get input into optionparser from either a file or command line. The code below detects the passed file argument and prints the file contents but the...
3
by: jlw16 | last post by:
Hello, I’m trying to use my vbs script to get a command line argument for a file which will need to be opened through QuickTestPro. Below are the commands I’m using: Dim qt_file 'As String ->...
6
by: =?Utf-8?B?Rm9ycmVzdCBIZWxsZXI=?= | last post by:
The only code that is helpful is this: fp = New IO.StreamReader(filename, New System.Text.UnicodeEncoding(True, False), False) If I pass a command line argument to my program that is not from...
51
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.