Reading strings from a file in reverse order : File Read « File « C / ANSI-C

Home
C / ANSI-C
1.assert.h
2.Console
3.ctype.h
4.Data Structure Algorithm
5.Data Type
6.Development
7.File
8.Function
9.Language Basics
10.Macro Preprocessor
11.Math
12.math.h
13.Memory
14.Pointer
15.setjmp.h
16.signal.h
17.Small Application
18.stdio.h
19.stdlib.h
20.String
21.string.h
22.Structure
23.time.h
24.wctype.h
C Tutorial
C++
C++ Tutorial
Visual C++ .NET
C / ANSI-C » File » File ReadScreenshots 
Reading strings from a file in reverse order

/*
Beginning C, Third Edition
 By Ivor Horton
 ISBN: 1-59059-253-0
 Published: Apr 2004
 Publisher: apress

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STRING_LENGTH 1000
#define BUFFER_SIZE 50

/* Global variables */
FILE *pInFile = NULL;                   /* File pointer to input file     */
FILE *pOutFile = NULL;                  /* File pointer  to output file   */
char *infilename = "C:\\myfile.txt";    /* Name of the file to be read    */
char *outfilename = "C:\\outfile.txt";  /* Name of the file to be written */
char *buffer = NULL;
size_t buffer_size = BUFFER_SIZE;


void main()
{
  size_t str_length = 0;
  int str_count = 0;
  fpos_t *positions = NULL;
  int i = 0;

  buffer = (char*)malloc(buffer_size);            /* Create initial buffer */

  if((pInFile = fopen(infilename, "r")) == NULL)  /* Open the input file   */
  {
    printf("Error opening %s for reading. Program terminated.", infilename);
    abort();
  }

  /* Find out how many strings there are */
  for(;;)
  {
    fread(&str_length, sizeof(size_t)1, pInFile);  /* Read the string length */
    if(feof(pInFile))                                /* If it is end of file   */
      break;                                         /* We are finished        */

    /* Check buffer is large enough and increase if necessary */
    if(str_length>buffer_size)
    {
      buffer_size = str_length+1;
      free(buffer);
      buffer = (char*)malloc(buffer_size);
    }
    fread(buffer, str_length, 1, pInFile);   /* Read the string */
    ++str_count;
  }
  printf("\nThere are %d strings in the input file.", str_count);

  /* Now get the position for the beginning of each record in the file */
  /* The buffer is now large enough to hold the longest string         */
  rewind(pInFile);
  positions = (fpos_t*)malloc(str_count*sizeof(fpos_t));  /* Array to store the positions */
  for(i = ; i<str_count ; i++)
  {
    fgetpos(pInFile, positions+i);                    /* Get the positions      */
    fread(&str_length, sizeof(size_t)1, pInFile);   /* Read the string length */
    fread(buffer, str_length, 1, pInFile);            /* Read the string        */
 }

  /* Open the output file */
  if((pOutFile = fopen(outfilename, "w")) == NULL)
  {
    printf("Error opening %s for reading. Program terminated.", outfilename);
    abort();
  }

  /* Read the records in reverse order from the input file and write to the new file */
  for(i = ; i<str_count ; i++)
  {
    fsetpos(pInFile, positions+str_count-i-1);            /* Set the file position  */
    fread(&str_length, sizeof(size_t)1, pInFile);       /* Read the string length */
    fwrite(&str_length, sizeof(size_t)1, pOutFile);     /* Write to new file      */
    fread(buffer, str_length, 1, pInFile);                /* Read the string        */
    fwrite(buffer, str_length, 1, pOutFile);              /* Write to new file      */
  }

  fclose(pInFile);                                        /* Close input file  */
  fclose(pOutFile);                                       /* Close output file */
  printf("\nNew file write complete\n");

  /* List contents of output file */
  if((pOutFile = fopen(outfilename, "r")) == NULL)        /* Open the new file to read it */
  {
    printf("Error opening %s for reading. Program terminated.", outfilename);
    abort();
  }
  printf("\nThe strings in the new file are:");
  for(i = ; i<str_count ; i++)
  {
    fread(&str_length, sizeof(size_t)1, pOutFile);
    fread(buffer, str_length, 1, pOutFile);
    buffer[str_length'\0';
    printf("\n%s", buffer);
  }
  printf("\n");
  fclose(pOutFile);                                    /* Close file */

  /* Free the memory we allocated */
  if(buffer != NULL)
    free(buffer);
  if(positions != NULL)
    free(positions);
 }

 

           
       
Related examples in the same category
1.Messing about with formatted file I/OMessing about with formatted file I/O
2.Viewing the contents of a file
3.Search a set of numbers
4.Count the number of characters in a file
5.Open a file and read its content using fgetc
6.Get string from file
7.fscanf() - fprintf() example
8. Get the next int value from a stream: how to use getw and putw
9. Get the next character from a stream: how to use fgetc
10. Get a string from a stream: how to use fgets Get a string from a stream: how to use fgets
11. Read formatted data from a stream: how to use fscanf
12. Get the next character: how to use getc
13.Create a file, write content and read the content
14.Reset the file reader pointer
15. Check for errors: How to use ferror
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.