Is there a more efficient way of reading a formatted file in C than I have done?

I need to read in a formatted file that looks something like this. Code: HARK Name: Oscar MRTE: Train etc At the moment my code looks like this. FILE *file; char unneeded[10]; char whitespace[2]; char actual[10]; file = fopen("scannertest.txt","r"); fscanf(file,"%s",unneeded); // this is the identifier and the colon (code:) fscanf(file,"%[ ]",whitespace); // this takes in the white space after the colon. fscanf(file,"%s",actual); // this is the value I actually need. /** * Do stuff with the actual variable **/ fclose(file); This way works for me but I don't think writing three fscanf()'s for each line in the text file is the best way to do it, especially as I will be doing it in a loop later. I tried doing it like this: fscanf(file, "%s %[ ] %s",unneeded,whitespace,real); However this gave me weird symbols when I tried printing the output.

以上就是Is there a more efficient way of reading a formatted file in C than I have done?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏