problem with writing in a file!


Results 1 to 2 of 2

Thread: problem with writing in a file!

  1. #1
    Join Date
    Mar 2005
    Location
    asdhsadgas
    Posts
    197

    problem with writing in a file!

    this is a code wich is supposed to take all numbers with 2 decimals in Balun.txt and is supposed to divde them by 10000 and then modify them so that they have only 2 decimals and write them back.
    can anyone please help me with this.
    i'm trying for 2 hours and i can't get it right.
    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main(){
        FILE *f=fopen("Balun.txt","r+");
        char ch[4000];
        ch[0]=0;
        while(!feof(f))
        {
        fscanf(f,"%s",&ch);
        if(strstr(ch,".00")!=NULL)
        {
         int lungime=strlen(ch);
         int lungimenou;
         double cifra=atof(ch);
          cifra/=10000;
          
          sprintf(ch,"%f",cifra);                          
          ch[strlen(ch)-4]=NULL;
          lungimenou=strlen(ch);
          for(int i=lungimenou;i<=lungime;i++)
                                    ch[i]=' ';
          
          fseek(f,SEEK_CUR,-lungime);
          ch[lungime]=NULL;
          fwrite(ch,lungime-1,1,f);
          
    if (ferror (f))
          printf ("error!!!\n");
    
          printf("%s\n",ch);
    
    //getch();
        }
        
        else printf("string\n");
        };
        system("pause");
        fclose(f);
        return 0;}
    i'm stupid

  2. #2
    Join Date
    Jul 2005
    Posts
    121
    could you comment your code please, it makes reading it alot easier.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •