Write to file

Programming, for all ages and all languages.
Post Reply
fooforbar
Posts: 1
Joined: Sat Nov 17, 2018 3:24 pm
Libera.chat IRC: 880961793

Write to file

Post by fooforbar »

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
alexfru
Member
Member
Posts: 1111
Joined: Tue Mar 04, 2014 5:27 am

Re: Write to file

Post by alexfru »

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?
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: Write to file

Post by Schol-R-LEA »

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.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
StudlyCaps
Member
Member
Posts: 232
Joined: Mon Jul 25, 2016 6:54 pm
Location: Adelaide, Australia

Re: Write to file

Post by StudlyCaps »

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.
User avatar
saltlamp
Member
Member
Posts: 50
Joined: Tue Dec 11, 2018 3:13 pm

Re: Write to file

Post by saltlamp »

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.
nullplan
Member
Member
Posts: 1770
Joined: Wed Aug 30, 2017 8:24 am

Re: Write to file

Post by nullplan »

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.
Carpe diem!
Post Reply