[问题] 读出档案 fread

楼主: EngRookie (EngRookie)   2016-03-15 14:12:57
开发平台(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 档需要被不同的程式读取
请问板大们要如何根据项目字串的不同将后面的的值单独读取出来呢?
这困扰我一阵子,所以上来问ㄧ下大家的意见,谢谢大家: )
作者: suwako   2016-03-15 14:31:00
用strstr应该可以达到你要的效果
楼主: EngRookie (EngRookie)   2016-03-15 17:27:00
谢谢 已解决 : )

Links booklink

Contact Us: admin [ a t ] ucptt.com