Thread: How to write to files ?

  1. #1
    Akilla
    Guest

    Question How to write to files ?

    I wrote a really cool program that prints the calendar
    of any given year on the screen.. (using printf() )

    Now, I want to print this thing on a file (basically a TXT)..
    how do I do this ?

    I don't have to change most of my code to do this, do I ?

    www.akilla.tk

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    43
    to write to files:

    #include <fstream>

    ofstream outfile("myfile.txt");

    then use outfile<< type anything that you need to print to the file myfile.txt

    outfile.close();

  3. #3
    Akilla
    Guest

    Question How do I do this in C ?

    How do I do this in C ?

    www.akilla.tk

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    43
    this is what I had used before in a program:

    #include <stdio.h>

    FILE *fp;
    FILE *fopen(char *name, char *mode);

    fp is a pointer to a FILE and fopen returns a pointer to FILE.

    in your program :

    fp = fopen (name, mode);

    name is a character string contains the name of the file(or the one you are creating), mode can be read"r" "a" for append, or "w" write, whihc I'll assume is what you want as you want ot write to a file.

    the website below gives a really good example of writing and reading from files, in C:

    http://heather.cs.ucdavis.edu/~matlo...ge/FileRW.html

    hope this helps!

  5. #5
    Unregistered
    Guest
    here's another reference to a pretty good C tutorial that has a section on file manipulation.

    http://home.twcny.rr.com/amantoan/cweb/fileio.htm

    after declare a file pointer, associating a file witht the pointer, and declaring mode to open file in, you write to file using the fprintf() function. Basically, any data written to screen with printf() can be written to file with fprintf().

  6. #6
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Writing to a file is really a useless option.
    This will do the same:

    program > outputfile

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    how is writing to a file a useless option? You are talking about DOS redirection of standard output with the program > outputfile. So what you are suggesting is that no program should ever write to a file outside of DOS redirection? I hope you are joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The problem read and write in files
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 07-05-2002, 12:54 PM
  2. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 07:53 PM
  3. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM
  4. Can I not call fstream to write to two files sequentially?
    By tigeress in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2002, 01:26 PM
  5. Write and use data stored in data files...
    By alex6852 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 01:45 PM