-3

Input files have one entry per line and be of the form T S where T is the arrival time and S is the sector to be read. I have a file.txt where it looks like

1 6 2 7 3 8

Would indicate three disk accesses for sectors 6, 7, and 8 arriving at time 1 2 and 3 respectively.

how do i parse it so that the first number goes to T and the second goes to S?

  • 3
    try `if (scanf("%d%d", &t, &s) != 2) /* error */;` possibly in a loop – pmg May 04 '14 at 21:06
  • look at here: http://stackoverflow.com/questions/3501338/c-read-file-line-by-line – doniyor May 04 '14 at 21:07
  • 2
    I count this as zero-research effort. What have you tried? Read some tutorial about c IO and you will know everything. – luk32 May 04 '14 at 21:12

1 Answers1

0

There are many ways to accomplish this task.

  • The scanf() (or perhaps fscanf()) method mentioned by pmg is perhaps most commonly taught to new programmers by academia.
  • The readLine() method, referred to by doniyor is an alternate approach which reads one character at a time to assemble a complete line, which then can be parsed as a string.
  • You might even try using fgets() to read an entire line, and then parse the numbers out of the line buffer.

The above list of methods is by no means complete. There are many other ways to accomplish the task.

Of course, you have probably have a full working understanding of these common methods, and you are looking for something with some zing! Perhaps the following will help you on your way:

#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);
   }
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28