Originally Posted by
vart
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp,*fw;// declaring file pointers.
double val;
char ch;
int new; // new for integer part.
if((fp = fopen("myfile.txt","r"))==NULL)
{
printf("sorry,cannot open the file\n");
exit(0);
}
fw = fopen("mynewfile.txt","w"); /* not checking return value */
do{
fscanf(fp, "%lf", &val); /* not checking return value */
ch = getc(fp); /* ch should be int */
new=val; //pass numbers to integer form.cos new is defined intefer
fprintf(fw,"%d",new); /* don't you need space between numbers? */
printf("%lf",val); //print out the real number.
printf("%d",new); //print out the integer number.
if ( ch=='\n' || ch==EOF )/* ch should be int to correctly compare with EOF */
printf("\n"); // switch to next line.
}while ( ch != EOF );
fclose(fp); /* what about closing the over file as well ? */
return 0;
}
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp,*fw;// declaring file pointers.
double val;
int ch;
int new; // new for integer part.
int i=0;
if((fp = fopen("a.txt","r"))==NULL)
{
printf("sorry,cannot open the file\n");
exit(0);
}
if((fw = fopen("mynewfile.txt","w"))==NULL)
do{
fscanf(fp, "%lf", &val); //read real number from the file. i dont know how the check the return number
ch = getc(fp); //get the following number after the realnumber.
new=val; //pass numbers to integer form.cos new is defined intefer
fprintf(fw,"%d",new);// the numbers are arranged in conclum 3*5, with a space to separate them.
printf("%lf",val); //print out the real number.
printf("%d",new); //print out the integer number.
i++;
if ( i%5==0 || ch==EOF )//determin to end the progress if there is a '\n' char or End Of File.
fprintf(fw,"\n"); // switch to next line.
}while ( ch != EOF );
fclose(fp);// what the matter?i just want to close the file
return 0;
}