How to pass a filename/path to text parser - c#

I'm trying to get a simple text parser class to work in VS2015. I received the class code and built a basic Console Application, added the class Cawk and tried to compile/run it.
The main error that I get is
Argument 1: cannot convert from 'string' to 'System.IO.StreamReader'
It's clear that I can't figure out how to pass a filename through Main to Cawk. How do I give it an argument of a filename?
Any help or pointers would be appreciated.
My Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main()
{
string input = #"c:\temp\test.txt";
Cawk.Execute(input);
}
}
}
Snippet of My Cawk.cs:
using System;
using System.Collections.Generic;
using System.IO;
namespace ConsoleApplication3
{
public static class Cawk
{
public static IEnumerable<Dictionary<string, object>> Execute(StreamReader input)
{
Dictionary<string, object> row = new Dictionary<string, object>();
string line;
//string[] lines = File.ReadAllLines(path);
//read all rows
while ((line = input.ReadLine()) != null)
{

Execute accepts a StreamReader not a string.
Cawk.Execute(new StreamReader(#"c:\temp\test.txt"))
However, you should close the stream after you are done with it.
using (var sr = new StreamReader(#"c:\temp\test.txt"))
{
Cawk.Execute(sr);
}

something like:
var sr = new System.IO.StreamReader(#"c:\temp\test.txt");
Cawk.Execute(sr);

Simply use the File class from the System.IO namespace.
Cawk.Execute(File.OpenText(#"c:\temp\test.txt"));

Like this:
string input = #"c:\temp\test.txt";
Cawk.Execute(new System.IO.StreamReader(input));
You can put using System.IO; to the top like the rest of the usings, then you don't have to write it out later.

Related

Check if filename matches particular pattern or not

I have many files in folder. and whenever there is any update in any file I Receive an event for that in my windows service application.
And I am looking for something by which I can validate the file with specific pattern. If it matches then only that file should be processed or else it should be ignored.
Something like this
if(File.Matches("genprice*.xml"))
{
DoSomething();
}
genprice20212604.xml
genprice20212704.xml
price20212604.xml
genprice20212704.txt
From above only #1 and #2 should be processed others should be ignored.
Your can try with regular expressions:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace ConsoleAppRegex
{
class Program
{
static void Main(string[] args)
{
string[] fileNames = new string[] { "genprice20212604.xml",
"genprice20212704.xml",
"price20212604.xml",
"genprice20212704.txt"};
Regex re = new Regex(#"genprice[^\.]*.xml");
foreach (string fileName in fileNames)
{
if (re.Match(fileName).Success)
{
Console.WriteLine(fileName);
}
}
Console.ReadLine();
}
}
}
I suggest to use Regex:
using System.Text.RegularExpressions;
using System.IO;
var reg = new Regex(#"genprice\d{8}$");
var fileNamesFromFolder = Direcotory.GetFiles(" #FolderĀ“s path ", "*.xml")
.Where(path => reg.IsMatch(Path.GetFileNameWithoutExtension(path)))
.Select(Folder=>
Path.GetFileNameWithoutExtension(Folder));
foreach (var file in fileNamesFromFolder )
{
//Do something...
}

error return keyword must not be followed by an object expression

The return(match) is giving me a headache! When I change it into Console.WriteLine(match) it gives me the return I expect but when I try to use the return(match) it gives me an error. I just don't know what to change here so any suggestions would be appreciated!
regards, James
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ReadTextFile
{
class Program
{
static void Main(string[] args)
{
List<string> fileLines = new List<string>();
using (var reader = new StreamReader("test.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
fileLines.Add(line);
string pattern = #"(\w+)#(\w+).([a-z]+)";
Match match = Regex.Match((line), pattern);
if (match.Success)
{
return(match);
}
}
}
Main returns a void if you want to return something else change the signature. Note that typically the return value of Main is called an error code where non-zero is considered an error.
If you create another function and put your logic into it you could then use return.
On the other hand if all you are looking to do is leave the while you instead need to make Match match; be after string line; and use break; to get out instead of return.
You're in a void method (void Main). void methods do not have a return value, so just return; by itself is all that's allowed.
BTW, return is not a function in C#, it's a statement, so there is no need for parenthesis.

fetching only one url in c#

I am working on collecting urls from the web site in C# using WatiN framework. In my program it is fetching only one url. I don't know what is the problem. Any help will be appreciated.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WatiN.Core;
using WatiN.Core.Native.InternetExplorer;
namespace magicbricks
{
class scroll
{
[STAThread]
static void Main(string[] args)
{
Browser browserInstance;
browserInstance = new IE(#"http://www.99acres.com/property-in-chennai- ffid?search_type=QS&search_location=CP32&lstAcn=CP_R&lstAcnId=32&src=CLUSTER&isvoicesearch=N&keyword_suggest=chennai%20%28all%29%3B&fullSelectedSuggestions=chennai%20%28all%29&strEntityMap=W3sidHlwZSI6ImNpdHkifSx7IjEiOlsiY2hlbm5haSAoYWxsKSIsIkNJVFlfMzIsIFBSRUZFUkVOQ0VfUywgUkVTQ09NX1IiXX1d&texttypedtillsuggestion=chennai&refine_results=Y&Refine_Localities=Refine%20Localities&action=%2Fdo%2Fquicksearch%2Fsearch&suggestion=CITY_32%2C%20PREFERENCE_S%2C%20RESCOM_R");
foreach (var links in browserInstance.Links.Filter(Find.ByClass("b")))
{
Console.WriteLine(links.Url);
String filePath = "C:/Users/User/Desktop/New folder";
String fileName = "newop4.csv";
using (StreamWriter sr = new StreamWriter(Path.Combine(filePath, fileName), true))
{
sr.WriteLine(links.Url);
}
Console.ReadLine();
}
}
}
}
the above code prints only one url in the console.
Remove the Console.ReadLine(); As you are in a ForEach loop. If you still want the Console.ReadLine(); move it out the foreach
The Console.ReadLine(); waits for a user input, after you enter any value you should see the next URL.

Using Vorbis and NAudio to play OGG files

I'm trying to play OGG file stream with NVorbis and NAudio, as described in the documention i'm trying to access VorbisWaveReader class, without success, here is my code:
using System;
using System.Collections.Generic;
using System.Text;
using NVorbis;
using NAudio;
namespace Paradise
{
class Program
{
static void Main(string[] args)
{
using (var vorbis = new NVorbis.NAudioSupport.VorbisWaveReader(#"C:\PATH\TO\OGG\FILE.ogg"))
using (var waveOut = new NAudio.Wave.WaveOut())
{
waveOut.Init(vorbis);
waveOut.Play();
}
}
}
}
I'm getting the following error:
type or namespace name 'VorbisWaveReader' does not exist in the namespace 'NVorbis.NAudioSupport'
It looks very basic and should work, i can see in the source code that VorbisWaveReader is present in the code, can you help me go thorugh that?
Thanks!
NVorbis.NAudioSupport change into NAudio.Vorbis
in Package-Manager Console type this:
Install-Package NAudio.Vorbis

How to print out the result of a SWI-Prolog query from C# using Swi-cs-pl library

I'm trying to understand how the Swi-cs-pl library works by doing my own little program, but I cannot printout any result from a query based on the example from SWI-Prolog web.
I have this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SbsSW.SwiPlCs;
namespace HolaMundo
{
class Program
{
static void Main(string[] args)
{
if (!PlEngine.IsInitialized)
{
String[] param = { "-q" }; // suppressing informational and banner messages
PlEngine.Initialize(param);
PlQuery.PlCall("assert(father(hader, nevar))");
PlQuery.PlCall("assert(father(hader, sergio))");
PlQuery.PlCall("assert(brother(nevar, sergio):- father(hader, nevar), father(hader, sergio))");
//How do I write out in a Console the answer for a query like:
// brother(nevar, sergio)
PlEngine.PlCleanup();
}
}
}
}
As I said on my code, I just want to query something basic like: brother(nevar, sergio). and get any answer from Console like true or whatever.
Can anyone help me?

Categories

Resources