473,378 Members | 1,543 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to read text from a file and populating a message?

8
Does anybody know how to read from a text file and if it contains a certain word or phrase (for example - error) to then flag up the error?
if possible...quoting the whole line of text the "Error" is on?
May 27 '10 #1

✓ answered by Frinavale

Hehe sorry that's my fault...for some weird reason I thought you were trying to mix the two types of applications together (it doesn't work well).

In order to see what's happen add a Console.ReadLine() to the end of your main method....this will cause the console application to wait for the user to hit "enter" before closing :)

-Frinny

6 1274
Frinavale
9,735 Expert Mod 8TB
Well first you need to read the file into memory.
Then I would probably use Regular Expressions to find any line that contains the word that you're searching for. If that word exists then just use the MessageBox (assuming this is a desktop application and not a web application) to display your message.

What part are you stuck on?
May 27 '10 #2
kieth
8
thank you for the reply,

this is what i have, but at the minute... i dont know what it's doing because the cmd box opens and closes instantly not allowing me to see any results
Expand|Select|Wrap|Line Numbers
  1.   {
  2.             FileStream fout;
  3.  
  4.             System.IO.FileInfo f = new System.IO.FileInfo("C:\\Documents and Settings\\kwareham01\\Desktop\\test log.txt");
  5.             // System.IO.FileStream s = f.Open(System.IO.FileMode.Open, System.IO.FileAccess.Read);
  6.             //  Console.WriteLine("Entry does not exist in database");
  7.             StreamReader s = f.OpenText();
  8.             string read = null;
  9.             while ((read = s.ReadLine()) != null)
  10.  
  11.                 if (read.Contains("Entry does not exist in database"))
  12.                 {
  13.  
  14.                     // open output file 
  15.                     try
  16.                     {
  17.                         fout = new FileStream("test.txt", FileMode.Create);
  18.                     }
  19.                     catch (IOException exc)
  20.                     {
  21.                         Console.WriteLine(exc.Message + "\nError Opening Output File");
  22.                         return;
  23.                     }
May 27 '10 #3
Frinavale
9,735 Expert Mod 8TB
Well maybe that's because it looks like you're writing a Console application?
What kind of application are you developing?
May 27 '10 #4
kieth
8
sorry if it's unclear, i'm still very new to programming...

I am trying to develop a console app, that will run through error logs, if it finds an error it flags it up...

to save the manual process of reading through lots of logs.

i may well just be misguided in my approach, in which case it will be back to the books...

just thought i'd pop it on here on the off chance

thanks
May 27 '10 #5
Frinavale
9,735 Expert Mod 8TB
Hehe sorry that's my fault...for some weird reason I thought you were trying to mix the two types of applications together (it doesn't work well).

In order to see what's happen add a Console.ReadLine() to the end of your main method....this will cause the console application to wait for the user to hit "enter" before closing :)

-Frinny
May 27 '10 #6
kieth
8
sorted, thank you
May 28 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
14
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most...
8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
7
by: Drew Berkemeyer | last post by:
Hello, I'm using the following code to read a text file in VB.NET. Dim sr As StreamReader = File.OpenText(strFilePath) Dim input As String = sr.ReadLine() While Not input Is Nothing...
4
by: Volker Jobst | last post by:
Hi, Is there a really fast way to read a text file which contains lines of variable length? I'm using the StreamReader to read the file, but this is not as fast as I need it. thanks a lot...
4
by: Nina | last post by:
Hi everyone, Do you know why the following code only read certain number of lines of text file, but not the entire file? Dim sr As StreamReader Dim str As String Dim al As ArrayList = New...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
1
by: John | last post by:
I have a process that reads a text file then uploads the data into a database table. The text file has 10 lines at the end of the file that are blank BUT it appears that the enter key or space bar...
3
by: John | last post by:
How can I tell if a line begins with a number instead of a character? I need to read a text file and some of the data begins with a character which is causing me an issue in reading and uploading...
3
by: jasvinder singh | last post by:
Respected Sir/madam, Can you help in providing code in 'C' for Reading text file with n number of rows and columns and putting the result in arrays.The sample file is as follows: rim_label =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.