how to extract text from text file and store it in a variable using c#
-
Hey Kindly do me a favor and tell me
-
Hey Kindly do me a favor and tell me
Syed_Owais wrote:
Hey Kindly do me a favor and tell me
Tell you what? We have no idea what you need to know - or even what you already do know. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So we have no idea what is "in" your file, how it is stored, what kind of file it is in - and there are a massive number of possibilities - or even if you know how to read anything from a file! Calling it "a text file" tells us very little: there are large number of ways that text can be used to store into, and an equally large number of different types of information you coudl store in it! So sit down, think about what you are trying to do, look at your data file, and try to clarify in your own mind exactly what help you need, then try to explain that.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Syed_Owais wrote:
Hey Kindly do me a favor and tell me
Tell you what? We have no idea what you need to know - or even what you already do know. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So we have no idea what is "in" your file, how it is stored, what kind of file it is in - and there are a massive number of possibilities - or even if you know how to read anything from a file! Calling it "a text file" tells us very little: there are large number of ways that text can be used to store into, and an equally large number of different types of information you coudl store in it! So sit down, think about what you are trying to do, look at your data file, and try to clarify in your own mind exactly what help you need, then try to explain that.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
Reading text from a text file is basically "programming 101". Simply Google for "C# read a text file" and start reading. This is far more than documented all over the web.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
Syed_Owais wrote:
Hey Kindly do me a favor and tell me
Tell you what? We have no idea what you need to know - or even what you already do know. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So we have no idea what is "in" your file, how it is stored, what kind of file it is in - and there are a massive number of possibilities - or even if you know how to read anything from a file! Calling it "a text file" tells us very little: there are large number of ways that text can be used to store into, and an equally large number of different types of information you coudl store in it! So sit down, think about what you are trying to do, look at your data file, and try to clarify in your own mind exactly what help you need, then try to explain that.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
This is weird. I replied to the OP, not you. Hmmm
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
Hey Kindly do me a favor and tell me
Reading text from a text file is basically "programming 101". Simply Google for "C# read a text file" and start reading. This is far more than documented all over the web.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
This is weird. I replied to the OP, not you. Hmmm
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakIt looks fine - but I got an email for it, so ... Hamsters at the Eggnog, I suspect.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Hey Kindly do me a favor and tell me
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:
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:
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 plannin
-
Reading text from a text file is basically "programming 101". Simply Google for "C# read a text file" and start reading. This is far more than documented all over the web.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakAnother "get a brain" response that is irrelevant to the question asked. Down-voted.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
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:
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:
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 plannin
Thank you for this post, which actually responds to the question asked ! Up-voted.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
Another "get a brain" response that is irrelevant to the question asked. Down-voted.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
I'm sorry. Are you not enjoying your urinated Cheerio's this morning?
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
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:
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:
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 plannin
-
Hey Kindly do me a favor and tell me
File.WriteAllText( "data.txt", "Hallelujah, I'm a bum!" ); string someVariable = File.ReadAllText( "data.txt" ).Split( new char\[\] { ',', ' ' } ).First(); Console.Write( someVariable ); Console.ReadKey();
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal