运行时C文件读取分段错误

时间:2019-05-18 21:08:55

标签: c

在这里我遇到了细分错误。我应该读至少20行。可能是fread()是一个不错的选择。 fgets()似乎正在创建运行时错误。如果有人陷入此类问题并尽早解决,那就更好了。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>

int main(void){
    //OPEN THE FILE HANDLE
    char* FileName=(char*)"xab";
    FILE *FP= fopen(FileName, "rb");
    if(FP!= NULL){
        int KeyCount=0;
        int ENDPresent=0;
        char Line[128];
        //long FilePosition=0;
        char KVSeparator[]=":";
        const char* Keys[]={"AIRBUS GNSS", "Header version", "Receiver name", "Firmware version", "No. Channels", "Sample rate", "Channel bandwidth", "Frequency band", "Bitwidth", "IQ order", "Raw samples", "Timestamp", "Comment", "END"};

        //SET THE FILE POINTER AT THE VERY BEGINNING OF THE FILE
        //fseek(FP, 0, SEEK_SET);
        while(fgets(Line, sizeof(Line), FP)!= NULL){
            char *Splice = strtok(Line, KVSeparator);
            //STOP READING LINES IF 'END' IS READ IN A LINE
            if(strcmp(Splice, "END")==0){
                ENDPresent=1;
                break;
            }

            //COMPARE WITH KEYWORDS USUALLY COME IN AS HEADER
            int i=0;
            while(Keys[i]){
                if(strcmp(Keys[i], Splice) == 0){
                    KeyCount++;
                    break;
                }
                i++;
            }
            //PRINT THE LINE AS ITS GOING TO BE READ
            //fputs(Splice, stdout);
        }
    }else{
        perror(FileName);
        printf("something went wrong: %s", strerror(errno));
    }
}

0 个答案:

没有答案