PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label readfile. Show all posts
Showing posts with label readfile. Show all posts

Sunday, October 30, 2022

[FIXED] How to read binary files until EOF in C#

 October 30, 2022     c#, eof, readfile     No comments   

Issue

I have a function to write some data into a binary file

private void writeToBinFile (List<MyClass> myObjList, string filePath)
{
    FileStream fs = new FileStream(filePath, FileMode.Create);
    BinaryWriter bw = new BinaryWriter(fs);

    foreach (MyClass myObj in myObjList)
    {
        bw.Write(JsonConvert.SerializeObject(myObj));
    }
    bw.Close();
    fs.Close();

}

I am looking something like

FileStream fs = new FileStream(filePath, FileMode.Create);
BinaryReader bw = new BinaryReader(fs);

while (!filePath.EOF)
{
    List<MyClass> myObjList = br.Read(myFile);
}

anyone can help with this? thanks in advance


Solution

JSON can be saved with no formatting (no new lines), so you can save 1 record per row of a file. Thus, my suggested solution is to ignore binary files and instead use a regular StreamWriter:

private void WriteToFile(List<MyClass> myObjList, string filePath)
{
    using (StreamWriter sw = File.CreateText(filePath))
    {
        foreach (MyClass myObj in myObjList)
        {
            sw.Write(JsonConvert.SerializeObject(myObj, Newtonsoft.Json.Formatting.None));
        }
    }
}

private List<MyClass> ReadFromFile(string filePath)
{
    List<MyClass> myObjList = new List<MyClass>();
    using (StreamReader sr = File.OpenText(filePath))
    {
        string line = null;
        while ((line = sr.ReadLine()) != null)
        {
            myObjList.Add(JsonConvert.DeserializeObject<MyClass>(line));
        }
    }
    return myObjList;
}

If you really want to use the binary writer to save JSON, you could change it to be like this:

private void WriteToBinFile(List<MyClass> myObjList, string filePath)
{
    using (FileStream fs = new FileStream(filePath, FileMode.Create))
    using (BinaryWriter bw = new BinaryWriter(fs))
    {
        foreach (MyClass myObj in myObjList)
        {
            bw.Write(JsonConvert.SerializeObject(myObj));
        }
    }
}

private List<MyClass> ReadFromBinFile(string filePath)
{
    List<MyClass> myObjList = new List<MyClass>();
    using (FileStream fs = new FileStream(filePath, FileAccess.Read))
    using (BinaryReader br = new BinaryReader(fs))
    {
        while (fs.Length != fs.Position) // This will throw an exception for non-seekable streams (stream.CanSeek == false), but filestreams are seekable so it's OK here
        {
            myObjList.Add(JsonConvert.DeserializeObject<MyClass>(br.ReadString()));
        }
    }
    return myObjList;
}

Notes:

  • I've added using around your stream instantiations so that the files are properly closed when memory is freed
  • To check the stream is at the end, you have to compare Length to Position.


Answered By - ProgrammingLlama
Answer Checked By - Mildred Charles (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, March 8, 2022

[FIXED] How to download xml file in yii

 March 08, 2022     header, php, readfile, xml, yii     No comments   

Issue

I want to create and download xml file in yii so i have write code to create xml file and it's working fine but it is giving error while downloading xml file
i have tried code

//xml structure
$xmldata = '<?xml version="1.0" encoding="utf-8"?>';
$xmldata .= '<MemberBill>';
foreach ($model as $model)
{
    $xmldata .= '<Bill>';
    $xmldata .= '<BillNo>'.$model->bill_no.'</BillNo>';
    $xmldata .= '</Bill>';
}
$xmldata .= '</MemberBill>';

if(file_put_contents('memberBill.xml',$xmldata)) // this code is working fine xml get created
{
    //echo "file created";exit;
    header('Content-type: text/xml');   // i am getting error on this line
    //Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\yii\framework\web\CController.php:793)

    header('Content-Disposition: Attachment; filename="memberBill.xml"');
    // File to download
    readfile('memberBill.xml');        // i am not able to download the same file
}

Solution

In D:\xampp\htdocs\yii\framework\web\CController.php:793 code already start outputting - it's method render. I think you has already rendered something before this code!



Answered By - CreatoR
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing