writing an array of numbers in a file in c.

Microsoft Visual studio 2008 professiona...
July 15, 2010 at 07:56:30
Specs: Windows XP, cpu
hi
i want to write an array of numbers (for example from 0 to 14) in a file
the source is something like that:
FILE *fp;
fp=fopen("output.txt","w");
for(int i=0;i<15;i++)
{a[i]=i;

fprintf(fp,"%f\n",a[i]);
}
fclose(fp);
no output is printed.can anybody say what is the problem?

See More: writing an array of numbers in a file in c.

Report •

#1
July 15, 2010 at 08:06:08
Read...

http://php.net/manual/en/function.f...

try...

FILE *fp;
fp=fopen("output.txt","w+");
for(int i=0;i<15;i++)
  {a[i]=i;
  fprintf(fp,"%f\n",a[i]);
  }
fclose(fp);


Report •

#2
July 15, 2010 at 17:22:56
1) You need to define FILE, etc. with #include <stdio.h>
2) %f is floating point numbers. You want %d for integers.
3) Why bother with the array, if you're just printing the numbers? Is this just a small fragment of the code?
4) "w" for the mode is fine. No need for "w+", as it's a once-through write of a fresh file.

Report •

#3
July 16, 2010 at 07:14:09
ya it s just a fragment.
here the problem is when i open the text file the output is there.
But it is not appearing in the output page when i run the program.
the function of fprintf statement is suppose to print output in both. is'nt it ?

Report •

Related Solutions

#4
July 16, 2010 at 07:36:09
fprintf: Writes to the specified stream a sequence of data formatted as the format argument specifies.
No, it does not.

Report •

#5
July 16, 2010 at 07:42:52
ok. thank u very much.

Report •

Ask Question