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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • skorch1
    New Member
    • Oct 2009
    • 1

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

    I want to write that "mjd1" value to a text file. I tried it using fprinf() function. but it dint work.
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<dos.h>
    #include<stdio.h>
    void main()
    {
       clrscr();
       FILE *TempFile; 
       int year,day,month,c=1,c1=0;
       char *temp="buf.txt";
       struct date d;
       long double mjd=54832,mjd1,count=0;
       cout<<"\nDay";
       cin>>day;
       cout<<"\nmonth";
       cin>>month;
       cout<<"\nyear";
       cin>>year;
       if(year!=2009)
       {
    	for(int j=2009;j<year;j++)
    	{   count+=365;
    	    if(j%4==0)
    	    c1++;
    	    cout<<"c1"<<c1;
    	}
        }
       if(year%4==0)
       c+=2;
       else c+=3;
       cout<<"\nc2"<<c;
       for(int i=1;i<month;i++)
    
           { if(i==11) c++;
    	 if(i==4)  c++;
    	 if(i==6)   c++;
    	 if(i==9)   c++;
    
    	   count+=31;
    	 }
       cout<<"\n Count"<<count;
       cout<<endl;
       mjd1= count + day + mjd - c + c1;
       cout<<"\nModified Julian Date:"<<mjd1;
      TempFile=fopen(temp,"w+");
      fprintf(TempFile,"Modified Julian date%s\t\n",mjd1);
      fclose(TempFile);
      getch();
    
    }
    Last edited by Niheel; Oct 17 '09, 10:54 PM. Reason: formatting
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    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.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      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.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        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.

        Comment

        Working...