我試圖運行我的 c 代碼。但是我遇到了一個問題,并且確實嘗試了很多來獲得正確的東西,但我不知道問題出在哪里。什么是視覺作業室告訴我,我有語法錯誤也錯過了“}”但我檢查了每一件事,如果結構有錯誤原因,它仍然給我錯誤請告訴我如何解決這個問題,但如果它對我有幫助得到正確的想法非常感謝你。這是我下面的代碼。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct University
{
Student* arr;
}University;
typedef struct Student
{
char* name;
long id;
float grade;
char labor[5];
char finalg;
}Student;
int readfile(University uni);
void outputfile(University uni, int n);
int main()
{
int n;
University uni;
n=readfile(uni);
outputfile(uni, n);
return 0;
}
int readfile(University uni)
{
FILE* fp;
char name[100], ch[5];
long id;
float grade;
int cnt = 0, i, j;
fp = fopen("input.txt", "rt");
if (fp == NULL)
{
printf("ERROR: Cannot open input file.\n");
return 1;
}
while (fscanf(fp, "%s %l %f %c", name, &id, &grade, &ch) > 0)
{
if (cnt == 0)
{
uni.arr = (Student*)malloc(1 * sizeof(Student));
if (uni.arr == NULL)
{
printf("No Memory to allocation.\n");
exit(1);
}
cnt ;
}
else if (cnt >= 1)
{
uni.arr = (Student*)realloc(uni.arr, (cnt 1) * sizeof(Student));
if (uni.arr == NULL)
{
printf("No Memory to allocation.\n");
exit(1);
}
cnt ;
}
for (i = cnt - 1; i < cnt;i )
{
uni.arr[i].name = (char*)malloc((strlen(name) 1) * sizeof(char));
if (uni.arr[i].name == NULL)
{
printf("No Memory to allocation.\n");
exit(1);
}
uni.arr[i].id = id;
uni.arr[i].grade = grade;
for (j = 0;j < 5;j )
uni.arr[i].labor[j] = ch[j];
}
}
return cnt;
}
void outputfile(University uni, int n)
{
FILE* fo;
int i,check,j;
fo = fopen("outputfile.txt", "w");
for (i = 0;i < n;i )
{
check = 0;
for (j = 0;j < 5;j )
if (uni.arr[i].labor[j] == 1)
check ;
if (check < 3)
check = 0;
else
check = 1;
fprintf(fo,"Student[%d]: %s %.2f %d", i 1,uni.arr[i].name,uni.arr[i].grade,check);
}
}
輸入檔案是:
michel 123 90.1 11001
alex 234 92.3 10011
Linda 345 89.4 10001
Shoco 456 90.6 01001
我想要的輸出檔案是:
Student 1: michel 123 90.1 1
Student 2: alex 234 92.3 1
Student 3: Shoco 456 90.6 0
……………..
…………….
Student n: Shoco 456 90.6 0
uj5u.com熱心網友回復:
語法錯誤
在 C Playground(可編輯)中,我收到以下錯誤:
/cplayground/code.cpp:7:5: error: unknown type name 'Student'
Student* arr;
^
在這里,在大學的定義中,您在定義學生之前參考它。將 Student 的定義移到 University 之前,它會起作用。
以下錯誤都屬于一起,解釋如下:
/cplayground/code.cpp:42:29: warning: invalid conversion specifier ' ' [-Wformat-invalid-specifier]
while (fscanf(fp, "%s %l %f %c", name, &id, &grade, &ch) > 0)
~~~^
/cplayground/code.cpp:42:44: warning: format specifies type 'float *' but the argument has type 'long *' [-Wformat]
while (fscanf(fp, "%s %l %f %c", name, &id, &grade, &ch) > 0)
~~ ^~~
%ld
/cplayground/code.cpp:42:49: warning: format specifies type 'char *' but the argument has type 'float *' [-Wformat]
while (fscanf(fp, "%s %l %f %c", name, &id, &grade, &ch) > 0)
~~ ^~~~~~
%f
這些都與使用%lwhich 不是完整的說明符有關。第一個錯誤告訴您這一點,其他兩個是因為以下說明符和引數不同步。修復第一個錯誤,其余的都會消失。
最后:
/cplayground/code.cpp:42:57: warning: data argument not used by format string [-Wformat-extra-args]
while (fscanf(fp, "%s %l %f %c", name, &id, &grade, &ch) > 0)
~~~~~~~~~~~~~ ^
4 warnings and 1 error generated.
在這里,您曾經%c指示單個字符,但正在傳遞一個char **(ch陣列的地址)。我懷疑你想要%s一個 C 字串,然后你只想選擇第一個元素,如果這是你想要的輸出(注意:C Playground 允許你上傳一個包含測驗輸入檔案的 ZIP,如果你在 cog 選項下查看想在那里嘗試)。
按值傳遞的引數
在定義和實作中readfile,outputfile你是按值傳遞的。這意味著該函式正在更新uni變數的副本,而不是傳遞的那個。所以添加的任何東西readfile都不會到達outputfile。
int readfile(University uni);
void outputfile(University uni, int n);
您需要更改宣告和實作以傳遞指標,因此:
int readfile(University *uni);
void outputfile(University *uni, int n);
然后,在這兩個函式中,將.訪問器更改->為取消參考指標。例如
uni->arr = (Student*)malloc(1 * sizeof(Student));
if (uni->arr == NULL)
邏輯錯誤
還有其他邏輯錯誤會阻止它正確運行,但我將把它留給 OP。
值得注意的是,for分配新學生后的回圈是不必要的(它只會回圈一次,它的目的是針對陣列末尾的“空”元素)。
在這里要做的一件好事是獲取指向最后一個元素的指標,然后使用它來簡化邏輯。
Student *student = uni->arr[cnt-1];
strcpy(student->name, name);
//...
你也忘了復制名字。
如果沒有輸入檔案,你應該exit()而不是回傳,否則你會得到一個分段錯誤。
除了外觀之外,還有更多,但希望這能讓你走得更遠,然后你可以回來。
這是一個更新的游樂場,可以解決邏輯錯誤。
uj5u.com熱心網友回復:
我認為大學結構中的結構構建存在指標問題您需要重寫指標。
uj5u.com熱心網友回復:
我建議你去看看你的編譯器錯誤,有很多!例如:
main.c:66:23: error: request for member ‘name’ in something not a structure or union
66 | uni.arr[i].name = (char*)malloc((strlen(name) 1) * sizeof(char));
| ^
main.c:67:27: error: request for member ‘name’ in something not a structure or union
67 | if (uni.arr[i].name == NULL)
| ^
main.c:72:23: error: request for member ‘id’ in something not a structure or union
72 | uni.arr[i].id = id;
| ^
main.c:73:23: error: request for member ‘grade’ in something not a structure or union
73 | uni.arr[i].grade = grade;
| ^
main.c:75:27: error: request for member ‘labor’ in something not a structure or union
75 | uni.arr[i].labor[j] = ch[j];
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/473136.html
上一篇:有沒有辦法使用#define指令來創建一個常量結構?
下一篇:如何將int轉換為void*?
