Thread: Need help on writing file

  1. #1
    C programming
    Join Date
    Jul 2005
    Location
    Singapore
    Posts
    2

    Need help on writing file

    I have some problem regarding to my project. I need to create a program using C programming and in the program it will be able to create another new file to store all my data. My output will prompt the user to key in the information. After that the program will automatically create a new file which containes all the data input by the user. I having difficultly in writing to a file to store the data. Can you give me some idea to program it??
    Thank you for your help!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you know about
    Code:
    printf( "hello world\n" );
    Realise that
    Code:
    fprintf( stdout, "hello world\n" );
    is exactly the same thing.

    Now that you know you can use files, try
    Code:
    FILE *fp = fopen( "out.txt", "w" );
    fprintf( fp, "hello world\n" );
    fclose( fp );
    The rest is simply a matter of choosing what to print
    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.

  3. #3
    C programming
    Join Date
    Jul 2005
    Location
    Singapore
    Posts
    2

    Need help!!!!

    hi really thanks for your help.....i already can create a file using your method...but now i having difficulty in saving the data. I need to ask the user to key in the data.....after the user key in the data....i will ask the user whether he/she want to continue or not. If the user want to continue, i will use loop function to display the sentence to prompt the user to key in the data again...then i will ask again he/she wish to continue or not...if the answer is no.....then i will display all the data that the user key in in the file that i had created.....now i can program the loop function.....but the problem is i can't print out all the data....only the last data that the user key in is being display. Can you give me some example in order to program it??......Thanks~!!!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    All input is going to the same file, right? Just open and close the file outside of your loop:
    Code:
    FILE *out = fopen ( filename, "w" );
    int done = 0;
    
    while ( !done ) {
      char buffer[BUFSIZ];
    
      printf ( "Enter input: " );
      fflush ( stdout );
    
      if ( fgets ( buffer, sizeof buffer, stdin ) == NULL )
        break;
    
      fputs ( buffer, out );
    
      printf ( "Continue? (y/n): " );
      fflush ( stdout );
    
      if ( getchar() != 'y' )
        done = 1;
    }
    
    fclose ( out );
    My best code is written with the delete key.

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, 12: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, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM