#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Code is creating a read-only file. i dnt want that


    #include <stdio.h>
    #include<math.h>
    #define e 2.7182818284590452353602874
    int createfile(void)
    {
    FILE *Output;
    Output = fopen("transient.xls", "w");
    }
    int data(double y,double z)
    {
    FILE *Output;
    Output = fopen("transient.xls", "a+");
    if (Output != 0)
    fprintf(Output, " %lf \t %lf \n ", y,z);
    else printf("file not opened");
    fclose(Output);
    return 0;
    }
    int main()
    {
    int x;
    double v,r,l,t=0,i,a;
    createfile();
    printf("enter the values for voltage(in v)\n");
    scanf ("%lf",&v);
    printf("enter the values for resistance(in ohm) \n");
    scanf ("%lf",&r);
    printf("enter the values for inductance(in henry) \n");
    scanf ("%lf",&l);
    for (t=0;t<100;t=t+0.5)
    {
    a = (double)-1*l*t/r;
    i = (v/r)*(1-pow(e,a));
    printf(" \n the value of current is %lf", i );
    data(t,i);
    }
    scanf ("%d",x);
    return 0;
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    If your file is read-only it is because of your operating system configuration. Instead, I think the actual problem is that your createfile function doesn't communicate properly with the outside world.
    Code:
    /* pay attention to compiler warnings *****************/
    
    
    #include <stdio.h>
    #include<math.h>
    
    /* you included math, which has M_E *****************/
    #define e M_E			/* use  exp function *****************/
    
    int createfile(void) {
      /* createfile does not communicate with the outside world *****************/
      FILE *Output;
      Output = fopen("transient.xls", "w");
    }
    int data(double y,double z) {
      FILE *Output;
      Output = fopen("transient.xls", "a+");
      if (NULL != Output)		/* NULL isn't necessarily 0 (it is in C++) *****************/
        fprintf(Output, " %lf \t %lf \n ", y,z);
      else fputs("file not opened",stdout);
      fclose(Output);
      return 0;
    }
    int main() {
      int x;			/* x takes an unknown value *****************/
      double v,r,l,t=0,i,a;
      createfile();
      puts("enter the values for voltage(in v)"); /* printf is inefficient.  Use it to convert values. *****************/
      scanf ("%lf",&v);
      puts("enter the values for resistance(in ohm)");
      scanf ("%lf",&r);
      puts("enter the values for inductance(in henry)");
      scanf ("%lf",&l);
      for (t=0;t<100;t+=0.5) {
        a = (double)-1*l*t/r;
        i = (v/r)*(1-pow(e,a)); 	/* you included math, use exp(a) *****************/
        printf(" \n the value of current is %lf", i ); /* why do you put a space character before \n?  (at end of previous line) *****************/
        data(t,i);
      }
      scanf ("%d",&x); /* storing an integer into unknown memory location is bad plan *****************/
      /* best to fix the behavior of the console window.  Search internet for "dos window remain open" *****************/
      return 0;
    }

    Code:
    /* something like this */
    FILE*createfile(void) {
      FILE *Output;
      char*filename = "transient.xls";
      Output = fopen(filename, "w");
      if (NULL == Output) were_totally_hosed(filename);
      return Output;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo