I have school project in Linux and I need to create a user defined text file which has 1000 random numbers between 100-999.
I managed to create user defined file by using array and my code has no errors but when I run it I only have one number in my file but I want to have 1000 numbers I use \n but its not working please help me ?
#include<stdio.h>
#define MAX 100
int main()
{
FILE *fp;
char dosya[MAX];
printf("\nLütfen dosya adı giriniz:");
scanf("%s",dosya);
fp=fopen(dosya,"w");
int i;
for (i=0;i<1000;i++);
{
int sayi;
sayi=rand()%999-100;
fprintf(fp,"\n");
fprintf(fp,"%d\n",sayi);
fprintf(fp,"\n");
}
fclose(fp);
return 0;
}
When I run this in my file there is only one number so I think it keeps writing on the same line for 1000 times (but there is /n) where is the rest I checked the for loop and its working please help me ?
Remove semicolon after for loop.
When you execute your code all after all 1000 iterations only one time your block after for loop executes.
for (i=0;i<1000;i++);
^^
In C, all instructions are terminated in a semicolon
Your "for" statement has a semicolon at the end, which is interpreted as a "do nothing" instruction.
for (i=0;i<1000;i++);
Remove the trailing semicolon and it will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With