【问题标题】:C: reading a file line by lineC:逐行读取文件
【发布时间】:2014-06-21 02:38:38
【问题描述】:

输入文件每行有一个条目,格式为 T S ,其中 T 是到达时间,S 是要读取的扇区。 我有一个 file.txt,它看起来像

1 6 2 7 3 8

表示扇区 6、7 和 8 的三个磁盘访问分别在时间 1、2 和 3 到达。

我如何解析它,以便第一个数字进入 T,第二个进入 S?

【问题讨论】:

标签: c parsing file-io scheduling


【解决方案1】:

有很多方法可以完成这项任务。

  • pmg 提到的 scanf()(或者 fscanf())方法可能最常被学术界教授给新程序员。
  • 由 doniyor 引用的 readLine() 方法是一种替代方法,它一次读取一个字符以组合成一个完整的行,然后可以将其解析为字符串。
  • 您甚至可以尝试使用 fgets() 读取整行,然后从行缓冲区中解析出数字。

上面的方法列表绝不是完整的。还有许多其他方法可以完成任务。

当然,您可能对这些常用方法有一个完整的工作理解,并且您正在寻找一些zing!也许以下内容会对您有所帮助你的方式:

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

int main(
      int I__argC,
      char *I__argV[]
      )
   {
   int rCode=0;
   struct stat statBuf;
   FILE *fp = NULL;
   char *fileBuf = NULL;
   size_t fileBufLength;
   char *cp;

   /* Verify that caller has supplied I__argV[1], which should be the path
    * to the datafile.
    */
   if(I__argC != 2)
      {
      rCode=EINVAL;
      fprintf(stderr, "Usage: %s {data file path}\n", I__argV[0]);
      goto CLEANUP;
      }

   /* Get the size of the file (in bytes);
    * (assuming that we are not dealing with a sparse file, etc...)
    */
   errno=0;
   if((-1) == stat(I__argV[1], &statBuf))
      {
      rCode=errno;
      fprintf(stderr, "stat(\"%s\", ...) failed.  errno[%d]\n", I__argV[1], errno);
      goto CLEANUP;
      }

   /* Open the caller-specified file in read mode. */
   errno = 0;
   fp=fopen(I__argV[1], "r");
   if(NULL == fp)
      {
      rCode=errno;
      fprintf(stderr, "fopen(\"%s\", \"r\") failed.  errno[%d]\n", I__argV[1], errno);
      goto CLEANUP;
      }

   /* Allocate a buffer large enough to hold the entire file content. */
   fileBuf=malloc(statBuf.st_size);
   if(NULL == fileBuf)
      {
      rCode=ENOMEM;
      fprintf(stderr, "malloc(%zd) failed.\n", statBuf.st_size);
      goto CLEANUP;
      }

   /* Read the file into the fileBuf. */
   errno=0;
   fileBufLength=fread(fileBuf, 1, statBuf.st_size, fp);
   if(fileBufLength != statBuf.st_size)
      {
      rCode=errno;
      fprintf(stderr, "fread() failed to read %zd file bytes.  errno[%d]\n",
            statBuf.st_size - fileBufLength, errno);
      goto CLEANUP;
      }

   /* Parse the fileBuf for specific data. */
   for(cp=fileBuf; cp < fileBuf + fileBufLength; ++cp)
      {
      long arrivalTime;
      long sector;

      /* Skip leading white-space (if any) */
      if(isspace(*cp))
         continue;

      /* Parse the 'arrival time'. */
      arrivalTime=strtol(cp, &cp, 10);

      /* Skip leading white-space (if any) */
      while(isspace(*cp))
         ++cp;

      /* Parse the 'sector'. */
      sector=strtol(cp, &cp, 10);

      printf("%ld, %ld\n", arrivalTime, sector);
      }


CLEANUP:

   /* Free the fileBuf (if it was successfully allocated) */
   if(fileBuf)
      free(fileBuf);

   /* Close the file (if it was successfully opened) */
   if(fp)
      fclose(fp);

   return(rCode);
   }

【讨论】:

    猜你喜欢
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 2022-01-25
    • 2015-05-19
    相关资源
    最近更新 更多