Esempio n. 1
0
int main(int argc, char *argv[])
/******************************************************************************/
{
  /* Array of pointers to read functions */
  int (*GetLine[3])(FILE*, char*) = { T_fscanf, T_fgets, T_fgetc };

  int   iReadMode;               /* Index into *GetLine[] array */
  int   iReadReturn;             /* Result of read function */
  int   isFilePosErr;            /* Boolean indicating file offset error */
  long  lFileLen;                /* Length of file */
  long  lLastFilePos;            /* Byte offset of end of previous line */
  long  lLineCount;              /* Line count accumulator */
  long  lLineLen;                /* Length of current line */
  long  lThisFilePos;            /* Byte offset of start of current line */
  char  szReadLine[MAX_REC_LEN]; /* Input buffer */
  FILE *inputFilePtr;            /* Pointer to input file */

  if (argc < 4)                  /* All arguments are required */
  {
    syntax();
    return 1;
  }

  /* Set index into function pointer array based on command line */
  if (strcmp(argv[3], "fscanf") == 0)
    iReadMode = 0;

  else if (strcmp(argv[3], "fgets") == 0)
    iReadMode = 1;

  else if (strcmp(argv[3], "fgetc") == 0)
    iReadMode = 2;

  else if (strcmp(argv[3], "fread") == 0)
    iReadMode = 3;

  else /* Oops */
  {
    syntax();
    return 1;
  }

  if (strcmp(argv[2], "t") == 0)
    inputFilePtr = fopen(argv[1], "r");  /* Open in TEXT mode */

  else if (strcmp(argv[2], "b") == 0)
    inputFilePtr = fopen(argv[1], "rb"); /* Open in BINARY mode */

  else /* Oops */
  {
    syntax();
    return 1;
  }

  if (inputFilePtr == NULL )             /* Could not open file */
  {
    printf("Error opening %s: %s (%u)\n", argv[1], strerror(errno), errno);
    return 1;
  }

  fseek(inputFilePtr, 0L, SEEK_END);     /* Position to end of file */
  lFileLen = ftell(inputFilePtr);        /* Get file length */
  rewind(inputFilePtr);                  /* Back to start of file */

  PrintHeader(argv, lFileLen);           /* Print the header info */

  /*
  *  The implementation of the fread() function in this program is
  *  different enough from the other read methods that it's easiest
  *  to just call it explicitly and quit.
  */

  if (iReadMode == 3)                    /* Use fread() */
  {
    iReadReturn = T_fread(inputFilePtr); /* Read the file and print output */
    fclose(inputFilePtr);                /* Close it */
    HR(80);                              /* Print a separator line */
    return (iReadReturn ? 0 : 1);        /* Exit with success or error code */
  }

  /* At this point, we'll be using fscanf(), fgets(), or fgetc() */

  lLineCount   =  0L; /* No lines read yet */
  lLastFilePos = -1L; /* So first line doesn't show an offset error */

  while (1)
  {
    isFilePosErr = 0;                   /* Clear error flag */
    lThisFilePos = ftell(inputFilePtr); /* Offset of start of line */
                                           /* This will not necessarily be */
                                           /* the absolute file offset if  */
                                           /* the file is opened in TEXT   */
                                           /* mode.                        */

    if (lThisFilePos != lLastFilePos + 1)  /* Set error flag if not next byte */
      isFilePosErr = 1;

    szReadLine[0] = '\0';                  /* Clear buffer for next line */

    /* Read the next line with the appropriate read function */
    iReadReturn = (*GetLine[iReadMode])(inputFilePtr, szReadLine);

    if (iReadReturn < 0)  /* Error reading line */
    {
      /*
      *  Any system error code generated in the read functions is returned
      *  as a negative number so we can use positive numbers to indicate
      *  success. Therefore, we need to 're-negative' it to convert it back
      *  to the original error code.
      *
      *  Error codes are implentation-dependent, but as far as I know, they
      *  are always positive integers.
      */

      printf("Error reading %s: %s (%u)\n",
             argv[1], strerror(-errno), -errno);
      break;
    }

    lLineLen = strlen(szReadLine); /* Get length of line */

    if (lLineLen)                  /* Got some data */
    {
      ++lLineCount;                /* Increment line counter */

      /* Print the line's detail */
      PrintLine(szReadLine, lLineCount, lLineLen,
                lThisFilePos, &lLastFilePos, isFilePosErr);
    }

    if (iReadReturn == 0)          /* End of file reached */
    {
      lThisFilePos = ftell(inputFilePtr); /* EOF offset */
      HR(80); /* Print a separator line */
      printf("EOF at offset %#x (dec %ld)\n", (int)lThisFilePos, lThisFilePos);
      break;
    }

  }/* end while (1) */

  fclose(inputFilePtr); /* Close the file */
  HR(80);               /* Print a separator line */
  return 0;             /* Exit with success code */

} /* end main() */
Esempio n. 2
0
int main(int argc, char *argv[])
/******************************************************************************/
{
	int bool_val = 0;					// bool for input val
	int   iReadReturn;             /* Result of read function */
	long  lFileLen;                /* Length of file */
	char decisionBool[4];
	char somestring[100];
	FILE *inputFilePtr;             /* Pointer to input file */
	FILE *fp;

	//inputText  = (char *)malloc(sizeof(char));
	//targetFile = (char *)malloc(sizeof(char));
	//keyword    = (char *)malloc(sizeof(char));
	//inputText = keyword;
	printf("   This script will read in an input text file that you will specify and will\nwrite each line of text data with has a certainkeyword into an output text file\nthat you also will specify.\n\n");
	printf("   Note that this keyword must be modified in this script ('read.C')(it is one\nof the first lines of code you will see).\n\n");
	printf("   To begin, please press enter.\n\n");
	getchar();
	//const char *keyword = keywordPrompt();
	//char *keyword = "test";
	char tmp_key[99]; 	  // Keyword definition, var for storing keyword
	char tmp_input[99];
	char tmp_output[99];
	char *keyword;
	char *inputText;
	char *targetFile;
	//char *keyword;
	// INPUT KEYWORD====================================================
	while (bool_val == 0){
	
		printf("Please type in your keyword:\n");
		fgets(somestring, sizeof(somestring)-1, stdin);
		sscanf(somestring, "%s", &tmp_key[0]);
		keyword = &tmp_key[0];
		
		printf("Your keyword is: '%s'\n      Do you wish to keep this value? [Y/n] : \n", keyword);
		fgets(decisionBool, sizeof decisionBool, stdin);
		strtok(decisionBool, "\n"); /* remove a newline if present */

		//strcmp(decisionBool, "n") ? keywordPrompt() : printf("no match w/  n\n");
		bool_val = strcmp(decisionBool, "n")==0 ? 0 : 1;
	}
	
	bool_val = 0;		//reset bool
	printf("      keeping keyword value '%s'\n\n", keyword);
	// print address
	//printf("Address of key_ptr: %p\n", keyword);
	//keyword    = keywordPrompt();
	while (bool_val == 0){
	
		printf("Please type in the name of your input text file (where to read raw data from):\n");
		fgets(somestring, sizeof(somestring)-1, stdin);
		sscanf(somestring, "%s", &tmp_input[0]);
		inputText = &tmp_input[0];
		
		printf("Your keyword is: '%s'\n      Do you wish to keep this value? [Y/n] : \n", inputText);
		fgets(decisionBool, sizeof decisionBool, stdin);
		strtok(decisionBool, "\n"); /* remove a newline if present */

		//strcmp(decisionBool, "n") ? keywordPrompt() : printf("no match w/  n\n");
		bool_val = strcmp(decisionBool, "n")==0 ? 0 : 1;
	}
	printf("      keeping inputText value '%s'\n\n", inputText);
	bool_val = 0; // reset bool
	
	
	//targetFile =  outputTextPrompt();
	//printf("This is the targtetFile directory: %s\n", targetFile);
	while (bool_val == 0){
	
		printf("Please type in the name of your input text file (where to read raw data from):\n");
		fgets(somestring, sizeof(somestring)-1, stdin);
		sscanf(somestring, "%s", &tmp_output[0]);
		targetFile = &tmp_output[0];
		
		printf("Your keyword is: '%s'\n      Do you wish to keep this value? [Y/n] : \n", targetFile);
		fgets(decisionBool, sizeof decisionBool, stdin);
		strtok(decisionBool, "\n"); /* remove a newline if present */

		//strcmp(decisionBool, "n") ? keywordPrompt() : printf("no match w/  n\n");
		bool_val = strcmp(decisionBool, "n")==0 ? 0 : 1;
	}
	printf("      keeping targetFile value '%s'\n\n", targetFile);
	//bool_val = 0; // reset bool



	printf("This is the keyword: %s\n", keyword);	
	printf("This is the inputText directory: %s\n", inputText);
	printf("This is the targtetFile directory: %s\n", targetFile);

	printf("This is keyword's address: %p\n", &keyword);
	printf("This is inputText's address: %p\n", &inputText);
	printf("This is targetText's address: %p\n", &targetFile);


	argc = 4;
	argv[0] = "read.c";
	argv[1] = inputText;
	//argv[1] = "test.txt";
	argv[2] = "t";
	argv[3] = "fread";
	// appending argv[4] - target file for writi	ng
	//argv[4] = "target_test2.txt";
	//argv[4] = outputText;
	
	// define keyword

  if (argc < 4)                  /* All arguments are required */
  {
    syntax();
    return 1;
  }
		
  if (strcmp(argv[2], "t") == 0)
    inputFilePtr = fopen(argv[1], "r");  /* Open in TEXT mode */

  else if (strcmp(argv[2], "b") == 0)
    inputFilePtr = fopen(argv[1], "rb"); /* Open in BINARY mode */

  else /* Oops */
  {
    syntax();
    return 1;
  }

  if (inputFilePtr == NULL )             /* Could not open file */
  {
    printf("Error opening %s: %s (%u)\n", argv[1], strerror(errno), errno);
    return 1;
  }

  fseek(inputFilePtr, 0L, SEEK_END);     /* Position to end of file */
  lFileLen = ftell(inputFilePtr);        /* Get file length */
  rewind(inputFilePtr);                  /* Back to start of file */


  /*
  *  The implementation of the fread() function in this program is
  *  different enough from the other read methods that it's easiest
  *  to just call it explicitly and quit.
  */

	//create outputText file if it doesn't exist
	if(file_exists(targetFile)==0){
		printf("file %s didn't exist - should exist now\n", targetFile);
		fp = fopen (targetFile, "w+");
		fclose(fp);
	}

	PrintHeader(argv, lFileLen);           /* Print the header info */
	//const char *keyword;
	//keyword = keywordPrompt();
	//keyword = "test";
  
    iReadReturn = T_fread(inputFilePtr, keyword, targetFile); /* Read the file and print output */
    fclose(inputFilePtr);                /* Close it */
    HR(80);                              /* Print a separator line */
    //free(inputText);
    //free(targetFile);
    //free(keyword);
    return (iReadReturn ? 0 : 1);        /* Exit with success or error code */
  
} /* end main() */