0

I am trying to read an image file and write it into a new file. But the written image file is not a supported file. please tell me about what is the proper way to read/write image files. Help me!!

Here is my full code. And I did not get any error.

using System;
using System.IO;

namespace readfile
{
    class Program
    {
        static int totalbyte = 0;
        static void Main(string[] args)
        {
            string path = "C:/Users/Nitesh Rathi/Downloads/"; // file's path.
            string filename = "IMG_20200317_150302.jpg"; // file's name.
            string fullpath = path + filename;

            readfile(fullpath);
            writefile(filename);

            Console.ReadKey(false);
        }

        static void readfile(string path)
        {
            FileStream stm = File.Open(path, FileMode.Open); // open a file using filestream.
            int size = (int)stm.Length; // size of the file.
            byte[] data = new byte[stm.Length]; // file buffer.

            while (size > 0) // loop until file' size is not 0.
            {
                int read = stm.ReadByte(); // reading file's data.
                size -= read;
                totalbyte += read;
            }
        }
        static void writefile(string filename)
        {
            FileStream stm = File.Create(filename); // create a new file.
            byte[] data = new byte[totalbyte]; // file's data.

            Console.WriteLine("Writing data into file...");

            stm.Write(data, 0, data.Length); // writing data into created file.

            Console.WriteLine("data has been wrote into file.");
            stm.Close();
        }
    }
}

I also used FileStream.Read() method. But it is also not working for me.

Oguz Ozgul
  • 6,286
  • 1
  • 11
  • 24
  • Is your goal just to "copy" an existing file? If so you might simply use the [`File.Copy`](https://docs.microsoft.com/en-us/dotnet/api/system.io.file.copy?view=netframework-4.8) method instead of building it on your own. – Markus Safar Mar 21 '20 at 11:26
  • You are writing all zeroes to the file since you are only doing new byte[totalbyte]. Before close do a stm.Flush(); – jdweng Mar 21 '20 at 11:46
  • @jdweng can you tell me what is the proper way to do it? –  Mar 21 '20 at 13:16
  • Try `System.IO.File.Copy(fullpath, filename);` – Oguz Ozgul Mar 21 '20 at 13:33
  • Even if fixed, your code is trying to copy a file very inefficiently, byte by byte, and also by loading all the file into memory. Please don't do this. Your solution is as simple as one `File.Copy` command. – Oguz Ozgul Mar 21 '20 at 13:37
  • Make byte[] data a static variable at top of code where totalbyte is located. – jdweng Mar 21 '20 at 13:55
  • @Qguz Ozgul I can't use File.Copy() method. Well I am making a file transfer application so I need to read file's content and send it to client so client will write it into file. –  Mar 21 '20 at 14:45
  • Your code does not say so. The easiest yet efficient way of doing this is to open the source file for reading with: `using(FileStream fs = File.Open(...)` and assuming you have an output stream (NetworkStream? since you are transferring this file to a remote client) doing: `fs.CopyTo(destStream);`. You can also specify the buffer size used during copying: `fs.CopyTo(destStream, 4096);` – Oguz Ozgul Mar 22 '20 at 07:12

1 Answers1

0

I figured out what was the problem in my code and I fixed my code.

See my fixed code. See what changes I have done.

    Public static byte[] data; // this variable will be store file's content.

    static void readfile(string path)
    {
        FileStream stm = File.Open(path, FileMode.Open); // open a file using filestream.
        int size = (int)stm.Length; // size of the file.
        data = new byte[size];

        while (size > 0) // loop until file' size is not 0.
        {
            int read = stm.read(data, totalbyte, size); // reading file's data.
            size -= read;
            totalbyte += read;
        }
    }
    static void writefile(string filename)
    {
        FileStream stm = File.Create(filename); // create a new file.
        byte[] bytes = data;

        Console.WriteLine("Writing data into file...");

        stm.Write(data, 0, data.Length); // writing data into created file.

        Console.WriteLine("data has been wrote into file.");
        stm.Close();
    }
  • This code is needlessly complicated and can be reduced to just two lines: `static void ReadFile(string path) { data = File.ReadAllBytes(path); }` and `static void WriteFile(string path) { File.WriteAllBytes(path, data); }`. So these methods aren’t actually needed at all, as they are doing the same as in-built functionality. – ckuri Apr 12 '20 at 09:41