Question

I'm extremelly new to System Calls. I think I can use Index but not quite sure how to implemented. Let's say I want to look up on a file with data for every ":" and '>"

for example I have a myfile.txt with the following:

hello: this is a testing for (1=zero i>10); this line should be written to the two files because it contains both the : and the >

so I'm gonna open two files for writing and one for reading (myfile.txt) and when i find a line containing ":" I'll write it to the colon.txt file. When the line contains ">" character I'll write it to the greaterthen.txt file.

more comments: Yes I need to implement buffer.

Was it helpful?

Solution

Sorry, I misread the question.

You're going to need to consider several things. Yes, you need a buffer but you also have handle if your line is larger than your buffer. So you need two buffers, one to read into and another to assemble the whole line. While possible to do in one buffer it seemed overly complicated.

My C is pretty rusty but this seems to work. It needs error handling and I'm sure it doesn't match your coding style, but my once & done test seemed to work. The read buffer is abnormally small to demonstrate the condition where a line is larger than the buffer -- in the real world your read buffer would be much larger.

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>

int findchar(char*, int, char);

int main()
{
    size_t dataIn = open("myfile.txt", O_RDONLY);
    size_t gtOut = open("greaterthan.txt", O_CREAT | O_WRONLY);
    size_t coOut = open("colon.txt", O_CREAT | O_WRONLY);

    char buffer[12];
    int iReadCnt;
    char* lineBuffer = NULL;
    int lineBufferSize = 0;

    // read input file until no more input
    while (iReadCnt = read(dataIn, buffer, sizeof(buffer)))
    {
        int x;
        int n;
        char* b;

        // add buffer to dynamic line buffer
        n = lineBufferSize + iReadCnt;
        b = malloc(n);
        memcpy(b, lineBuffer, lineBufferSize);
        memcpy(&b[lineBufferSize], buffer, iReadCnt);
        // free old buffer if exists
        if (lineBuffer)
        {
            free(lineBuffer);
        }

        lineBufferSize = n;
        lineBuffer = b;

        // look for end of line
        x = findchar(lineBuffer, lineBufferSize, '\n');

        if (x >= 0)
        {
            int gtPos;
            int coPos;
            int n;
            char* b;

            // look for gt
            gtPos = findchar(lineBuffer, x, '>');

            if (gtPos >= 0)
            {
                write(gtOut, lineBuffer, x + 1);
            }

            // look for colon
            coPos = findchar(lineBuffer, x, ':');

            if (coPos >= 0)
            {
                write(coOut, lineBuffer, x + 1);
            }

            // remove line from buffer
            n = lineBufferSize - (x + 1);
            b = malloc(n);
            memcpy(b, &lineBuffer[x + 1], n);
            free(lineBuffer);
            lineBufferSize = n;
            lineBuffer = b;
        }
    }

    // close files
    close(dataIn);
    close(gtOut);
    close(coOut);
    exit(0);
}

// simple function to find a character in a buffer
int findchar(char* buffer, int len, char c)
{
    int i;
    for (i = 0; i < len; ++i)
    {
        if (buffer[i] == c)
        {
            return i;
        }
    }

    return -1;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top