• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How do I make a FIFO buffer for files in C?

Onceler

Golden Member
I need to read in 9 and just 9 bytes at a time from a very large file and increment the file position to read in the next 9 bytes after I have done calculations on the first 9
If my calculations don't use up the entire 9 then keep the leftover in the buffer and read in more subtracted by whatever is in buffer like say I read in 9 but only the first 5 match my calculations then I would have four left over and I want them to go to the front of the buffer and read in five bytes to the end of the buffer so that buffer is always exactly nine bytes the leftovers at the beginning and the read in bytes appended to the leftovers.
How would I be able to do this?
This is some of the code I found on google with a few modifications:
Code:
unsigned long long int fd;
char buffer[9] = {0};
char * myfifo = "/tmp/myfifo";    

mkfifo(myfifo, 0666);/*what does this mean?\*
printf("What would you like to send?\n");
fgets(buffer, 9, stdin);

if((fd = open(myfifo, O_WRONLY)) < 0) what does O_WRONGLY mean and how do I get it to compile my compiler says error O_WRONGLY undeclared
    printf("Couldn't open the FIFO for writing!\n");
else {
    write(fd, buffer, strlen(buffer));
    close(fd);
I am a beginner.
Thank you
 
Last edited:
O_WRONLY means open as write only and the 666 means all users can read and write, but can't execute. mkfifo creates a pipe with those permissions. Are you using a Unix OS variant? If not, that code won't help.

You could use a circular buffer as Ken g6 recommended.

Code:
while file is not at end
{
if(buffer size < 9)
read how many bytes you need and add to buffer

take whatever bytes you want out and do work
}

Also, your initial description really needs some punctuation. It's quite hard to read.
 
Uh keep tabs on how many bytes into your file and use fseek to move your file read point and fgets to read in exactly what you need.
 
no I am using Code::Blocks GCC windows 7

Well regardless of what you use, you need some type of data structure to hold your data. Ken g6's link had a C implementation of a circular buffer that would work great for a FIFO. The pseudo-code I provided shows how you would use the buffer once you have implemented it.
 
If you understand what an array is and how pointers work, then you should be able to read up on circular queues and implement one to do what you need. If you're having trouble, your problem is a lack of fundamental knowledge about programming and is not particular to circular queues.

(not trying to be a jerk, just trying to point out that this is a fairly simple problem if you understand the ideas behind it)
 
fgets() definitely does not do what the OP defines as goal. fread could. Your environment either has documentation for things like 'fgets' or you can websearch for "man fgets", "man fread", "man open O_WRONLY".


It does sound like the data contains 1-9 byte entries (for example: points, lines, spheres ... or assembly language). Each entry is processed when it is found. Each entry must have type information its first bytes or else one would not know how many bytes belong to it. When processing an entry one thus knows how many bytes are used.
Code:
// returns size of processed entry
size_t process( void * array );
...
size_t next = process( array );
copy array[next]..array[MAX-1] to array[0]..array[MAX-1-next]
size_t got = fread( &array[MAX-next], 1, next, file );
if ( got < next ) ... // ferror or feof
...
Simple, pseudo-code beginner implementation. Not a pretty circularity. No scary "OOP crap". No meat on the bones either, so copy-paste will fail.


Disclaimer: The index math has not been checked with device Brain.
 
fgets() definitely does not do what the OP defines as goal. fread could. Your environment either has documentation for things like 'fgets' or you can websearch for "man fgets", "man fread", "man open O_WRONLY".


It does sound like the data contains 1-9 byte entries (for example: points, lines, spheres ... or assembly language). Each entry is processed when it is found. Each entry must have type information its first bytes or else one would not know how many bytes belong to it. When processing an entry one thus knows how many bytes are used.
Code:
// returns size of processed entry
size_t process( void * array );
...
size_t next = process( array );
copy array[next]..array[MAX-1] to array[0]..array[MAX-1-next]
size_t got = fread( &array[MAX-next], 1, next, file );
if ( got < next ) ... // ferror or feof
...
Simple, pseudo-code beginner implementation. Not a pretty circularity. No scary "OOP crap". No meat on the bones either, so copy-paste will fail.


Disclaimer: The index math has not been checked with device Brain.

Yah my bad. Fread is correct . You could us fgets with a max size and cast I guess. I haven't programmed c in a couple years..... kinda miss it haha
 
You keep track of which indices are used. The wikipedia article has a pretty good overview of how it works. Assuming you're using C then you'll want a struct to hold the buffer and track its status, probably something like
Code:
typedef struct
{
  int dataStart;
  int dataEnd;
  char data[9];
} CircularQueue;

Then appropriate functions to insert data, remove and so on.
 
Back
Top