writing to a file

This is a discussion on writing to a file within the C Programming forums, part of the General Programming Boards category; Hi I need help on this small exercise I'm doing, I just cant get the concept on who lseek works.. ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    Question writing to a file

    Hi I need help on this small exercise I'm doing, I just cant get the concept on who lseek works..
    To start off I have a file called "scoreboard" with 5 zeros in it, now i am trying to enter some of the scores for each player and then write it onto a file to keep updating, but i really can get my head around it..


    Sorry, its true I have not explained myself well enough, well basically.
    Quote:
    The file contains intergers

    , i have already check this, and it's fine.. now this program I am trying to do basically just gets the scores from the players which is 0 - 5 players.. hence the 5 zeros to start off and you will be able to update this scores by prompting the user to enter the scores..and once the user has entered this scores then depending on which player's score it is, it will add to the player score.

    Quote:
    The 1st 0 is player1.. 2nd 0 is player2.. etc

    I know this is where lseek comes into place, so that when the player wants to update for example the 5th player then it will skip the previous 4 players.

    To make it more clear, cause sometimes even i get comfused..


    Quote:
    open file --> read scores --> ask user for scores 1-5 --> allocate each player a score by adding to the current one..



    Code:
     //Write Score
     /* open the scoreboard file in read mode */
    
    
    printf("Player 1 score: ");
    scanf("%d", &p1);
    printf("Player 2 score: ");
    scanf("%d", &p2);
    printf("Player 3 score: ");
    scanf("%d", &p3);
    printf("Player 4 score: ");
    scanf("%d", &p4);
    printf("Player 5 score: ");
    scanf("%d", &p5);
    
    
    fd = open("scoreboard", O_RDWR);
    if (fd<0) {
    	perror("cannot open scoreboard file");
    	exit(2);
    }
    
    lseek(fd, numPlayers*sizeof(int), SEEK_SET);
    
    read(fd, &score, sizeof(int));
    
    if(totalScore == 250)
    {
    	printf("You are the Winner!!!");
    }
    else if(totalScore > 250)
    {
    	printf("Tough luck you need..");
    }
    
    else if(totalScore < 250)
    {
    	lseek(fd, numPlayers*sizeof(int), SEEK_SET);
    
    	/* now write the incremented value back to the file */
    	write(fd, &score, sizeof(int));
    }
    
    	/* done, close the file */
    	close(fd);
    Can you show me how each one will be edited and stored please...
    Last edited by code_guru; 11-27-2005 at 03:42 PM.

  2. #2
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Why not simply read the five scores from the file into memory, preform calculations, and overwrite the file with the new scores? of course, if the score for player X hasn't changed, you'll be overwriting with the same number, but I doubt you'll lose anything by doing so.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21