954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

read text file and store itin a string

Hi Guys,
I am reading a text file and i want to store that file ina string, below is my code, can anyone tell me what am i doing wrong

private void toolStripButton1_Click(object sender, EventArgs e)
        {
           // OpenFileDialog dialog = new OpenFileDialog();
            FolderBrowserDialog dialog1 = new FolderBrowserDialog();
            parser = new GrammarParser();
            parser.showStep += new GrammarParser.ShowStep(parser_showStep);
            if (dialog1.ShowDialog() == DialogResult.OK)
            {
                string nameD = dialog1.SelectedPath;
              
                string[] names = Directory.GetFiles(nameD, "*txt");
                for (int i = 0; i < names.Length; i++)
                {
                    
                    TextReader txtread = new StreamReader(names[i]);
                   MessageBox.Show(txtread.ReadToEnd());
                    // read file to the string


                   string text = txtread.ReadToEnd();
    
                    parser.Text = text;
                    
                   resultString = parser.SerParserString;
                   
                    
                    //   go to to database

                }
            }
        }
sdhawan
Junior Poster
122 posts since May 2010
Reputation Points: 8
Solved Threads: 0
 

When you are reading a file, the API maintains a file pointer which points to where you are in the file. You can think of this as the position of the cursor. When you call ReadToEnd() , the data is read from the current position to the end of the file.

In your code, you call ReadToEnd() twice. After the first call, the cursor position is at the end of the file. So, when you call it second time, it reads from current position to the end. But since it is at the end already, it gives you an empty string. Hence, you either need to reset the file pointer between calls or (which should be more efficient) read it into a string, and then use that string to display the MessageBox and assign it to the parser object.

Here is the updated code:

private void toolStripButton1_Click(object sender, EventArgs e)
{
    // OpenFileDialog dialog = new OpenFileDialog();
    FolderBrowserDialog dialog1 = new FolderBrowserDialog();
    parser = new GrammarParser();
    parser.showStep += new GrammarParser.ShowStep(parser_showStep);
    if (dialog1.ShowDialog() == DialogResult.OK)
    {
        string nameD = dialog1.SelectedPath;
        string[] names = Directory.GetFiles(nameD, "*txt");
        for (int i = 0; i < names.Length; i++)
        {
            TextReader txtread = new StreamReader(names[i]);
            // read file to the string
            string text = txtread.ReadToEnd();
            txtread.Close(); //be good, close streams
            MessageBox.Show(text);
            parser.Text = text;
            resultString = parser.SerParserString;
        }
    }
}

Hope this helps.

sergb
Junior Poster
105 posts since May 2010
Reputation Points: 17
Solved Threads: 32
 
if (dialog1.ShowDialog() == DialogResult.OK)
{
string nameD = dialog1.SelectedPath;

resultString =  File.ReadAllText(nameD);

}


how abt this?

kdcorp87
Junior Poster in Training
54 posts since Jan 2010
Reputation Points: 14
Solved Threads: 9
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You