Dealing with text and args

Making the program recognize every text

Page 1 of 1

2 Replies - 1030 Views - Last Post: 26 December 2007 - 04:45 AM Rate Topic: -----

#1 inf_f   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-December 07

Dealing with text and args

Post icon  Posted 23 December 2007 - 02:38 AM

Hi everyone :)
I am having this question almost for long time , and so when i went to learn c# (which was before few days) i really want to use it as my primary programming language and so i want to take it into serous level (you may probably see my n00by questions asked here too much)

Ok , let's assume this
When we get a text like :
Devices founds:	
	CDROM location = /dev/scd01 , mounted at /media/cdrom
	HardDisk Location /dev/sda1 , mounted at /media/harddisk

how i can deal with that? , i want to make the program to know that text as something we could use it (array or any other things that c# support) like we have got a 2 devices found and they are CDROM and HardDisk each of them got their own properties.


if i wasn't clear in my previous example i hope this clear it a bit
Example:
passing args into the program when it start up => app.exe -o file.txt -w Text to be written at the end of the text
here i want the program to recognize the file name "file.txt" which comes after -o , and to know that everythings after -w should be written into the file.

I hope someone could help me with this or at lest giving me reference or something to read it
and thanks in advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: Dealing with text and args

#2 Nayana   User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 32
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Re: Dealing with text and args

Posted 26 December 2007 - 01:57 AM

So you're basically trying to make a program that appends text into a specified text file?
Was This Post Helpful? 0
  • +
  • -

#3 RodgerB   User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: Dealing with text and args

Posted 26 December 2007 - 04:45 AM

Maybe something like this? (Built with a C# Console application).

static void Main(string[] args)
{
	// Check that the cmd arguments aren't above or below four.
	if (args.Length != 4 || args[0] != "-o" || args[2] != "-w")
	{
		Console.WriteLine("Invalid arguments.");
		return; // Exit the program.
	}

	// Just to make it easier to follow.
	string filePath = args[1];
	string textToAppend = args[3];

	try {
		// Write the text to append to the file.
		System.IO.File.AppendAllText(filePath, textToAppend);
	}

	catch (Exception ex)
	{
		// Display error if any.
		Console.WriteLine("Couldn't write to file: Error: {0}", ex.Message);
	}			
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1