【问题标题】:Segmentation fault with function带函数的分段错误
【发布时间】:2016-11-18 02:12:16
【问题描述】:

我在第一次 printf 之后立即收到分段错误 printf("Original:\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] );
  }

}

【问题讨论】:

  • sizeof(line) --> size, studentI_D[i] --> studentI_D + i, %d for studentI_D
  • 仅供参考,exit; 很可能不会按照你的想法去做。将警告提高到 pedantic 级别,将它们视为错误,并修复标记的 所有内容Example.
  • 另外,它实际上不是从文件中读取的。
  • 我认为您尝试编写太多代码,然后您终于运行它,程序爆炸了,现在您不知道该怎么办。你必须学会​​一块一块地构建你的程序,并在你进行时进行测试。另外,阅读sscanf 的文档,了解数组和字符串,并学习使用调试器,例如gdb
  • 感谢您的建议,我仍然遇到分段错误问题,请帮助。

标签: c


【解决方案1】:

在 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++;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    相关资源
    最近更新 更多