בעיה בתוכנית

Sandro

New member
בעיה בתוכנית

אני כותב ב- 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;
}
 

Sandro

New member
נעשה את זה יותר ברור

#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;
}
 

BravoMan

Active member
אתה לא יכול סתם ככה להתחיל לקרוא נתונים

מסוף קובץ שפתוח לכתיבה בלבד.

גם משום שאתה בסוף הקובץ ואין מה לקרוא, וגם כי כאמור הקובץ פתוח לכתיבה בלבד ולכן אי אפשר לקרוא ממנו.

אחרי שאתה מסיים לכתוב, סגור את הקובץ ופתח אותו לקריאה. אז תוכל לקרוא.
השימוש ב-feof דווקא בסדר.
 

Sandro

New member
היי, עדיין לא הצלחתי

תודה על התגובה, תיקנתי בהתאם למה שאמרת, אבל עכשיו הוא עושה Error בזמן הרצה :/

#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++;
}
fclose(binary);
}

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);
FILE* binary2=fopen("binary.txt","r+b");
while (!feof(binary2)) {
fscanf (binary2, "%d", array[n]);
printf("%d ", array[n]);
n++;
}
fclose(binary2);
return 0;
}
 

BravoMan

Active member
"עושה error בזמן ריצה" אומר בערך:

"המכונית לא נוסעת".
אי אפשר להבין מזה אם נגמר הדלק, יש תקלה מכנית, או סתם מישהו שכח לסובב את המפתח.

במבט חטוף אני יכול לראות שאתה משתמש לא נכון ב-fscanf, ומעביר תא במערך במקום מצביע עליו (כתובת שלו).
ייתכן שיש שגיאות נוספות שלא שמתי לב עליהן, אבל אם לא תספר מה הודעת השגיאה המדויקת שאתה מקבל, יהיה קשה מאוד לעזור לך.

אגב, למה r+b במקום פשוט rb?
 

Sandro

New member
עבד, ממש תודה! :)

בהתחלה באמת עשיתי &array אבל חשבתי שאולי כאן הטעות, עכשיו ששיניתי בחזרה הכל באמת עובד כמו שצריך
חשבתי שיהיה מגניב להוסיף "+" חחח
 
למעלה