Thread: File Positioning stdout

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    36

    File Positioning stdout

    Hi. Noob here just playing around with some things trying to learn. I was wondering if there is a way to print a stationary progress bar onto my console window. I tried ftell() and fseek() but that didn't work.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        long pos[11];
        int i, t;
    
        pos[0] = 0;
        printf("[");
        pos[1] = ftell(stdout);
        /* record current position */
        for (i = 2; i < 10; ++i)
        {
            putchar('-');
            pos[i] = ftell(stdout);
        }
    
        printf("-]");
        /* print progress bar */
        for (i = 1, t = 10; t > 0; ++i, --t)
        {
            fseek(stdout, pos[i], SEEK_SET);
            putchar('=');
        }
    
        return 0;
    }
    Output:
    [T3256@GURY GibletSearch]$ ./temp
    [---------]==========
    I'm trying to overwrite the dashes. [==========].

    Thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can use backspace in a string ('\b'). Ie, backspace, then replace the remaining '-' that you backspaced over.

    Anything more complicated than that and you will need a text console library such as ncurses (any OS) or winAPI.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    Thank you kind sir.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM