Project One Coding Academy

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Project One Coding Academy

Postby Phil A » Sun Feb 21, 2010 2:12 pm

Having a great time trying my hand. My last experience was playing with Basic many years ago. Cannot understand why the commands
./ToDoList.exe hello from mono and
./ToDoList.exe add "Feed cats"
have failed. I got as far as the output
hello
HELLO
from
FROM
mono
MONO

but only after I had put 'hello from mono' into the 'todo.txt' file manually. Now stuck on inputting 'Feed cats@ etc. Any help at all appreciated.

Phil A
Phil A
 
Posts: 3
Joined: Sun Feb 21, 2010 12:56 pm

Postby Ram » Sun Feb 21, 2010 3:44 pm

Needs to see your code.

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1682
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Phil A » Sun Feb 21, 2010 4:25 pm

Hi,
Thanks for replying. This is as far as I got
using System;
using System.IO;

namespace ToDoList
{
class MainClass
{
public static void Main(string[] args)
{
var contents = File.ReadAllText("todo.txt");
foreach (var arg in args) {
Console.WriteLine(arg);
Console.WriteLine(arg.ToUpper());
}
}
}
}

If I press F8 I get the message build successful.

If I now run the programme using ./ToDoList.exe hello from mono the output is as follows
hello
HELLO
from
FROM
mono
MONO

My problem is that I had to put hello from mono into the todo.txt file manually for this to work.

Am I misunderstanding the instruction ./ToDoList.exe hello from mono which I thought would put 'hello from mono' into the todo.txt file, which it does not. Later on there is an example

./ ToDoList.exe add "feed cats
./ ToDoList.exe add "Wash socks"
./ ToDoList.exe add "Take over the earth"

I understood that this would place these arguments in the todo.txt file, it doesn't which is why I am feeling decidedly stupid and why I am unable to go further.

Phil A
Phil A
 
Posts: 3
Joined: Sun Feb 21, 2010 12:56 pm

Postby Ram » Sun Feb 21, 2010 7:39 pm

I ran your code as is by booting up the live CD.

./ToDoList.exe hello

This error this errors on execution as I had not created the todo.txt file - It needs to be in the same directory that ToDoList.exe is ran from. It only needs to exists, can be empty

It errors because the program tries to access it with this line
Code: Select all
var contents = File.ReadAllText("todo.txt");

Once I created the blank todo,txt file and re-ran the ./TodoList.exe hello

I got the correct output

hello
HELLO

Your code works as is. Which is read from file todo.txt - populated or not. Output command line arguments hello "Hello from Mono" etc.

To add stuff to the todo.txt file you need more code adding that reads the command line arguments. This follows futher on in the tutorial.

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1682
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Phil A » Sun Feb 21, 2010 10:22 pm

Hi Ram,

Grateful for the help, will see how I get on following your advice.
There appears to be lots of assistance available in respect of problems that other beginners have come across and of course there is the DVD.

Many thanks.

Phil A
Phil A
 
Posts: 3
Joined: Sun Feb 21, 2010 12:56 pm

help: trouble deleting

Postby branmask » Mon Mar 01, 2010 7:52 pm

I'm having trouble actually deleting something from the file. When I try running the del 1(or 2 or 3) command I get this error message:

Unhandled Exception: System.IO.FileNotFoundException: Could not find file "/home/.../Projects/ToDoList/ToDoList/bin/Debug/todo.txt.".
File name: '/home/.../Projects/ToDoList/ToDoList/bin/Debug/todo.txt.'
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000]
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000]
at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
at System.IO.File.OpenRead (System.String path) [0x00000]
at System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) [0x00000]
at System.IO.StreamReader..ctor (System.String path) [0x00000]
at (wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (string)
at System.IO.File.OpenText (System.String path) [0x00000]
at System.IO.File.ReadAllLines (System.String path) [0x00000]
at ToDoList1.MainClass.Main (System.String[] args) [0x00000]

This is my code:


if(args.Length==0){
Console.WriteLine("Usage:list|add<item>|del<num>");
return;
}
if (args[0]=="add"){
var contents=File.ReadAllLines("todo.txt");
var counter=1;
foreach(var item in contents){
Console.WriteLine(counter+":"+item);
counter++;
}
}else if(args[0]=="del"){
var contents=File.ReadAllLines("todo.txt.");
File.Delete("todo.txt");
var counter=1;
var deleteme=int.Parse(args[1]);
foreach(var item in contents){
if(counter!=deleteme){
File.AppendAllText("todo.txt",item +"\n");
}

counter++;
}
}else{
var contents=File.ReadAllLines("todo.txt");
var counter=1;

foreach(var item in contents){
Console.WriteLine(counter+":"+item);
counter++;
}

}
}
}
}

I haven't been able to figure out what I'm doing wrong as of yet. Any help would be most appreciated. Thanks
branmask
 
Posts: 14
Joined: Thu Feb 18, 2010 1:19 am

Postby Ram » Mon Mar 01, 2010 10:24 pm

It would appear that the todo.txt file does not exist in the same directory that the program is ran from.

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1682
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby branmask » Tue Mar 02, 2010 12:54 am

I finally figured it out! I started from scratch and rewrote it all, now it works. :-) here's what I came up with:

using System;
using System.IO;

namespace todolist
{
class MainClass
{
public static void Main(string[] args)
{
if(args.Length==0){
Console.WriteLine("Usage: list | add<item> | del<num>");
return;
}
if(args[0]=="add"){
File.AppendAllText("todo.txt",args[1]+"\n");
}else if(args[0]=="del"){
var contents=File.ReadAllLines("todo.txt");
File.Delete("todo.txt");
var counter=1;
var deleteme=int.Parse(args[1]);
foreach(var item in contents){
if(counter!=deleteme){
File.AppendAllText("todo.txt",item+"\n");
}

counter++;
}
}else{
var contents=File.ReadAllLines("todo.txt");
var counter=1;
foreach(var item in contents){
Console.WriteLine(counter+":"+item);
counter++;
}
}
}
}
}

very cool...
branmask
 
Posts: 14
Joined: Thu Feb 18, 2010 1:19 am

Postby JohnMac » Sun May 02, 2010 8:59 am

My experience with the forgoing is a bit different. I downloaded the file for project 1 and tried to run as follows (note format line limits cause new lines which are not in compiled version):-
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.IO;

namespace ToDoList
{
class MainClass
{
public static void Main(string[] args)
{
if (args.Length == 0) {
Console.WriteLine("Usage: list | add <item> | del <num>");
return;
}

if (args[0] == "add") {
File.AppendAllText("todo.txt", args[1] + "\n");
} else if (args[0] == "del") {
var contents = File.ReadAllLines("todo.txt");
File.Delete("todo.txt");

var counter = 1;
var deleteme = int.Parse(args[1]);

foreach(var item in contents) {
if (counter != deleteme) {
File.AppendAllText("todo.txt", item + "\n");
}

counter++;
}
} else {
var contents = File.ReadAllLines("todo.txt");
var counter = 1;

foreach(var item in contents) {
Console.WriteLine(counter + ": " + item);
++counter;
}
}
}
}
}


The following is a copy of the Terminal action and resultant error message.

john@john-desktop:~/Projects/ToDoList/ToDoList/bin/Debug$ ./ToDoList.exe add "addition"
john@john-desktop:~/Projects/ToDoList/ToDoList/bin/Debug$ cat todo.txt
Meh
Hello from Mono
Take over the world
Extra Extra
Debuging
Test OK
Problem
addition
john@john-desktop:~/Projects/ToDoList/ToDoList/bin/Debug$ ./ToDoList.exe del "Test OK"

Unhandled Exception: System.FormatException: Input string was not in the correct format
at System.Int32.Parse (System.String s) [0x00000]
at ToDoList.MainClass.Main (System.String[] args) [0x00000]
john@john-desktop:~/Projects/ToDoList/ToDoList/bin/Debug$


I suspect that there is some incompatibility regarding conversion of args(1) into an Integer value and then comparing the contents of the array 'contents' with this. The file in the above list ( todox.txt) is my backup of the todo.txt file. My solution was to revert to comparing the contents of each element in the array (contents)with args[1] primitive but it worked! - comparing a variable counter with variable deleteme also appears to be a misprint.
Again I would like help with a solution :cry:
JohnMac
 
Posts: 4
Joined: Fri Sep 18, 2009 6:09 am
Location: Parramatta 2150 Australia

Postby Ram » Sun May 02, 2010 11:35 am

Your code works as it should, it's Your execution of the program, that is wrong.
Code: Select all
./ToDoList.exe list

This will put numbers in front of your list, to be used in the deletion, like so. BTW any args[0] that's not "add or del" will produce a list.

Code: Select all
1: Meh
2: Hello from Mono
3: Take over the world
4: Extra Extra
5: Debuging
6: Test OK
7: Problem
8: addition



then
Code: Select all
./ToDoLost.exe del 6

will delete "Test OK"


Code: Select all
 
./ToDoList.exe list
1: Meh
2: Hello from Mono
3: Take over the world
4: Extra Extra
5: Debuging
6: Problem
7: addition

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1682
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby JohnMac » Mon May 03, 2010 6:10 am

:lol: Thank you for straightening out my misconception. I had rewritten the code to test for 'del' input as text (and got it to work!). Knowing the appropriate array index in advance is certainly more efficient.
Communication is difficult when working against preconceived ideas from an earlier era! I admire your skill.
Sincere thanks and I will continue!
JohnMac
 
Posts: 4
Joined: Fri Sep 18, 2009 6:09 am
Location: Parramatta 2150 Australia


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests