blah blah blah is here! blah blah » Close

up0down
link

Dear All,

My application runs with input from a text file. So it parse the text file, and then use the value inside to do several things.

But last time, a typo occured, and the application shows and error. Lucky I put exception to the application. I just wondering whether I can make another application with C# that will scan and detect syntax error inside the text file, so before uploading it, the error is detected.

Any idea how to do this? what came into my mind is I parse the text and then check for value error. But how if I put command also inside the text file? Like a simple loop.

Kindest Regards,




E

last answered one year ago

2 answers

up0down
link

well if this is available, its preferable to fix any syntax error before inserting it to the text file like when the user wants to save the data. now concerning the checker, its all dependent on the way of the text that is being parsed so if you want to keep the program safe and fast, make a checker that will check the file in a time interval. this checker should do the same parsing of the file when u need to retrieve a specific data from so you won't feel that anything is being done but it will be in the background.
To make a thread:

void CheckFile()
{
while(true)
{
try
{
ParseFile();
}
catch(Exception){}
System.Threading.Thread.Sleep(20000);//check every 20 seconds
}
}
// in the main add this checker thread:
new System.Threading.Thread(CheckFile).Start();

thanks for the input muster, anyway I was thinking of doing it sequencially, so it was like this: button -> opentxt -> parse -> and now, it is like button -> opentxt -> check -> if error dont parse, if not error, parse and use as input. So it is like a compiler in between them. What come into my mind is on how to 'compile' the text file and see whether there is any mistake like typo made by the user. Thanks :)

up0down
link

Here's a possible pattern for dealing with that situation, if I've understood it correctly:

bool CheckFile(string filePath, out string[] lines)
{
lines = System.IO.File.ReadAllLines(filePath);
bool isValid = true;
foreach(string line in lines)
{
// code to check for bad line
if (bad)
{
isValid = false;
break;
}
}
if (!isValid) lines = null;
return isValid;
}

// code to call this method
string[] lines;
if (CheckFile(filePath, out lines))
{
// do something with lines
}
else
{
MessageBox.Show(filePath + " contains errors");
}

I'd only run CheckFile on a separate thread if you're processing large files and want to keep the UI responsive in the meantime.

Incidentally, given that you're talking about 'syntax checking', does the text file contain source code in C# or some other programming language?

Feedback