Two C Questions Help?

This is a discussion on Two C Questions Help? within the C Programming forums, part of the General Programming Boards category; How do you populate a file with with five zeros going vertically? and How do you print out the file's ...

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

    Two C Questions Help?

    How do you populate a file with with five zeros going vertically? and

    How do you print out the file's inode number and size in bytes one the above has been created?

    This is the first stage of a darts program. The first file would contain zero's 5 of them becasue there are 5 players. The thing is, I don't know how to do it. below is my tried attempt sort of:

    for (i=5; i<scores; i++)
    write(fd, &scores, sizeof (int));

    also how do i do the i node number i have no idea about this?

    the rest of my coding which so far does, accepts the filename from command line and create the file with read and write for owner.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<errno.h>
    
    int main (int argc, char *argv[])
    
         int scores;
    
         if (argc !=2) {
           printf("Usage: %s scores\n", argv[0]);
           exit(1);
    
           fd = open("scorefile", 0_RDWR | 0_CREAT, 0600);
           if (fd<0) {
    	 perror("cannot open file score");
    	 exit(2);

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by The_Lone_Wolf
    How do you populate a file with with five zeros going vertically? and

    How do you print out the file's inode number and size in bytes one the above has been created?

    for (i=5; i<scores; i++)
    write(fd, &scores, sizeof (int));
    By using the write function (and open) below, you are actually not using standard C functions, though they will be widely implemented.

    Anyway, your code has issues:

    - if you want to do 5 zeroes, why are you trying to write the value of "scores"?
    - Why is it in the for loop? Try:
    Code:
    for (i = 0; i < 5; i++)
    - I assume you want to create a text file, and have the scores human readable, in which case you could either use fprintf, or use write() to send a string that's been built with something like snprintf().


    also how do i do the i node number i have no idea about this?
    Standard C has no concept of inodes. Look up the posix functions stat and fstat.


    Code:
    #include<stdio.h>
    #include<string.h>
    #include<errno.h>
    
    int main (int argc, char *argv[])
    
         int scores;
    
         if (argc !=2) {
           printf("Usage: %s scores\n", argv[0]);
           exit(1);
    
           fd = open("scorefile", 0_RDWR | 0_CREAT, 0600);
           if (fd<0) {
    	 perror("cannot open file score");
    	 exit(2);
    All your code here is within the if (argc != 2) block, so if there is not 1 parameter, it will print the usage and exit. If there is one parameter, it will skip the whole code you pasted and go onto whatever else there is.

  3. #3
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    As for doing it vertically, files have no concept of "lines", they have linebreak characters. Thus, if I understand you correctly, you would want to print a linebreak for every zero then.
    Last edited by Silfer; 11-25-2005 at 07:02 AM.
    -S

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

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