בעיה בתוכנית
אני כותב ב- Microsoft Visual C++
קיבלתי משימה לקרוא ולכתוב לקובץ בינארי
הכתיבה בסדר - משום מה משהו משתבש לי בקריאה
אני חושב שהשימוש ב-feof לא טוב..
אשמח אם מישהו יוכל לתקן אותי
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
void writetobinary(int N, FILE *binary)
{
//FILE* binary=fopen("binary.txt","wb");
srand(time(NULL));
int x=1, t=0;
while (x <= N) {
t=rand()%6 + 1;
fprintf(binary,"%d ",t);
x++;
}
x=1;
// fprintf(binary,"\r\n");
while (x <= N) {
t=rand()%6 + 1;
fprintf(binary,"%d ",t);
x++;
}
}
int main () {
int N, n=0, array[100]={0};
FILE* binary=fopen("binary.txt","wb");
printf("Please enter an integer number:\n");
scanf("%d",&N);
writetobinary(N, binary);
while (!feof(binary)) {
fscanf (binary, "%d", array[n]);
printf("%d ", array[n]);
n++;
}
fclose(binary);
return 0;
}
אני כותב ב- Microsoft Visual C++
קיבלתי משימה לקרוא ולכתוב לקובץ בינארי
הכתיבה בסדר - משום מה משהו משתבש לי בקריאה
אני חושב שהשימוש ב-feof לא טוב..
אשמח אם מישהו יוכל לתקן אותי
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
void writetobinary(int N, FILE *binary)
{
//FILE* binary=fopen("binary.txt","wb");
srand(time(NULL));
int x=1, t=0;
while (x <= N) {
t=rand()%6 + 1;
fprintf(binary,"%d ",t);
x++;
}
x=1;
// fprintf(binary,"\r\n");
while (x <= N) {
t=rand()%6 + 1;
fprintf(binary,"%d ",t);
x++;
}
}
int main () {
int N, n=0, array[100]={0};
FILE* binary=fopen("binary.txt","wb");
printf("Please enter an integer number:\n");
scanf("%d",&N);
writetobinary(N, binary);
while (!feof(binary)) {
fscanf (binary, "%d", array[n]);
printf("%d ", array[n]);
n++;
}
fclose(binary);
return 0;
}