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

Wednesday, November 2, 2022

[FIXED] Why filestream.close not available

 November 02, 2022     fclose, file, filestream, fopen, vala     No comments   

Issue

I see following code example on this Vala documentation page:

public static int main (string[] args) {
    // Opens "foo.txt" for reading ("r")
    FileStream stream = FileStream.open ("foo.txt", "r");
    assert (stream != null);

    // buffered:
    char buf[100];
    while (stream.gets (buf) != null) {
        print ((string) buf);
    }

    return 0;
}

However, I cannot find a close() function. I want to open file once for reading and later again for writing. Is it safe to do so without a close in between?

(I do not want to use a+ etc mode which permit both reading and writing as both may not be needed while running the application.)


Solution

There are two key items at play:

  1. The FileStream class is a binding to standard C library functions (e.g. open for fopen, read for fread, etc.). (See: this Stack Overflow answer for a good overview of various file APIs)
  2. Vala does automatic reference counting and will free objects for you (See: Vala's Memory Management Explained).

Now if we look at the definition for the FileStream Vala binding, we see:

[ CCode ( cname = "FILE" , free_function = "fclose" ) ]
public class FileStream

Notice the free_function = "fclose" part. This means that when it comes time for Vala to free a FileStream object, it will implicitly call fclose. So no need to attempt this manually. (Also see: Writing VAPI files under the Defining Classes section for details on free_function)

What this means for you is that once your stream object goes out of scope, reference count hits 0, etc. it will get cleaned up for you as you would expect with any other object. You can safely open the file for reading again later by using FileStream.open and getting a new FileStream object.



Answered By - avojak
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, May 12, 2022

[FIXED] When using Filestream Filemode.Append does it overwrite what is lying next to the file?

 May 12, 2022     append, c#, filestream, overflow, overwrite     No comments   

Issue

Lets assume that exactly 1 byte after the File-1-EOF another file (file2) starts.

If I open up file 1 and use FileStream Filemode.Append, does it overwrite file2 or does it make another copy at a place where there is enough memory?

Thanks, in regards!

Edit: For everyone after me: I forgot that you have a file system, which is split into chunks. Making this question nonsense!


Solution

You appear to be laboring under the misapprehension that files are stored sequentially on disk, and that extending one file might overwrite parts of another file. This doesn't happen when you go via a filestream append in c#. The operating system will write the bytes you add however it likes, wherever it likes (and it likes to not overwrite other files) which is how files end up broken into smaller chunks (and why defragging is thing) scattered all over the disk. None of this is of any concern to you, because the OS presents those scattered file fragments as a single contiguous stream of bytes to any program that wants to read them

Of course, if you wrote a program that bypassed the OS and performed low level disk access, located the end of the file and then blindly write more bytes into the locations after it then you would end up damaging other files, and even the OS's carefully curated filesystem .. but a .net file stream won't make that possible

TLDR; add your bytes and don't worry about it. Keeping the filesystem in order is not your job



Answered By - Caius Jard
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

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