473,804 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_op en() )
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 3334
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.com wrote 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_op en() )
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
3459
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
2939
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 have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command line arguments to the program, the behaviour of one of the functions changes mysteriously. I have...
2
4205
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 how to use Command-Line Arguments inside C program.
3
5419
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 I end up having to do is modify the registry directly from a separate installer program, which from a deployment perspective is--let's just say less than optimal. The second problem I have is that when the Win2K service control manager runs...
34
6882
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: 1) get whole string of input line 2) preset table of strings matching <command> 3) preset table of function calls
8
3521
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 individual swithces do not get passed to option parser.
3
10329
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 -> If I don’t comment out the As String, I get an “Expected end of statement” error – is this correct?? qt_file = Command -> This doesn’t appear to be correct – when I echo out qt_file it’s null qtApp.Test.DataTable.Import qt_file ' Import data...
6
2364
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 Visual Studio.NET is debug mode, the above code won't work, even though filename is not changed whether I pass a command line argument or not. By not work, I mean it will set fp.EndOfStream to true and fp.Peek() returns -1. The command line argument...
51
4168
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 Command Prompt or Browser or some other application? Ojas.
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9152
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.