473,386 Members | 1,786 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,386 software developers and data experts.

How to Read a Specific Line from A Text File?

2
Hi, I'm writing a console program in order to make my own keyboard shortcuts.
Expand|Select|Wrap|Line Numbers
  1. ConsoleKeyInfo input;
  2.  
  3. do
  4. {
  5.        if (File.Exists("info.txt") == true) ; 
  6.        else Console.WriteLine("File not found! \nPress any key to try again..."); Console.ReadKey(true);
  7. } while (File.Exists("info.txt") == false);
  8.  
  9. StreamReader reader = new StreamReader("info.txt");
  10.  
  11. do
  12. {
  13.        input = Console.ReadKey(true);
  14.        if (input.Key == ConsoleKey.R && input.Modifiers == ConsoleModifiers.Control)
  15.        {
  16.              Process.Start(reader.ReadLine()); 
  17.        }
  18.        if (input.Key == ConsoleKey.B && input.Modifiers == ConsoleModifiers.Control)
  19.        {
  20.              Process.Start(reader.ReadLine());
  21.        }
  22. } while (input.Key != ConsoleKey.Escape);
I want (R+Ctrl) to process.start the first line of info.txt, while (B+Ctrl) only the second line.

Whether pressing (R+Ctrl) or (B+Ctrl), at the first press the console readlines the first line, and at the second keypress the console readlines the second line; afterwards since there is no third line, pressing those keys crashes the console.

Thanks for help.
Feb 13 '11 #1

✓ answered by Aimee Bailey

to read a specific line in a text file, use the following:

Expand|Select|Wrap|Line Numbers
  1. string filename = "test.txt";
  2.  
  3. if (File.Exists(filename))
  4. {
  5.     string[] lines = File.ReadAllLines(filename);
  6.     Console.WriteLine(lines[3]);
  7. }
  8.  
where 3 can be replaced with any line number (zero based).

2 26546
Aimee Bailey
197 Expert 100+
to read a specific line in a text file, use the following:

Expand|Select|Wrap|Line Numbers
  1. string filename = "test.txt";
  2.  
  3. if (File.Exists(filename))
  4. {
  5.     string[] lines = File.ReadAllLines(filename);
  6.     Console.WriteLine(lines[3]);
  7. }
  8.  
where 3 can be replaced with any line number (zero based).
Feb 14 '11 #2
Logic
2
Thank you very much...

I run the program; when I initially press (R+Ctrl) or (B+Ctrl), then the pressed key(s) are only Console.ReadKey(true)'ed as you can see on the sixth code line in the above code.

How to make that specific [Console.ReadKey(true)] on the 6th line to execute only if the the statement is false.
Feb 14 '11 #3

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

Similar topics

2
by: David Thomas | last post by:
Hi there, I am trying to store data in a text file and output it to the browser using PHP. All very easy - if I was using English! the problem is, I want to use Japanese and I'm finding it a tad...
3
by: H Brown New To It | last post by:
I am very new to Java. How would you go about making Java program that reads in a fixed length text file and pass the data into a table in an sql table? The text file data looks something like...
2
by: cricfan | last post by:
I'm parsing a text file to extract word definitions. For example the input text file contains the following content: di.va.gate \'di_--v*-.ga_-t\ vb pas.sim \'pas-*m\ adv : here and there :...
13
by: Dan V. | last post by:
How do I create a one line text file with these control codes? e.g.: 144 = 0x90 and 147 = 0x93? I am trying to create a one line text file with these characters all one one row with no spaces. ...
8
by: MLH | last post by:
Am trying to import 20,000+ lines of text in a file FTP'd from a UNIX platform to windows via FTP session in a DOS box. About 2000 records have multiple lines in them separated by CRLF's. ...
1
by: Saravanan | last post by:
I need to read in a text file as strings and then write some of these strings into another text file. The output file is in the working directory while the input text file is not. how can I...
6
by: arf | last post by:
Hello, i have a problem where i need read dataset from text file and keep them into a vector. e.g. dataset: "0012 0033 00045 0044 0047" Based on this dataset, i need to keep every 4...
7
by: voroojak | last post by:
Hi I have a text file that has differnt information to be more clear i hav eto say that i have three differnt kind of information. the column name for all the information are same but there is...
4
by: Johnymap | last post by:
Hi everyone. The text file i am using has the following format: "2001_1.rtf" (1 January 2001) Ookame v Tombale (CCF No. 203 of 2001) BWHC 1 "2001_2.rtf" (1 January 2001) Kgaimena v Leoifo...
2
by: kino | last post by:
Hello, I am supposed to read from a text file a series of numbers and then store them in a struct array. Here's my code: int main() { ifstream In; string folder, filename, name2,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.