-
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 month, int day, int year, int *week)
{
int centurycode, monthcode[13], C, D;
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/4 + D)%7 +monthcode[month] + day)%7;
}
void drawcalendar(int days, int firstday)
{
int i, daythen;
char space = ' ';
if (firstday == 0)
{
firstday = 7;
}
daythen=firstday;
printf("SUN MON TUE WED THU FRI SAT\n");
for (i=1; i<firstday; i++)
{
printf("%3c ", space);
}
for (i=1; i<=days; i++)
{
printf("%3d ", i);
daythen++;
if (daythen>7 && i != days)
{
daythen=1;
printf("\n");
}
}
printf("\n\n\n");
}
void getdays(int yeartype, int month, int *days)
{
if (yeartype == 0 && month == 2)
{
*days = 28;
}
if (yeartype == 1 && 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 month, year, days, firstday, yeartype;
do
{
cout << "Enter the year: ";
cin >> year;
} while (year<1700 || year>2099);
clrscr();
yeartype = leapyear(year);
for (month =1; month <=12; month++)
{
getweekday(month, 1, year, &firstday);
getdays(yeartype, month, &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(days, firstday);
}
}
err... and please don't steal my code :-)
COOL PROGRAMS @ www.akilla.tk
-
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.
-
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.
-
help
what's binary mode ? can you show me an example ?
www.akilla.tk
-
binary mode is that
#include <fstream>
using namespace std;
int main()
{
fstream File("test.txt",ios::out,ios::in,ios::binary);
char ch;
ch='m';
File.put(ch);
File.close();
exit(0);
}
-
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)
-
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.
-
And you would pass the file pointer to any functions which write to the file:
void drawcalendar(FILE *fp, int days, int firstday)