[問題] 讀出檔案 fread

看板C_and_CPP作者 (EngRookie)時間8年前 (2016/03/15 14:13), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串1/1
開發平台(Platform): Raspbian (Linux) 額外使用到的函數庫(Library Used): stdio.h 問題(Question):讀檔 餵入的資料(Input): parameter.txt IP:192.168.1.11 DeviceType:C ModuleID:1 DelayTime1:1500 DelayTime2:8000 預期的正確結果(Expected Output):根據項目將參數讀入不同的環境中使用 錯誤結果(Wrong Output):none 程式碼(Code):(請善用置底文網頁, 記得排版) #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *pFile; char *buffer; pFile = fopen ( "parameter.txt" , "r" ); if (pFile==NULL) { printf("ERROR:fopen Failed\n"); return -1; } // obtain file size: int resultSeek = fseek (pFile , 0 , SEEK_END); if(resultSeek!=0) { printf("ERROR:fseek Failed\n"); return -1; } long lSize = ftell (pFile); printf("lSize = %d\n", lSize); rewind (pFile); // allocate memory to contain the whole file: buffer = (char*) malloc (sizeof(char)*lSize); if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);} // copy the file into the buffer: unsigned int resultFread = fread (buffer, sizeof(char), lSize, pFile); printf("resultFread = %d\n", resultFread); //if (result != lSize) {fputs ("Reading error",stderr); exit (3);} /* the whole file is now loaded in the memory buffer. */ printf("buffer = %s\n", buffer); // terminate fclose (pFile); free (buffer); return 0; } 補充說明(Supplement): 我目前可以把txt全部讀入 char *buffer 中,這個 txt 檔需要被不同的程式讀取 請問板大們要如何根據項目字串的不同將後面的的值單獨讀取出來呢? 這困擾我一陣子,所以上來問ㄧ下大家的意見,謝謝大家: ) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.220.255.40 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1458022380.A.90B.html

03/15 14:31, , 1F
用strstr應該可以達到你要的效果
03/15 14:31, 1F

03/15 17:27, , 2F
謝謝 已解決 : )
03/15 17:27, 2F
文章代碼(AID): #1MvwViaB (C_and_CPP)