Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. reading the data values in textfile

reading the data values in textfile

Scheduled Pinned Locked Moved C#
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nettai
    wrote on last edited by
    #1

    i am having a text file of 10*10 integer datas...i am reading and writing into another textfile using readline method...but in this program i did some mistake...i couldn't get the output file with datas...here is the program

    private void button1_Click(object sender, EventArgs e)
    {
    try
    {
    string path = "c:\\Vicky\\DATAS\\matrix.txt";
    string path2 = "c:\\Vicky\\DATAS\\copy.txt";

                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
    
                FileStream fs1 = new FileStream(path2, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs1);
    
                string line;
                while ((line = sr.ReadLine()) != null)
                {
    
                    sw.WriteLine(line);
    
                }
    
                fs.Close();
                sr.Close();
                fs1.Close();
    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
    

    " Ella Pugalum iraivan oruvanukkae-A R Rahman "

    L R A D 5 Replies Last reply
    0
    • N Nettai

      i am having a text file of 10*10 integer datas...i am reading and writing into another textfile using readline method...but in this program i did some mistake...i couldn't get the output file with datas...here is the program

      private void button1_Click(object sender, EventArgs e)
      {
      try
      {
      string path = "c:\\Vicky\\DATAS\\matrix.txt";
      string path2 = "c:\\Vicky\\DATAS\\copy.txt";

                  FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                  StreamReader sr = new StreamReader(fs);
      
                  FileStream fs1 = new FileStream(path2, FileMode.Create, FileAccess.Write);
                  StreamWriter sw = new StreamWriter(fs1);
      
                  string line;
                  while ((line = sr.ReadLine()) != null)
                  {
      
                      sw.WriteLine(line);
      
                  }
      
                  fs.Close();
                  sr.Close();
                  fs1.Close();
      
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
      

      " Ella Pugalum iraivan oruvanukkae-A R Rahman "

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      That looks like File.Copy("c:\\Vicky\\DATAS\\matrix.txt", "c:\\Vicky\\DATAS\\copy.txt"); Anyway, you're closing the first stream twice. Remove sr.Close();

      N 1 Reply Last reply
      0
      • L Lost User

        That looks like File.Copy("c:\\Vicky\\DATAS\\matrix.txt", "c:\\Vicky\\DATAS\\copy.txt"); Anyway, you're closing the first stream twice. Remove sr.Close();

        N Offline
        N Offline
        Nettai
        wrote on last edited by
        #3

        thanks for ur reply...i tried what u said...even after it is not working...i know that file.copy method...but i want to deal with the matrix row by row...Is there any other way to deal with these datas...Already i posted one message related to this... http://www.codeproject.com/script/Forums/View.aspx?fid=1649&select=2973846&fr=151&df=90&mpp=25&noise=3&sort=Position&view=Quick#xx2973846xx[^] In that program ,i couldn't get the correct output..May be,i am thinking that Readline method is not the correct approach..Is there any other way to solve this problem

        " Ella Pugalum iraivan oruvanukkae-A R Rahman "

        L 1 Reply Last reply
        0
        • N Nettai

          thanks for ur reply...i tried what u said...even after it is not working...i know that file.copy method...but i want to deal with the matrix row by row...Is there any other way to deal with these datas...Already i posted one message related to this... http://www.codeproject.com/script/Forums/View.aspx?fid=1649&select=2973846&fr=151&df=90&mpp=25&noise=3&sort=Position&view=Quick#xx2973846xx[^] In that program ,i couldn't get the correct output..May be,i am thinking that Readline method is not the correct approach..Is there any other way to solve this problem

          " Ella Pugalum iraivan oruvanukkae-A R Rahman "

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          what is the format of the file?

          N 1 Reply Last reply
          0
          • L Lost User

            what is the format of the file?

            N Offline
            N Offline
            Nettai
            wrote on last edited by
            #5

            .txt format..with full of image intensity values from 0 to 16000

            " Ella Pugalum iraivan oruvanukkae-A R Rahman "

            L 1 Reply Last reply
            0
            • N Nettai

              .txt format..with full of image intensity values from 0 to 16000

              " Ella Pugalum iraivan oruvanukkae-A R Rahman "

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Ok, that's not helping though, what is the actual format? Something like: (({decimal string}; )+{newline})* what does it look like Seriously, this could be important..


              Last modified: 49mins after originally posted --

              1 Reply Last reply
              0
              • N Nettai

                i am having a text file of 10*10 integer datas...i am reading and writing into another textfile using readline method...but in this program i did some mistake...i couldn't get the output file with datas...here is the program

                private void button1_Click(object sender, EventArgs e)
                {
                try
                {
                string path = "c:\\Vicky\\DATAS\\matrix.txt";
                string path2 = "c:\\Vicky\\DATAS\\copy.txt";

                            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                            StreamReader sr = new StreamReader(fs);
                
                            FileStream fs1 = new FileStream(path2, FileMode.Create, FileAccess.Write);
                            StreamWriter sw = new StreamWriter(fs1);
                
                            string line;
                            while ((line = sr.ReadLine()) != null)
                            {
                
                                sw.WriteLine(line);
                
                            }
                
                            fs.Close();
                            sr.Close();
                            fs1.Close();
                
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                

                " Ella Pugalum iraivan oruvanukkae-A R Rahman "

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                You need to close your StreamWriter:

                fs.Close();
                sr.Close();
                fs1.Close();

                to:

                fs.Close();
                sr.Close();
                sw.Close();
                fs1.Close();

                N 1 Reply Last reply
                0
                • N Nettai

                  i am having a text file of 10*10 integer datas...i am reading and writing into another textfile using readline method...but in this program i did some mistake...i couldn't get the output file with datas...here is the program

                  private void button1_Click(object sender, EventArgs e)
                  {
                  try
                  {
                  string path = "c:\\Vicky\\DATAS\\matrix.txt";
                  string path2 = "c:\\Vicky\\DATAS\\copy.txt";

                              FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                              StreamReader sr = new StreamReader(fs);
                  
                              FileStream fs1 = new FileStream(path2, FileMode.Create, FileAccess.Write);
                              StreamWriter sw = new StreamWriter(fs1);
                  
                              string line;
                              while ((line = sr.ReadLine()) != null)
                              {
                  
                                  sw.WriteLine(line);
                  
                              }
                  
                              fs.Close();
                              sr.Close();
                              fs1.Close();
                  
                          }
                          catch (Exception ex)
                          {
                              MessageBox.Show(ex.Message);
                          }
                  

                  " Ella Pugalum iraivan oruvanukkae-A R Rahman "

                  R Offline
                  R Offline
                  riced
                  wrote on last edited by
                  #8

                  Try this.

                  private void button1_Click(object sender, EventArgs e)
                  {
                  try
                  {
                  string path = "c:\\Vicky\\DATAS\\matrix.txt";
                  string path2 = "c:\\Vicky\\DATAS\\copy.txt";

                    using (StreamReader sr = new StreamReader(path))
                    using (StreamWriter sw = new StreamWriter(path2))
                    {
                       string line;
                       while ((line = sr.ReadLine()) != null)
                       {
                          sw.WriteLine(line);
                       }
                    }
                  

                  }
                  catch (Exception ex)
                  {
                  MessageBox.Show(ex.Message);
                  }
                  }

                  You don't need to close the files/streams because using will do it for you. Even if there is an exception. I suggest setting a breakpoint on the while and stepping through the loop a few times.

                  Regards David R

                  1 Reply Last reply
                  0
                  • L Lost User

                    You need to close your StreamWriter:

                    fs.Close();
                    sr.Close();
                    fs1.Close();

                    to:

                    fs.Close();
                    sr.Close();
                    sw.Close();
                    fs1.Close();

                    N Offline
                    N Offline
                    Nettai
                    wrote on last edited by
                    #9

                    thanks man....i got it....

                    " Ella Pugalum iraivan oruvanukkae-A R Rahman "

                    1 Reply Last reply
                    0
                    • N Nettai

                      i am having a text file of 10*10 integer datas...i am reading and writing into another textfile using readline method...but in this program i did some mistake...i couldn't get the output file with datas...here is the program

                      private void button1_Click(object sender, EventArgs e)
                      {
                      try
                      {
                      string path = "c:\\Vicky\\DATAS\\matrix.txt";
                      string path2 = "c:\\Vicky\\DATAS\\copy.txt";

                                  FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                                  StreamReader sr = new StreamReader(fs);
                      
                                  FileStream fs1 = new FileStream(path2, FileMode.Create, FileAccess.Write);
                                  StreamWriter sw = new StreamWriter(fs1);
                      
                                  string line;
                                  while ((line = sr.ReadLine()) != null)
                                  {
                      
                                      sw.WriteLine(line);
                      
                                  }
                      
                                  fs.Close();
                                  sr.Close();
                                  fs1.Close();
                      
                              }
                              catch (Exception ex)
                              {
                                  MessageBox.Show(ex.Message);
                              }
                      

                      " Ella Pugalum iraivan oruvanukkae-A R Rahman "

                      A Offline
                      A Offline
                      Alan N
                      wrote on last edited by
                      #10

                      Hi, I can't see any obvious mistake in the code, so you should look for clues. 1) Does the input file contain the expected data 2) Do you get an output file. If yes is it empty 3) Does the output file timestamp get updated 4) Trace the code using the debugger and determine if the string variable 'line' contains the expected data from the input file. Once you have done all of that come back to us and tell us what you have found. Alan.

                      1 Reply Last reply
                      0
                      • N Nettai

                        i am having a text file of 10*10 integer datas...i am reading and writing into another textfile using readline method...but in this program i did some mistake...i couldn't get the output file with datas...here is the program

                        private void button1_Click(object sender, EventArgs e)
                        {
                        try
                        {
                        string path = "c:\\Vicky\\DATAS\\matrix.txt";
                        string path2 = "c:\\Vicky\\DATAS\\copy.txt";

                                    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                                    StreamReader sr = new StreamReader(fs);
                        
                                    FileStream fs1 = new FileStream(path2, FileMode.Create, FileAccess.Write);
                                    StreamWriter sw = new StreamWriter(fs1);
                        
                                    string line;
                                    while ((line = sr.ReadLine()) != null)
                                    {
                        
                                        sw.WriteLine(line);
                        
                                    }
                        
                                    fs.Close();
                                    sr.Close();
                                    fs1.Close();
                        
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                        

                        " Ella Pugalum iraivan oruvanukkae-A R Rahman "

                        D Offline
                        D Offline
                        DaveyM69
                        wrote on last edited by
                        #11

                        A general method for file copying, based on what you have, would look something like this:

                        bool FileCopy(string path1, string path2)
                        {
                        try
                        {
                        using (FileStream readFileStream = new FileStream(path1, FileMode.Open, FileAccess.Read))
                        {
                        using (StreamReader streamReader = new StreamReader(readFileStream))
                        {
                        using (FileStream writeFileStream = new FileStream(path2, FileMode.Create, FileAccess.Write))
                        {
                        using (StreamWriter streamWriter = new StreamWriter(writeFileStream))
                        {
                        string line;
                        while ((line = streamReader.ReadLine()) != null)
                        {
                        streamWriter.WriteLine(line);
                        }
                        }
                        }
                        }
                        }
                        return true;
                        }
                        catch
                        {
                        return false;
                        }
                        }

                        Dave
                        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups