File Input and Output, simple.

This is a discussion on File Input and Output, simple. within the C Programming forums, part of the General Programming Boards category; Hi, I want to know how to make a simple action in C. Open a file and then write in ...

  1. #1
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    File Input and Output, simple.

    Hi, I want to know how to make a simple action in C.
    Open a file and then write in the same line a simple thing:

    first a char[30] thats means the name of a player and after 4 spaces or better a \t I need to write his scores.

    How should I make this?
    I tried to make a simple thing, but didnt worked.

    Here is the code, my problems is writing the spaces and the score.. the name I writed perfectly

    Code:
    void saverecord(void)
    {
       FILE *fp; //The file...
       
       //Open the file!
       if((fp = fopen("scores.dat","a")) == NULL)
       {  
          printf("Error! Cannot open the file!\n");
          msleep(1); //Delay...
          exit(1); //Exit...
       }
       
       //Write #HERE I NEED HELP#
       fwrite(name, sizeof(name),1,fp);
       //Now I havent any idea how to make the spaces and to 
       //output the number two...
       
        //Close the file
        fclose(fp);
         
        
    }
    Thanks for helping!

  2. #2
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Is there a library we should be including other than stdio.h?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by AMDPwred
    Is there a library we should be including other than stdio.h?
    That depends on what you want to do. For simple file IO, no.

    For the original poster, there is no 'name' defined in your code. If you're using fwrite, you should be opening in binary mode rather than text.

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

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    I think you didnt understood what I want :)

    I'll try to explain again.
    I need to know how to write in a file something like this:

    Joshua 100 Points

    Now I got the 2 variables:
    Name (char) and points (int)

    I need to add on the file:
    Name\tPoints

    How do I make this, I know that I need to use fwrite, but I just know how to add the name not the \t and points

    Thanks!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: I think you didnt understood what I want :)

    Originally posted by Vber
    I'll try to explain again.
    I need to know how to write in a file something like this:

    Joshua 100 Points

    Now I got the 2 variables:
    Name (char) and points (int)

    I need to add on the file:
    Name\tPoints

    How do I make this, I know that I need to use fwrite, but I just know how to add the name not the \t and points

    Thanks!
    Try actually reading this thread. Asked and answered. To quote Salem:

    fprintf( fp, "%s\t%d\n", name, score );
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Ahh Ok :)

    Really sorry, I didnt knew that fprintf write in the file.
    Sorry again, and thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  4. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 04:28 PM

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