Connecting Tech Pros Worldwide Help | Site Map

how to write a "long" value to a text file?

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: Oct 17 '09
I want to write that "mjd1" value to a text file. I tried it using fprinf() function. but it dint work.
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<dos.h>
  4. #include<stdio.h>
  5. void main()
  6. {
  7.    clrscr();
  8.    FILE *TempFile; 
  9.    int year,day,month,c=1,c1=0;
  10.    char *temp="buf.txt";
  11.    struct date d;
  12.    long double mjd=54832,mjd1,count=0;
  13.    cout<<"\nDay";
  14.    cin>>day;
  15.    cout<<"\nmonth";
  16.    cin>>month;
  17.    cout<<"\nyear";
  18.    cin>>year;
  19.    if(year!=2009)
  20.    {
  21.     for(int j=2009;j<year;j++)
  22.     {   count+=365;
  23.         if(j%4==0)
  24.         c1++;
  25.         cout<<"c1"<<c1;
  26.     }
  27.     }
  28.    if(year%4==0)
  29.    c+=2;
  30.    else c+=3;
  31.    cout<<"\nc2"<<c;
  32.    for(int i=1;i<month;i++)
  33.  
  34.        { if(i==11) c++;
  35.      if(i==4)  c++;
  36.      if(i==6)   c++;
  37.      if(i==9)   c++;
  38.  
  39.        count+=31;
  40.      }
  41.    cout<<"\n Count"<<count;
  42.    cout<<endl;
  43.    mjd1= count + day + mjd - c + c1;
  44.    cout<<"\nModified Julian Date:"<<mjd1;
  45.   TempFile=fopen(temp,"w+");
  46.   fprintf(TempFile,"Modified Julian date%s\t\n",mjd1);
  47.   fclose(TempFile);
  48.   getch();
  49.  
  50. }
  51.  
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#2: Oct 17 '09

re: how to write a "long" value to a text file?


mjd1 is declared as a long double. Are you sure you didn't want it to be a long int? Review the man page for fprintf. The %s conversion specifier is only for use with strings. You need something different for long doubles; and something else again for long ints.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,158
#3: Oct 17 '09

re: how to write a "long" value to a text file?


Hi Don, IIRC there some weird and complex maths in calculation of julian dates that involves floating point operations. The output should be a long IIRC so mjd1 should cast to a unsigned(?) long for output.

However skorch1 you have used %s which indicates a pointer to a string, I wouldn't be surprised if this program caused a memory access violation. You need to cast mdj1 to unsiged long and use %lu in the fprintf format string and probably read up on format strings.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#4: Oct 17 '09

re: how to write a "long" value to a text file?


In my opinion, the definitive text for all date-related calculations is 'Calendrical Calculations' by Nachum Dershowitz and Edward Reingold. I have the first edition, copyrighted 1997. I see they are up to the 3rd edition now.
Reply