Click here to Skip to main content
15,887,175 members
Home / Discussions / C#
   

C#

 
Questionhow to extract text from text file and store it in a variable using c# Pin
Syed_Owais24-Dec-18 20:54
professionalSyed_Owais24-Dec-18 20:54 
AnswerRe: how to extract text from text file and store it in a variable using c# Pin
OriginalGriff24-Dec-18 21:04
mveOriginalGriff24-Dec-18 21:04 
GeneralRe: how to extract text from text file and store it in a variable using c# Pin
Dave Kreskowiak25-Dec-18 4:46
mveDave Kreskowiak25-Dec-18 4:46 
GeneralRe: how to extract text from text file and store it in a variable using c# Pin
OriginalGriff25-Dec-18 5:00
mveOriginalGriff25-Dec-18 5:00 
AnswerRe: how to extract text from text file and store it in a variable using c# Pin
Dave Kreskowiak25-Dec-18 4:46
mveDave Kreskowiak25-Dec-18 4:46 
GeneralRe: how to extract text from text file and store it in a variable using c# Pin
BillWoodruff27-Dec-18 16:17
professionalBillWoodruff27-Dec-18 16:17 
GeneralRe: how to extract text from text file and store it in a variable using c# Pin
Dave Kreskowiak27-Dec-18 17:45
mveDave Kreskowiak27-Dec-18 17:45 
AnswerRe: how to extract text from text file and store it in a variable using c# Pin
SawmillTurtle25-Dec-18 22:06
SawmillTurtle25-Dec-18 22:06 
I'm going to run with the assumption that you didn't mean to sound as snarky as you did. For future reference, saying "Do me a favor and tell me" is considered bad manners. It would be a good idea to explain what it is you're trying to do and then use "please" and "thank you".

I'm going to give you basic instructions for reading from a raw text file. If you're trying to read binary data, this won't help much.

1. You need to start by placing "using System.IO" at the top of your project.
2. You need to declare an instance of StreamReader, open the file and read a line of text using the StreamReader. This is a watered down version of the class I created for my own projects:

C#
using System;
using System.IO;

namespace FileOperations
{
	public class LoadData
	{
		StreamReader reader;

		public void open(string s)
		{
			reader=new StreamReader(s);
		}

		public string read()
		{
			return reader.ReadLine();
		}

		public void close()
		{
			reader.Close();
		}
    }
}


The official Microsoft documentation contains excellent instructions and another example. You can find that HERE. I break everything up like I do so that I'm not stuck reading every line in the file at once like in the Microsoft example. I can call the read() function and read one line at a time, or I can use a for or while loop to call it again and again. I can also leave the file open if I need to or close it immediately, depending on my coding needs.

I would recommend that if you use this code, you implement a way to check for the end of the file. That would be as simple (as the Microsoft example shows you) as checking to see if the StreamReader returns a null string.

Also, since your question shows a bit of inexperience, I feel the need to point out that you should declare a new instance of "LoadData". Here's a simple example:

C#
LoadData ld = new LoadData();
ld.open("myfile.txt");
string readLine=ld.read();
ld.close();


You can also simply copy the functions as they are into your existing code and call them without the need of a separate class, but by doing it this way you make this code available across your entire application.

And be sure to add your own namespace at the top.

Good luck, and happy coding.

-Turtle

EDIT: It slipped my mind to mention that you don't need to do it through function calls at all if you aren't planning to add anything to them, since they serve no purpose in the watered down form in which I've given them to you. In their unedited form they do much more. If you didn't have the need to parse the text after reading it or look for specific lines or anything like that, you could simply change the last bit of code to this:

C#
StringReader reader = new StringReader("myfile.text");
string getTextFromFile=reader.ReadLine();
reader.close();


I felt I should point that out before someone felt the need to comment on the redundancy of my example class. Laugh | :laugh:

modified 26-Dec-18 4:52am.

GeneralRe: how to extract text from text file and store it in a variable using c# Pin
BillWoodruff27-Dec-18 16:19
professionalBillWoodruff27-Dec-18 16:19 
GeneralRe: how to extract text from text file and store it in a variable using c# Pin
Richard MacCutchan27-Dec-18 22:25
mveRichard MacCutchan27-Dec-18 22:25 
AnswerRe: how to extract text from text file and store it in a variable using c# Pin
Gerry Schmitz28-Dec-18 9:08
mveGerry Schmitz28-Dec-18 9:08 
QuestionRendering Kleinian group fractals Pin
Member 1157700824-Dec-18 19:19
Member 1157700824-Dec-18 19:19 
AnswerRe: Rendering Kleinian group fractals Pin
Dave Kreskowiak25-Dec-18 4:43
mveDave Kreskowiak25-Dec-18 4:43 
GeneralRe: Rendering Kleinian group fractals Pin
BillWoodruff26-Dec-18 7:43
professionalBillWoodruff26-Dec-18 7:43 
GeneralRe: Rendering Kleinian group fractals Pin
Dave Kreskowiak26-Dec-18 7:58
mveDave Kreskowiak26-Dec-18 7:58 
GeneralRe: Rendering Kleinian group fractals Pin
BillWoodruff27-Dec-18 16:25
professionalBillWoodruff27-Dec-18 16:25 
GeneralRe: Rendering Kleinian group fractals Pin
Member 1157700828-Dec-18 3:07
Member 1157700828-Dec-18 3:07 
GeneralRe: Rendering Kleinian group fractals Pin
Dave Kreskowiak28-Dec-18 3:27
mveDave Kreskowiak28-Dec-18 3:27 
GeneralRe: Rendering Kleinian group fractals Pin
Member 1157700828-Dec-18 3:34
Member 1157700828-Dec-18 3:34 
AnswerRe: Rendering Kleinian group fractals Pin
BillWoodruff25-Dec-18 13:11
professionalBillWoodruff25-Dec-18 13:11 
GeneralRe: Rendering Kleinian group fractals Pin
Member 1157700828-Dec-18 3:31
Member 1157700828-Dec-18 3:31 
GeneralRe: Rendering Kleinian group fractals Pin
BillWoodruff28-Dec-18 10:43
professionalBillWoodruff28-Dec-18 10:43 
QuestionC# - least square method Pin
Member 1409938024-Dec-18 8:46
Member 1409938024-Dec-18 8:46 
AnswerRe: C# - least square method Pin
Pete O'Hanlon24-Dec-18 8:51
mvePete O'Hanlon24-Dec-18 8:51 
GeneralRe: C# - least square method Pin
Member 1409938024-Dec-18 9:27
Member 1409938024-Dec-18 9:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.