Exemple #1
0
/*!
 * @function parseLines_array
 * Opens [file] and reads all lines from it, returning an array of strings,
 * each element being a line.
 * @param file
 * The filename of the file to be read.
 * @return
 * An array of strings containing the lines.
 */
Array *parseLines_array(const char * const file)
{
	const char * const fname = "newParseLine";
	FILE *fd;
	char *line, **result;
	Array *lines;

	/* open file */
	fd = fopen(file, "r");
	if (NULL == fd) {
		Debug_out(DEBUG, "%s: '%s': %s.\n", fname, file, strerror(errno));
		goto error;
	}

	/* create array */
	lines = Array_newString(LINES_DEFAULT, LINES_INCREASE);
	if (NULL == lines) {
		Debug_out(DEBUG, "%s: unable to create new Array.\n", fname);
		goto clean1;
	}

	/* read the file */
	do {
		line = fileGetLine(fd);
		if (NULL != line) {
			Array_add(lines, &line);
		}
	} while (NULL != line);

	return lines;

clean2:	Array_free(&lines);
clean1:	fclose(fd);
error:	return NULL;
}
Exemple #2
0
int main()
{
  int i, j;
  char *line = NULL;
  char *left = NULL;
  char *right = NULL;
  char *aword = NULL;
  char **shufflewords = NULL;
  int readlines;
  int irekaenum;
  struct Lines lines;
  lines.length = 0;
  lines.line = NULL;
  for (i = 0; (line = fileGetLine(stdin)) != NULL; i++)
    {
      lines.length += 1;
      lines.line = (char**)realloc (lines.line, sizeof(char*) * lines.length);
      lines.line[i] =  line;
    }
  i = 0;
  while (strcmp("-", lines.line[i]) != 0)
    {
      aword = (char*)malloc (sizeof(char) * (strlen(lines.line[i]) + 1));
      strcpy(aword, lines.line[i]);
      i++;
      readlines = atoi (lines.line[i]);
      i++;
      for (j = 0; j < readlines; j++)
	{
	  irekaenum = atoi(lines.line[i+j]);
	  right = (char*)malloc (sizeof(char) * (strlen(aword) + 1));
	  left = (char*)malloc (sizeof(char) * (strlen(aword) + 1));
	  strcpy(right, "");
	  strcpy(left, "");
	  strncpy(left, aword, irekaenum);
	  left[irekaenum] = '\0';
	  strcpy(right, aword + irekaenum);
	  strcat(right, left);
	  strcpy(aword, right);
	  free(left);
	  free(right);
	}
      i += j;
      printf("%s\n",aword);
      free(aword);
    }

  for(i = 0; i < lines.length; i++)
    {
      free(lines.line[i]);
    }

  free(lines.line);
  return 0;
}
Exemple #3
0
int main()
{
  int i;
  char *line = NULL;
  char **words;
  char *fastword;
  int jufukuword = 0;
  struct Lines lines;
  lines.length = 0;
  lines.line = NULL;

  for (i = 0; (line = fileGetLine(stdin)) != NULL; i++)
    {
      lines.length += 1;
      lines.line = (char**)realloc (lines.line, sizeof(char*) * lines.length);
      lines.line[i] =  line;
    }

  words = split(&lines);
  lower(words);

  fastword = words[0];
  for (i = 1; strcmp("END_OF_TEXT", words[i]) != 0; i++)
    {
      if(strcmp(fastword, words[i]) == 0)
	{
	  jufukuword++;
	}
    }

  printf("%d\n", jufukuword);
  for(i = 0; i < lines.length; i++)
    {
      free(lines.line[i]);
    }

  free(lines.line);
  return 0;
}