Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

bitmap - How to get cursor to the begining while reading a file in C

I'm trying to read a bitmap file and and want to read the same line multiple times. But using fseek function can't get the cursor to the beginning of the file every time I finished reading the scanline. Here is my code;

// iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        // iterate over pixels in scanline
        for (int j = 0; j < bi.biWidth; j++)
        {
            int cursor;
            for(int a = 0; a < n; a++)
            {
                // temporary storage
                RGBTRIPLE triple;
    
                // read RGB triple from infile
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
    
                // write RGB triple to outfile
                for(int k = 0; k < n; k++)
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
                fseek(inptr, padding_in, SEEK_SET);
                cursor = ftell(inptr);
            }
        }
    
    
        // skip over padding, if any
        fseek(inptr, padding_in, SEEK_CUR);
    
        // then add it back (to demonstrate how)
        for (int k = 0; k < padding_ou; k++)
        {
            fputc(0x00, outptr);
        }
    }

cursor variable somehow gets the value of 3. I couldn't figure out why. Shouldn't it be 0 since I used SEEK_SET in fseek()

question from:https://stackoverflow.com/questions/66065925/how-to-get-cursor-to-the-begining-while-reading-a-file-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...