我在第一次printf,printf("原文:\ n")之后就收到了分段错误。注意:grades.csv是一个excel文件。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
void getGradesFromFile(const char *filename,
int size,
char line[],
int studentI_D[],
int test1,
int test2,
int test3,
char grades[]);
void insertionSort(int length,
int studentI_D[],
char list[]);
void flushScanf();
void binarySearch(int size,
int target,
int studentID[]);
void printArray(int size,
char A[*],
int B[*],
int test1,
int test2,
int test3,
char grades[]);
int main(void)
{
char grades[2];
int size = 60;
char str[size];
int studentID [7];
char letter;
int exam1, exam2, exam3;
int num;
printf("Original:\n");
getGradesFromFile("grades.csv", size, str, studentID, exam1, exam2, exam3, grades);// function call
printArray(size, str, studentID, exam1, exam2, exam3, grades);
printf("Sorted:");
insertionSort(size, studentID, grades);// inscertion sort function call
printf("\n\n");
printf("Enter student ID and -1 to quit");
scanf("%d", num);
flushScanf();
printf("Hello World");
while (num != -1){
if (num > 99999 && num < 1000000){
binarySearch(size, num, studentID);
}
else {
printf ("Error and enter again [100000, 999999] and -1 to quit");
}
exit;
}
return 0;
} // end of main
/*
*Function name: getNumbersFromFile
*
*Input Parameters: const char *filename, int size, float number[]
*
*Desription: gets numbers from the file and checks for errors
*
*Return value: filename
*/
我不确定是否使用此功能正确获取数据,感谢您对此功能的任何帮助。该函数的作用是从excel文件中获取数据并将其放入字符串中。
void getGradesFromFile(const char *filename,
int size,
char line[],
int studentI_D[],
int test1,
int test2,
int test3,
char grades[])
{
FILE* pData = fopen("grades.csv","r+");
int i = 0;
if (pData == NULL) {// opens file grades.csv
fprintf(stderr, "Error opening file.\n", filename);// error handling statement
exit(1);
}
while (fgets(line,sizeof(line), pData) != NULL){
sscanf(line, "%s, %d, %d, %d, %s", studentI_D[i], &test1, &test2, &test3,
grades[i]);
i++;
}
if (fclose(pData)== EOF) {
fprintf(stderr, "Error closing file.\n", filename);// close file
exit(2);
}
}
void insertionSort(int length, int studentID[], char grades[])
{
int i, key, key1, j;
for (i = 1; i < length; i++)
{
key = studentID[i];
key1= grades[i];
j = i-1;
while (j >= 0 && studentID[j] > key)
{
studentID[j+1] = studentID[j];
grades[j+1] = grades[j];
j = j-1;
}
studentID[j+1] = key;
grades[i+j] = key1;
}
}
void flushScanf()
{
while(getchar() != '\n')
;
}
void binarySearch(int size, int target, int student[])
{
char *locn;
int first = 0;
int last = size - 1;
int mid;
bool found = false;
while (first <= last) {
mid = (first + last) / 2;
if (target > student[mid]) {
first = mid + 1;
} else if (target < student[mid]) {
last = mid - 1;
} else {
*locn = mid;
found = true;
break;
}
}
}
void printArray(int size,
char A[size],
int B[],
int test1,
int test2,
int test3,
char grades[])
{
for (int j=0; j<4; j++) {
printf("%s, %d, %d, %d, %s", A[j], B[j], test1, test2, test3, grades[j] );
}
}
答案 0 :(得分:0)
检查getGradesFromFile中的sscnaf逻辑。你想要使用%s输入成绩而不是%c。
void getGradesFromFile(const char *filename,
int size,
char line[],
int studentI_D[],
int test1,
int test2,
int test3,
char grades[])
{
FILE* pData = fopen("grades.csv","r+");
int i = 0;
if (pData == NULL) {// opens file grades.csv
fprintf(stderr, "Error opening file.\n", filename);// error handling statement
exit(1);
}
while (fgets(line,sizeof(line), pData) != NULL){
sscanf(line, "%d, %d, %d, %d, %c", &studentI_D[i], &test1, &test2, &test3,
&grades[i]);
i++;
}