reading the data values in textfile
-
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 "
-
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 "
-
That looks like
File.Copy("c:\\Vicky\\DATAS\\matrix.txt", "c:\\Vicky\\DATAS\\copy.txt");
Anyway, you're closing the first stream twice. Removesr.Close();
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 "
-
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 "
-
.txt format..with full of image intensity values from 0 to 16000
" Ella Pugalum iraivan oruvanukkae-A R Rahman "
-
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 "
-
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 "
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
-
You need to close your StreamWriter:
fs.Close();
sr.Close();
fs1.Close();to:
fs.Close();
sr.Close();
sw.Close();
fs1.Close(); -
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 "
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.
-
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 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)