首先,我真的很抱歉發布這么長的代碼,但我已經剪掉了,我認為這就是我解決問題所需要的一切。也歡迎你給我反饋,我可以為我未來的問題改進:)
也可能有語法/翻譯錯誤,因為英語不是我的第一語言。
我的問題:我希望用戶能夠閱讀這個結構:
typedef struct {
char movieTitle[30];
unsigned int yearPublished;
char director[50];
char genre[50];
float profit;
} movie_t;
來自一個已經有 4 部電影的二進制檔案,但也應該能夠存盤最多 100 個電影結構,如這里定義的:
//global var.
// ================================================================
#define MAX_STORAGE 100
movie_t movieData[MAX_STORAGE];
movie_t movieRecord;
unsigned int movieCount = 0;
使用我已經在下面撰寫的代碼,我無法以某種方式從檔案中讀取所有電影,因為我的“計數檢查器”每次只讀取一部電影時都會告訴我。在我的“readFile”函式下方的控制臺輸出代碼中也只有一部電影。
// main functions --- read from file
// ================================================================
int
readFile(const char* fileName)
{
//open file
FILE* read = fopen(fileName, "rb");
if (!read)
{
printf("ERROR: Could not open the file!\n");
return -1;
}
else
{
printf("%s were opened successfully.\n\n", fileName);
}
//read from file
int count = 0;
while (!feof(read))
{
size_t count = fread(&movieRecord, sizeof(movieRecord), 1, read);
if (count == 0)
{
printf("ERROR: Read process was unsuccessful or incomplete!\n");
}
if (count == 1)
{
printf("Successfully read %i movie.\n", count);
}
else
{
printf("Successfully read %i movies.\n", count);
}
return count;
}
fclose(read);
return 0;
}
這是應該顯示檔案中所有電影的控制臺輸出。此外,它們應該以 1 - 100 的數字 (Num.) 顯示。
// main functions --- console Output
// ================================================================
void
consoleOutput(movie_t movieData2, unsigned index)
{
printf("MOVIE DATA Num. %u\n\n", index);
printf("Movie title : %s\n", movieData2.movieTitle);
printf("Publishing year : %u\n", movieData2.yearPublished);
printf("Director : %s\n", movieData2.director);
printf("Genre : %s\n", movieData2.genre);
printf("Profit : %.2f\n", movieData2.profit);
}
如果你想看的話,這就是我的主要功能的樣子:
// MAIN
// ================================================================
int main(void)
{
// defaultname for file
char fileName[50] = "movies.dat";
// variable for loop
int stopLoop = 0;
// loop
do {
// Output menu
printf("\nMENU:\n");
printf("(a) Show all movies\n");
printf("(o) Read moviefile\n");
printf("(q) Exit programm\n");
printf("\nYour choice: ");
// User input
char ch = _getch();
printf("%c\n\n", ch);
// Switch case for user input
switch (ch)
{
case 'q': // Exit programm
stopLoop = 1;
break;
case 'a': // Show all movies
consoleOutput(movieRecord, movieCount = readFile(fileName));
break;
case 'o': // Read moviefile
readFile(fileName);
break;
default:
printf("==> Invalid input!\n");
}
} while (!stopLoop);
return 0;
}
旁注:因為用戶能夠輸入新電影,所以他們應該能夠將這些資料保存到他們閱讀的同一個檔案中。但這是我想在尋求幫助之前先測驗自己的事情。
我真的很感激任何幫助,因為我仍然是編碼的初學者,這個問題讓我有點沮喪......
非常感謝您提前!:)
uj5u.com熱心網友回復:
僅閱讀一個條目后,您將從回圈正文回傳。如果您真的希望讀取檔案中的所有條目,則需要將讀取回圈更改為讀取到您的陣列中,由于fread作業原理,您實際上可以一起擺脫所有條目:
// main functions --- read from file
// ================================================================
int
readFile(const char* fileName)
{
//open file
FILE* read = fopen(fileName, "rb");
if (!read)
{
printf("ERROR: Could not open the file!\n");
return 0; // note that since you'll probably assign the output to movieCount, it might be better to return 0 on failure
}
else
{
printf("%s were opened successfully.\n\n", fileName);
}
// Here's where the problem, you only ever read out one movie entry into movieRecord and returned
// You can use fread to read out the entire file into the array instead
// It would also be better not to use a global variable for this, instead taking in a pointer to an movie_t array to readFile instead but I left it using the global as per the original program
size_t count = fread(movieData, sizeof(movie_t), MAX_STORAGE, read);
if (ferror(read))
{
printf("ERROR: Read process was unsuccessful or incomplete!\n");
fclose(read);
return 0;
}
if (count == 1)
{
printf("Successfully read %i movie.\n", count);
}
else
{
printf("Successfully read %i movies.\n", count);
}
fclose(read);
return count;
}
從這里開始,您將movieData填充檔案中的資料,并相應地調整程式的其余部分。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/394866.html
下一篇:我不明白如何使用?currentPage=0&pageSize=18[php]之類的引數從MYSQLI中列出json
