Again Needing Help

Danetee
Danetee used Ask the Experts™
on
I am suppose to take a file read the contents , show on screen then write the contents to another file , I can get it to read the first number from the file , but not the characters and it does not write anything but zeros  , help!!! here is the code I have done
#include <stdlib.h>
#include <stdio.h>

  main()
  {
      FILE *fp;
      char filename[40], mode[4];
     float f1,f2,f3,f4,f5;
     int count;

     while (1)
     {

         /* Input filename and mode. */

         printf("\nEnter a filename: ");
         gets(filename);
         printf("\nEnter a mode (max 3 characters): ");
         gets(mode);

         /* Try to open the file. */

         if ( (fp = fopen( "rates.txt", "r" )) != NULL )
         {
             printf("\nSuccessful opening %s in mode %s.\n",
                     filename, mode);
     fscanf(fp,”%f %f %f %f %f”,&f1, &f2, &f3, &f4, &f5);
     printf(“ The values are %f,%f,%f,%f,and %f\n.”,f1,f2,f3,f4,f5);
     printf("\nEnter a filename: ");
      gets(filename);
      printf("\nEnter a mode (max 3 characters): ");
      gets(mode);
        /* Try to open the file. */

         if ( (fp = fopen( "conversion.txt", "w" )) != NULL )
         {
             printf("\nSuccessful opening %s in mode %s.\n",
                     filename, mode);
     for (  count =0; count <5; count++)
     {
               fprintf(fp, “%f”,count, f1);
               fprintf(fp, “%f”,count, f2);
               fprintf(fp, “%f”,count, f3);
               fprintf(fp, “%f”,count, f4);
               fprintf(fp, “%f”,count, f5);
}
      fclose(fp);
     getchar();
     return 0;
     }
}}}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Can you show me what the first and second lines of the "rates.txt" file are?  

You say that "I can get it to read the first number from the file , but not the characters" but you only seem to be reading in floats and no chars. Does "rates.txt" contain only floats?  If it contained strings then fscanf would need them included in the format or you would be trying to read in strings into the float.

Commented:
u shud used fscanf ..only IFF u have fixed format of data written in the file..
other wise use normal fgets OR read OR fread
..
another thing . u have only one file pointer ..  u r reading from same file and writing to same . i hope u know what u r doing
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
struct _stat buf;
int data,flen,result;
long int i,fsize;
char filename[30];                                      
char fdata[50000];                    
FILE *fp;                        

getfile:
printf("Enter filename to transmit as string\n");
scanf("%s", &filename);

flen = strlen(filename);                  
if (flen > 30)
{
    printf("Filename too large\n");
    goto getfile;
}

fp = fopen(filename,"rb");     // binary read          
          if ( (fp == NULL))
          {
               printf("Error in opening the file for reading\n");
              goto getfile;
          }
               data = getc( fp );

             result = _stat(filename,&buf );
             fsize = buf.st_size;
     
i=0;
data = getc( fp );

    while (i !=fsize )
        {
         fdata[i] = data;
            printf("%c",fdata[i]);              
           data = getc( fp );                  
         i++;
    }
          fclose(fp);

         printf("Enter name to copy file to ");
         scanf("%s",&filename);

   fp = fopen(filename,"wb");  // binary write

i=0;
         while(i != fsize)
         {
              putc(fdata[i], fp );          
           i++;
         }
      fclose(fp);

printf("File copied successfully\n");
}

Commented:
you can use your same code.but in the input file the numbers should be separated with single spaces.eg: 1 2 3 4.
i also agree with akhsay that u'r using the same pointer for reading & writing.also i couldn't get why u'r accepting i/p from the user as file name & mode.
u can use this code it's the same code u have written but with a little modification.
----------------------------------------------------
#include <stdlib.h>
#include <stdio.h>

 main()
 {
     FILE *fr,*fw;
     float f1,f2,f3,f4,f5;
     int count;

     if ( (fr = fopen( "rates.txt", "r" )) != NULL )
     {
           printf("\nSuccessful opening \n");
           fscanf(fr,"%f%f%f%f%f",&f1, &f2, &f3, &f4, &f5);
           printf("The values are %f,%f,%f,%f,and %f\n.",f1,f2,f3,f4,f5);
        fclose(fr);
     }
     else
     {
        printf("\n Error opening file in read mode \n");
        return(0);
     }
     if ( (fw = fopen("conversions.txt","w")) != NULL)    
     {
        printf("\nSuccessful opening \n");
         fprintf("%f %f %f %f %f ",f1,f2,f3,f4,f5);
        fclose(fw);
     }
     else
     {
           printf("\n Error opening fiel in write mode \n");
         return(0);
     }
     return(1);
}

----------------------------------------------------
     
hope this may help you      
     




Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial