好的,我正在制作一個評分程式/代碼,它將擁有自己的文本檔案,用于存盤所有成績。我想制作一個二維陣列,其中第一個“維度”是學生,第二個“維度”是個人成績(如果有更聰明的成績方式告訴我,順便說一下,我選擇了這種方法,因為它是唯一的我知道以后如何只添加更多學生或更多成績)請記住,成績和學生的數量并不總是固定的,因此沒有簡單的出路。無論如何,我已經嘗試過一些東西,我認為它只適用于字符而不適用于整數(即使成績是 1-5)。此外,我想要一種列印出來的方法,但我認為這是更大的問題。謝謝。
typedef char string [20];
string row;
int i=0,j=0;
char arr[20][20];
FILE *fp;
fp=fopen("grades.txt","r");
for(i=0;arr[i-1][j]!=EOF;i )
{
fgets(row,sizeof(row),fp);//I used fgets so I could get the size of the line
for(j=0;j<strlen(row);j )
{
fscanf(fp,"%c ",&arr[i][j]);
}
}
我不知道這是否會幫助,但我認為該文本檔案將是這個樣子:
54455
43544
22443
21232
21121
uj5u.com熱心網友回復:
fgets報告它是否有效。因此,讀取直到檔案結束或直到緩沖區已滿:
for(i = 0; i < sizeof arr / sizeof *arr && fgets(arr[i], sizeof arr[i], fp); i )
{
// probably remove the \n that fgets writes into the buffer
// otherwise nothing else to do
}
網站注意事項:
fgets從檔案中讀取,不需要額外的讀取fscanffgets讀取包含換行符的行,如果您不想要,請將其洗掉- 你需要檢查是否
fopen有效
uj5u.com熱心網友回復:
我找到了將學生成績存盤在struct. 一般來說,每個學生都有名字、姓氏、成績……您可以添加任何您想要的內容。我對fname, lname,很好grades。
typedef struct student_s {
char fname[25];
char lname[25];
int* grades;
int count_of_grades; // Track number of grades for each student
} student_t;
通過分配一個動態陣列,student_t您可以獲得任意數量的學生。
// Allocate array of structs
student_t* students = (student_t *) malloc(sizeof(student_t));
通過使用,getline()您可以一次從檔案中讀取整行(行以 結尾\n)。getline()不是標準的 C 函式,因此您需要放在#define _GNU_SOURCE腳本的開頭。
while ((read_len = getline(&line, &len, fp)) != -1)
每次函式getline()讀取檔案的下一行時,陣列大小count將增加并重新分配陣列。
count;
// Increase size of array beacause of new student to add
students = realloc(students, sizeof(student_t) * count);
if (students == NULL)
{
printf("Couldn't allocated memmory\n");
return 1;
}
下一步是分配grades陣列,該陣列將存盤特定學生的所有成績。回圈遍歷line您可以提取每個等級。然后只需定義 struct 的成員,您就可以為每個學生添加成績。
// Allocate array to store all grades from file for one student
// Count of grades does not have to be the same for every student
students[index].grades = (int *) malloc(sizeof(int) * (read_len-1));
// Iterate grades read from file
for (int i=0; i<read_len-1; i)
{
// char --> char *
char grade[2] = "\0";
grade[0] = line[i];
// Add grade to the array of grades
students[index].grades[i] = atoi(grade);
}
最后,您應該將等級數存盤在陣列中,以便稍后在腳本中使用資料進行簡單操作。
// Track number of grades
students[index].count_of_grades = read_len-1;
index
完整代碼:
#define _GNU_SOURCE // necessery to use getline()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student_s {
char fname[25];
char lname[25];
int* grades;
int count_of_grades;
} student_t;
int main(int argc, char const *argv[])
{
// Allocate array of structs
student_t* students = (student_t *) malloc(sizeof(student_t));
int count = 0;
int index = 0;
FILE* fp;
char* line = NULL;
size_t len = 0;
ssize_t read_len;
fp = fopen("data.txt", "r");
if (fp == NULL)
{
return 1;
}
// Read line by line from file until fp reaches end of file
while ((read_len = getline(&line, &len, fp)) != -1)
{
count;
// Increase size of array beacause of new student to add
students = realloc(students, sizeof(student_t) * count);
if (students == NULL)
{
printf("Couldn't allocated memmory\n");
return 1;
}
// Replace with your code, which adds name to struct or get rid of it (also from struct)
memcpy(students[index].fname, "John", 4);
memcpy(students[index].lname, "Wash", 4);
// Allocate array to store all grades from file for one student
// Count of grades does not have to be the same for every student
students[index].grades = (int *) malloc(sizeof(int) * (read_len-1));
// Iterate grades read from file
for (int i=0; i<read_len-1; i)
{
// char --> char *
char grade[2] = "\0";
grade[0] = line[i];
// Add grade to the array of grades
students[index].grades[i] = atoi(grade);
}
// Track number of grades
students[index].count_of_grades = read_len-1;
index;
}
fclose(fp);
if (line)
{
free(line);
}
// Print data from structs
for (int i=0; i<count; i)
{
printf("%s: ", students[i].fname);
for (int j=0; j<students[i].count_of_grades; j)
{
printf("%d ", students[i].grades[j]);
}
printf("\n");
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/352726.html
