Tuesday, July 14, 2009

C Programming Language?

I have the Following Program:





# include %26lt;stdio.h%26gt;


int a;


int Temperature;


main(){


for (a=0; a %26lt;= 5; a++)


{


printf("Constant Temperature is 25\n");


printf("Read Temperature\n");


scanf("%d",%26amp;Temperature);


if (Temperature = 25)


printf("Temperature is 25\n");


else


if (Temperature %26gt; 25)


printf("Decrease Temperature To 25\n");


else


if (Temperature %26lt; 25)


printf("Increase Temperature To 25\n");


}


}





My Question is, How can i save the values i entered for the Temperature in a text file using C programming Language

C Programming Language?
If you want to do something other than redirect (which will redirect all output, including your comments about increasing/decreasing temperature).





Open a file:





FILE *fopen (char *name, char *mode) opens a file and returns a pointer to that file.





FILE *fp = fopen ("myname", "w")





Then, after reading in each temperature, do a formatted print to file:





fprintf(fp, "%d\n", Temperature);





At the end of the program, close the file.





fclose (fp)
Reply:You have to open your file before the for loop, and then write the vaules there in the loop, and at the end close the file.





Seems simple enough.
Reply:The simplest way I can say is to redirect the output to a file.





I guess ur program is TempProgram.c





so normally you would run it like this.





./TempProgram





but instead if you do this





./TempProgram %26gt; OutputFile.txt





everything that prints on screen will go to this file. You can later cat the file and see output. Similar thing can be done on Microsoft platforms.
Reply:It looks like you need to open Notepad. Enter 45. Save it as inputfile.txt . Then enter this to comand line: programname %26lt; inputfile.txt . Link is below. Look at 7.1 Standard input and Output


No comments:

Post a Comment