Originally Posted by
wang8442
here is my code:
<...>
i am a little confused about feof() function,i just want to read from
the input.txt file and output the numbers of input file into output.txt file,but when i excute the program,i could see nothing
in the output file.
the input file is like this:
0 1 3.3[return]
Here is your code commented (-ed-). Please, read the comment carefully and ask for details if needed :
Code:
/* -ed-
* Code reindented
* Comments moved (To prevent from having lines longer than 76 columns)
* Code full-braced.
* Useless macro removed
*/
#include<stdlib.h>
#include<stdio.h>
/* read characters in input file */
void Read (FILE * fpin, char *s)
{
char c;
c = fgetc (fpin);
while (c != ' ' && c != '\n')
{
*(s++) = c;
c = fgetc (fpin);
}
*s = '\0';
}
/* change a string into a float number */
float TypeChange (char *s)
{
int i = 0, j = 0;
float m = 0;
while (s[i] != '\0')
{
if (s[i] == '.')
{
j = 0;
}
else
{
m = 10 * m + s[i] - '0';
j++;
}
i++;
}
if (i == j)
{
return m;
}
else
{
for (; j != 0; j--);
m = m / 10;
return m;
}
}
int main ()
{
char s[10];
float array[100];
int i = 0;
FILE *fpin, *fpout;
fpin = fopen ("input.txt", "r");
/* -ed-
* fopen() can fail returning NULL.
* This should have been tested before use.
* (typically, I have no 'input.txt' file on my machine).
*/
fpout = fopen ("output.txt", "w");
while (!feof (fpin)) /* some confusion here */
/* -ed-
* The main confusion is about the feof() function itself.
* Calling this functions only makes sense once you have detected an
* 'end of reading' condition. Each time a stream reading function is
* called, and end of reading can happen, mainly for two reasons :
* - An error occured
* - The end of file was reached.
* This is why the readind function returns some information (NULL, EOF)
* to indicate this condition. This should be tested, and now,
* and only now, the feof() and ferror() functions can be called to
* identify the exact cause of the end of reading, if you really want to
* know about these details.
*/
{
Read (fpin, s);
/* -ed-
* Typically, this function should return some 'end of reading' status...
*
* Note that the interface of this function is dangerous.
* If I got it well, 's' the address of an output array of char.
* You forgot to pass the sizeof of it, making the Read() function
* unsafe (rejected by my Quality Control Service)
*/
array[i] = TypeChange (s);
fprintf (fpout, "%f", array[i]);
i++;
}
fclose (fpin);
fclose (fpout);
return 0;
}
And here is your code fixed and simplified:
Code:
#include<stdlib.h>
#include<stdio.h>
/* read characters in input file */
int Read (FILE * fpin, char *s, size_t size)
{
size_t i = 0;
int c;
while ((c = fgetc (fpin)) != ' '
&& c != '\n'
&& c != EOF
&& i < size - 1)
{
s[i] = c;
i++;
}
s[i] = '\0';
return c;
}
/* change a string into a float number */
float TypeChange (char *s)
{
int i = 0, j = 0;
float m = 0;
while (s[i] != '\0')
{
if (s[i] == '.')
{
j = 0;
}
else
{
m = 10 * m + s[i] - '0';
j++;
}
i++;
}
if (i == j)
{
return m;
}
else
{
for (; j != 0; j--);
m = m / 10;
return m;
}
}
int main (void)
{
#define F_IN "input.txt"
#define F_OUT "output.txt"
FILE *fpin = fopen (F_IN, "r");
if (fpin != NULL)
{
FILE *fpout = fopen (F_OUT, "w");
if (fpout != NULL)
{
char s[10];
while (Read (fpin, s, sizeof s) != EOF)
{
float value = TypeChange (s);
fprintf (fpout, "%f\n", value);
}
fclose (fpout);
}
else
{
perror (F_OUT);
}
fclose (fpin);
}
else
{
perror (F_IN);
}
return 0;
}
Note that the whole thing could have been done with fgets() and strtod() in an tenth of lines...