OSDev.org

The Place to Start for Operating System Developers
It is currently Mon Feb 18, 2019 6:10 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Write to file
PostPosted: Sat Nov 17, 2018 3:27 pm 
Offline

Joined: Sat Nov 17, 2018 3:24 pm
Posts: 1
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


Top
 Profile  
 
 Post subject: Re: Write to file
PostPosted: Sat Nov 17, 2018 6:25 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 932
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?


Top
 Profile  
 
 Post subject: Re: Write to file
PostPosted: Sat Nov 17, 2018 7:04 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1424
Location: Athens, GA, USA
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
μή εἶναι βασιλικήν ἀτραπόν ἐπί γεωμετρίαν
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.


Top
 Profile  
 
 Post subject: Re: Write to file
PostPosted: Sun Nov 18, 2018 10:16 pm 
Offline
Member
Member

Joined: Mon Jul 25, 2016 6:54 pm
Posts: 161
Location: Adelaide, Australia
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.


Top
 Profile  
 
 Post subject: Re: Write to file
PostPosted: Sat Jan 12, 2019 10:49 pm 
Offline
User avatar

Joined: Tue Dec 11, 2018 3:13 pm
Posts: 22
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.


Top
 Profile  
 
 Post subject: Re: Write to file
PostPosted: Sun Jan 13, 2019 12:40 am 
Offline
Member
Member

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google Feedfetcher and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group