當我編譯代碼並運行它時,它說Segmentation Fault
。Linux中的分段錯誤
這裏是我的代碼:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int i;
char weather_value;
char weather_incoming[2000];
FILE *in;
in=fopen("home/pi/weather_project/weather_incoming.txt","r");
for(i=0;i<2000;i++)
{
fscanf(in,"%c",&weather_incoming[i]);
}
char* tagStartBegin = strstr(weather_incoming,"<pty");
char* tagStartEnd = strstr(tagStartBegin,">");
char* value = tagStartEnd+1;
char* tagEndBegin = strstr(tagStartEnd,"</pty>");
*tagEndBegin = '\0';
weather_value=*value;
printf("%c",weather_value);
fclose(in);
}
我必須找到從txt文件的天氣值,<pty>
和一個人幫我這個之間。當我在windows(codeblocks)上編譯它時,它工作得很完美,但是當我在linux(gcc)上編譯它時,它一直向我發送Segmentation Fault
。 我該如何解決這個問題? 我不知道什麼部分是錯誤的任何信息,因爲該錯誤信息只是Segmentation Fault
你應該使用一個調試器(在Linux上,有gdb)。 –
代碼有很多問題。文件句柄'in'將是錯誤的,你正在嘗試讀取它。你確定'家'而不是'/家'? – Jeyaram
用char'fscanf()'讀取char文件看起來很奇怪。我敢打賭,你的錯誤與此有關。改爲使用'fread()',並在一個讀取語句中讀取整個文件。我想這會做到這一點。 – oysteijo