OSDev.org
https://forum.osdev.org/

Write to file
https://forum.osdev.org/viewtopic.php?f=13&t=33322
Page 1 of 1

Author:  fooforbar [ Sat Nov 17, 2018 3:27 pm ]
Post subject:  Write to file

I am trying to figure out how to write to a file in c. I could only find how to do this with libraries, and I really, really don't want to spend my time linking (shudder :shock:). Thanks! BTW I'm compiling with GCC. :D

Author:  alexfru [ Sat Nov 17, 2018 6:25 pm ]
Post subject:  Re: Write to file

The question looks suspiciously close to one about how to list a directory without having any FS code (probably, nothing whatsoever).
Same poster? Or do they somehow multiply?

Author:  Schol-R-LEA [ Sat Nov 17, 2018 7:04 pm ]
Post subject:  Re: Write to file

fooforbar wrote:
I am trying to figure out how to write to a file in c. I could only find how to do this with libraries, and I really, really don't want to spend my time linking (shudder :shock:). Thanks! BTW I'm compiling with GCC. :D


I assume you know how to do this with the C Standard I/O library, and just need to bypass it for some reason; otherwise, this is not the right forum for you, and you need to ask the question again in a forum better suited to answering it. If you aren't an experienced programmer already, and well-versed in general application programming, then diving into OS dev at this point is about as sensible as skydiving without any lessons, preparations, or parachute.

The disdain for linking doesn't make sense, though - even with OS dev (maybe especially with OS dev), linking is a basic part of the development process. Indeed, implementing (or at least porting) a linker is a key part of OS dev. A single binary blob isn't going to cut it with any general-purpose OS.

Stipulating that, I we'd also need more details as to what you are trying to accomplish... a lot more. Starting with the platform (presumably PC, but you never know with this group) and OS (and there has to be one, otherwise the concept of 'file' wouldn't exist.

If this is for your own OS, then the answer is, "first, write the disk drivers and the filesystem". You wouldn't be writing to a 'file' at that point, but to raw disk sectors, which aren't at all the same thing. I assume that this would, in fact, be the question you want to answer if you are OS-Dev'ing, but I wouldn't expect it to be posted in "General Programming" in that case.

Otherwise, the answer is either, "with calls out to the file system driver, via whatever system calls and/or IPC the operating system provides". This means either linking to the libraries which exposed the APIs for the OS and the file system, or linking to the libraries with all the C standard I/O functions which are already wrapped around those system calls for your convenience.

Depending on the OS, there may be some way to do it without a library of some sort by invoking the system calls manually (which could mean a lot of different possible mechanisms depending on the OS and platform; just on x86 alone this might mean an INT, a SYSENTER, a non-impl trap, or a call gate, among other things). However, that would a) require some assembly coding, which may or may not work as inline assembly, and b) be entirely dependent on not only the OS, but possibly on the specific version of the OS in question, since low-level mechanisms are actually more subject to change than high-level ones.

So again, we'd need more detail to give a meaningful answer.

Author:  StudlyCaps [ Sun Nov 18, 2018 10:16 pm ]
Post subject:  Re: Write to file

fooforbar wrote:
I am trying to figure out how to write to a file in c. I could only find how to do this with libraries, and I really, really don't want to spend my time linking (shudder :shock:). Thanks! BTW I'm compiling with GCC. :D


I want to ask, what OS is this for, is it your own OS or an already existing one?
If it's your own, then you write a file system driver and a disk driver and you make a function that writes a file onto the disk.
If it's a existing OS, then there is the C standard library, which GCC will automatically link for you, or there will be OS specific libraries. This does require linking.

I would also advise you learn how to link, otherwise you will repeatedly hit this problem any time you want to access new functionality and you'll end up re-inventing a dozen different wheels and awkwardly mashing them together.

Author:  saltlamp [ Sat Jan 12, 2019 10:49 pm ]
Post subject:  Re: Write to file

If you are doing this using a kernel of some sorts, then the kernel (usually...) has system calls for this very thing. If you are doing this off of the raw disk, then just write to some space on a disk, and refer to that space by its beginning and length in bytes, and you can 'write' a file in that way. I highly recommend using a filesystem of some sorts, though. (I am assuming that you are wanting to do this in some conventional way.) If not, then I don't know what to tell ya.

Author:  nullplan [ Sun Jan 13, 2019 12:40 am ]
Post subject:  Re: Write to file

Please don't revive old threads. Especially if you don't know what to tell people. The OP in this case pretty clearly was a troll or someone with absolutely no clue why their question makes no sense here, and in any case, they never answered to the question marks on everyone's faces.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/