Thread: Writing to a TXT

  1. #1
    Akilla
    Guest

    Exclamation Writing to a TXT

    ok guys.. I wrote a calendar program myself..
    Now, I want to print the thing to a TXT file (it should give
    an option Y/N)... when the answer is yes .. I
    want it to print basically everything the program outputs
    on the screen after the clrscr() command..

    what's the easy way to do this..

    here's my code

    PHP Code:

    //Program written by Akilla //
    //www.akilla.tk//

    #include <stdio.h>
    #include <iostream.h>
    #include <conio.h>

    int leapyear(int year)
    {
        if ( (
    year%4==0) && (year%400==0) )
        {
            return 
    1;
        }

        if ( (
    year%4==0) && (year%100!=0) )
        {
            return 
    1;
        }
        
        return 
    0;
    }


    void getweekday(int monthint dayint yearint *week)
    {
        
    int centurycodemonthcode[13], CD;

        
    monthcode[0]=1;
        
    monthcode[1]=1;
        
    monthcode[2]=4;
        
    monthcode[3]=4;
        
    monthcode[4]=0;
        
    monthcode[5]=2;
        
    monthcode[6]=5;
        
    monthcode[7]=0;
        
    monthcode[8]=3;
        
    monthcode[9]=6;
        
    monthcode[10]=1;
        
    monthcode[11]=4;
        
    monthcode[12]=6;

        
    C=year/100;
        
    D=year%100;


        if (
    C==17)
        {
         
    centurycode=4;
        }
        if (
    C==18)
        {
         
    centurycode=2;
        }
        if (
    C==19)
        {
         
    centurycode=0;
        }
        if (
    C==20)
        {
         
    centurycode=6;
        }

        *
    week = (centurycode + (D/D)%+monthcode[month] + day)%7;


    }

    void drawcalendar(int daysint firstday)
    {

        
    int idaythen;
        
    char space ' ';

        if (
    firstday == 0)
        {
            
    firstday 7;
        }

        
    daythen=firstday;
        
    printf("SUN MON TUE WED THU FRI SAT\n");

        for (
    i=1i<firstdayi++)
        {
            
    printf("%3c "space);
        }

        for (
    i=1i<=daysi++)
        {
            
    printf("%3d "i);
            
    daythen++;

            if (
    daythen>&& != days)
            {
                
    daythen=1;
                
    printf("\n");
                
            }

        }
        
        
    printf("\n\n\n");
    }

    void getdays(int yeartypeint monthint *days)
    {
        if (
    yeartype == && month == 2)
        {
            *
    days 28;
        }
        
        if (
    yeartype == && month == 2)
        {
            *
    days 29;
        }
        
        if ( (
    month==4) || (month==6) || (month==9) || (month==11) )
        {
            *
    days 30;
        }
        
        if ( (
    month==1) || (month==3) || (month==5) || (month == 7) || (month==8) || (month==10) || (month==12) )
        {
            *
    days 31;
        }
    }

    main()
    {
        
    int monthyeardaysfirstdayyeartype;
        
        do
        {
            
    cout << "Enter the year: ";
            
    cin >> year;
        } while (
    year<1700 || year>2099);

        
    clrscr();
        
        
    yeartype leapyear(year);

        for (
    month =1month <=12month++)
        {
            
    getweekday(month1year, &firstday);
            
    getdays(yeartypemonth, &days);

            if (
    month == 1)
            {
                
    cout << "January " << year << endl << endl;
            }
            else if (
    month ==2)
            {
                
    cout << "February " << year << endl << endl;
            }
            else if (
    month ==3)
            {
                
    cout << "March " << year << endl << endl;
            }
            else if (
    month ==4)
            {
                
    cout << "April " << year << endl << endl;
            }
            else if (
    month ==5)
            {
                
    cout << "May " << year << endl << endl;
            }
            else if (
    month ==6)
            {
                
    cout << "June " << year << endl << endl;
            }
            else if (
    month ==7)
            {
                
    cout << "July " << year << endl << endl;
            }
            else if (
    month ==8)
            {
                
    cout << "August " << year << endl << endl;
            }
            else if (
    month ==9)
            {
                
    cout << "September " << year << endl << endl;
            }
            else if (
    month ==10)
            {
                
    cout << "October " << year << endl << endl;
            }
            else if (
    month ==11)
            {
                
    cout << "November " << year << endl << endl;
            }
            else if (
    month ==12)
            {
                
    cout << "December " << year << endl << endl;
            }
            
    drawcalendar(daysfirstday);
        }

    err... and please don't steal my code :-)

    COOL PROGRAMS @ www.akilla.tk

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    You'll have to include fstream.h whenever you use files.

    heres a simple program to do something like what you want..

    Code:
    #include<fstream.h>
    int main()
    {
    char name[21],ans;
    ofstream fout; 
    
    cout<<"ENTER NAME : ":
    cin.getline(name,20,'\n');
    
    cout<<"\n\nYOU ENTERED : "<<name<<"\n\n";
    
    cout<<"DO YOU WANT TO WRITE THIS TO FILE [y/n] : ";
    cin>>ans;
    
    if(ans=='y' || ans=='Y')
    {
     fout.open("MYFILE.TXT");
     fout<<"NAME : "<<name<<"\n";
     fout.close();
    
     cout<<"\n\nFINISHED WRITING\n";
    }
    
    return 0;
    }

    modify this to your needs.
    -

  3. #3
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    I suggest: use the fstream, and use BINARY MODE... (if you are unfamilar with the fstream, as i believe u are, maybe you want to use text mode).

    at that time format your output into the file with the variables you wish.
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  4. #4
    Akilla
    Guest

    help

    what's binary mode ? can you show me an example ?


    www.akilla.tk

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    106
    binary mode is that


    #include <fstream>
    using namespace std;

    int main()
    {
    fstream File("test.txt",ios:ut,ios::in,ios::binary);

    char ch;
    ch='m';

    File.put(ch);

    File.close();

    exit(0);
    }

  6. #6
    Unregistered
    Guest
    I lost the code, please use the code option and put it in again..

    also, what exactly is binary mode ?

    is there a way i can printf it to the screen and to the file without
    a whole lot of extra coding ? (please see my code above)

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    In your code you are mixing cout's and printf()'s.
    One option is to use fprintf(). Just open the file using:
    Code:
       FILE *fp;
       fp = fopen("calendar.txt","w");
    Then change all cout's and printf()'s to fprintf()'s:
    fprintf(fp,"January %d\n\n",year);

    Or you could use the C++ style fstream.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And you would pass the file pointer to any functions which write to the file:

    void drawcalendar(FILE *fp, int days, int firstday)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  2. Very slow file writing of 'fwrite' function in C
    By scho in forum C Programming
    Replies: 6
    Last Post: 08-03-2006, 02:16 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  5. Writing "Enter" character to a txt file
    By Hankyaku in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2003, 11:06 AM