tags:

views:

118

answers:

5
+2  Q: 

copy a text file

I am trying to copy a text file in an other text file line by line. It seems that there is a buffer of 1024 character. If there is less than 1024 character in my file, my function will not copie in the other file.

Also if there is more than 1024 character but less a factor of 1024, these exceeding characters will not be copied.

Ex:

2048 character in initial file - 2048 copied

988 character in initial file - 0 copied

1256 character in initial file - 1024 copied

private void button3_Click(object sender, EventArgs e)
{
    // écrire code pour reprendre le nom  du fichier sélectionné et 
    //ajouter un suffix "_poly.txt"
    string ma_ligne;
    const int RMV_CARCT = 9;

    //délcaration des fichier
    FileStream apt_file = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
    textBox1.Text = textBox1.Text.Replace(".txt", "_mod.txt");
    FileStream mdi_file = new FileStream(textBox1.Text, FileMode.OpenOrCreate,FileAccess.ReadWrite);

    //lecture/ecriture des fichiers en question
  StreamReader apt = new StreamReader(apt_file);
  StreamWriter mdi_line = new StreamWriter(mdi_file, System.Text.Encoding.UTF8, 16);



  while (apt.Peek() >= 0)
  {
      ma_ligne = apt.ReadLine();
      //if (ma_ligne.StartsWith("GOTO"))
      //{
      //   ma_ligne = ma_ligne.Remove(0, RMV_CARCT);
      //   ma_ligne = ma_ligne.Replace(" ","");
      //   ma_ligne = ma_ligne.Replace(",", " ");
      mdi_line.WriteLine(ma_ligne);
      //}
  }
  apt_file.Close();
  mdi_file.Close();
}
+2  A: 

You know, that there is a File.Copy()-method? And FileInfo has a Copy()-method, too.
Edit: I see you want to modify the file content during copy, although this is commented out right, now.

For a usage of StreamReader you can have a look at the msdn documentation. It suggests an implementation without Peek():

 using (StreamReader sr = new StreamReader("TestFile.txt")) 
 {
      string line;
      // Read and display lines from the file until the end of 
      // the file is reached.
      while ((line = sr.ReadLine()) != null) 
      {
          Console.WriteLine(line);
      }
 }
tanascius
It looks like he's doing some processing to each line as he copies it.
Shane Fulmer
from the commented code, it seems that the goal is to read the file line-by-line and modify each line before it is written to the output file.
M4N
@Shane, @Martin: Thanks for pointing out - you are probably right and my answer won't help in his case
tanascius
Thrue but we are looking for specific lines and modifying them. Like shown in the code in comments.Everything actually works, the problem is that it will not copy the last lines because the buffer is incomplete.
+1  A: 

you'll want:

using (StreamReader apt= new StreamReader(apt_file)) 
{
    while (apt.Peek() > -1){ ...}
 }

see: http://msdn.microsoft.com/en-us/library/system.io.streamreader.peek.aspx

Glennular
This has nothing to do with his problem.
Adam Robinson
I believe what he has >= 0 is equivalent to > -1 in this case since the return value is int.
David
+5  A: 

Two issues:

  1. Your FileStream, StreamWriter, and StreamReader classes should be inside using { } blocks. They implement IDisposable, so you need to be calling Dispose, and the using block will do that for you. If you do this, that's actually all you have to fix (which I'll explain in a minute). Doing this also means you no longer need to call Close().
  2. At a minimum, call mdi_line.Flush() before closing it. This will cause the buffer to be written to the file immediately.

Calling Dispose on the StreamWriter class autmatically calls Flush, which is why the using block will correct the problem.

using (FileStream apt_file = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read))
{
    textBox1.Text = textBox1.Text.Replace(".txt", "_mod.txt");

    using (FileStream mdi_file = new FileStream(textBox1.Text, FileMode.OpenOrCreate, FileAccess.ReadWrite))
    {
        //lecture/ecriture des fichiers en question 
        using (StreamReader apt = new StreamReader(apt_file))
        using (StreamWriter mdi_line = new StreamWriter(mdi_file, System.Text.Encoding.UTF8, 16))
        {
            while (apt.Peek() >= 0)
            {
                ma_ligne = apt.ReadLine();
                //if (ma_ligne.StartsWith("GOTO")) 
                //{ 
                //   ma_ligne = ma_ligne.Remove(0, RMV_CARCT); 
                //   ma_ligne = ma_ligne.Replace(" ",""); 
                //   ma_ligne = ma_ligne.Replace(",", " "); 
                mdi_line.WriteLine(ma_ligne);
                //} 
            }
        }
    }
}
Adam Robinson
mdi_lineFlush() works great!!!Thkx!
@melt: It will work, but you really *should* use the `using` blocks like I depicted. You should just be able to copy and paste that code into your application as a replacement for what's there.
Adam Robinson
A: 

You could read the file using the ReadLine() method like this:

using (StreamReader sr = new StreamReader(inputFile)) 
{
    String line;
    while ((line = sr.ReadLine()) != null) 
    {
       // process line
    }
}
M4N
This has nothing to do with his problem.
Adam Robinson
A: 

How about just buffer on your own and apt.ReadBlock() into the buffer. Then split on \r\n on the buffered content, process the lines in your split array. Dont process the last line in your splitted buffer, prepend it to the next ReadBlock, rinse repeat.

David