a question about file output

This is a discussion on a question about file output within the C Programming forums, part of the General Programming Boards category; here is my code: Code: #include<stdlib.h> #include<stdio.h> #define MaxString 10 void Read(FILE *fpin,char *s) /* read characters in input file ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    8

    a question about file output

    here is my code:
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    #define MaxString 10
    
    void 
    Read(FILE *fpin,char *s)   /* read characters in input file */
    {
    	char c;
    	c=fgetc(fpin);
    
    	while(c!=' '&&c!='\n')
    	{
    	     *(s++)=c;
    	     c=fgetc(fpin);
    	}
    	*s='\0';
    }
    
    float 
    TypeChange(char*s)  /* change a string into a float number */
    {
    	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[MaxString];
    	float array[100];
    	int i=0;
    	FILE *fpin,*fpout;
    	fpin=fopen("input.txt","r");
    	fpout=fopen("output.txt","w");
    
    	while(!feof(fpin))      /* some confusion here */
    	{
    		Read(fpin,s);
    		array[i]=TypeChange(s);
    		fprintf(fpout,"%f",array[i]);
    		i++;
    	}
    
    	fclose(fpin);
    	fclose(fpout);
    	return 0;
    }
    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]

    Could anyone help me?thx~~~

  2. #2
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote 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...
    Last edited by Emmanuel Delaha; 12-26-2004 at 03:17 AM.
    Emmanuel Delahaye

    "C is a sharp tool"

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    12
    Doesn't the use of array[100] also fail your Quality Control Service? Especially since the use of an array there seems superfluous - you're only dealing with one float at a time, so just use a single float.

  4. #4
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by DonFiasco
    Doesn't the use of array[100] also fail your Quality Control Service? Especially since the use of an array there seems superfluous - you're only dealing with one float at a time, so just use a single float.
    This has been seen later. See my amended post.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 11:08 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 04:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21