I have an example right here. I just did it. Maybe it's what you're looking for. It generates numbers between 0 and 10, and writes them to a file.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int writetoFile(int intArray[5]);
int main(void)
{
int anotherIntArray[6];
int i;
/*enter data*/
printf("Entering data (5 lines):\n");
srand((unsigned int)time(NULL));
int j;
for(j=0;j<5;j++)
{
anotherIntArray[j]=rand()%10;
printf("%d,",anotherIntArray[j]);
}
anotherIntArray[5]=0;
/*writing to file*/
writetoFile(anotherIntArray);
printf("\nDone writing to file...%d\n",anotherIntArray[5]);
return 0;
}
int writetoFile(int intArray[5])
{
/*open file and write to it*/
FILE *openedFile;
openedFile=fopen("data.dat","w");
if(openedFile==NULL)
{
printf("Could not open file. Terminate program");
exit(0);
}
int i;
for(i=0;i<5;i++)
{
printf("\nWriting to file %d",intArray[i]);
fprintf(openedFile,"%d\n",intArray[i]);
}
fclose(openedFile);
return 0;
}
|