Thread: file input/output

  1. #1
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38

    file input/output

    i am trying to program a function that registers what card i have drawn. i want specific entries to be put on specific lines (ex. ace of spades on line one, 2 of clubs on line 52). i already have a blank file but i have no idea where to go from there. is there a function that lets me put my output on a specific line?

    also for reference, here is my function for generating a blank file
    Code:
    void createfile()
    {
        list=fopen("cardlist.txt","w");
    
        for(i=0;i<DECKSIZE;i++)
        {
            fprintf(list,"\n");
        }
        fclose(list);
    }
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not in text mode, no. You have one of two options:
    1) Use binary mode, and just seek to the spot you want and write that way. (You'll have to of course create the file so that it contains N bytes of data first, so that you can seek through them all.)

    2) Use text mode, and read and write the entire file whenever you want to change its contents. (Read the file into an array, or whatever, write it all at once.)

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38
    is it possible for me to use /v and /r to move the cursor in fprintf or will that not work? im still a n00b so i dont know how to read a file into an array yet.
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > i am trying to program a function that registers what card i have drawn
    So why not use an array in memory rather than a file?

    Or is this an exercise in using a file as an array?

    Quicky tutorial
    Code:
    // your array, of MYTYPE, which is NUM elements long
    MYTYPE array[NUM];
    
    // open a file in binary mode
    FILE *fp = fopen( "array_on_disk", "r+b");
    
    // write the whole array
    fwrite( array, sizeof(MYTYPE), NUM, fp );
    
    // position at array[pos]
    fseek( fp, pos * sizeof(MYTYPE), SEEK_SET );
    
    // read array[pos] from the file
    fread( &array[pos], sizeof(MYTYPE), 1, fp );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM