Skip to content

Instantly share code, notes, and snippets.

@kgashok
Last active December 7, 2016 17:24
Show Gist options
  • Save kgashok/07839bbcfbd6da7fe26d9e37b10eeac1 to your computer and use it in GitHub Desktop.
Save kgashok/07839bbcfbd6da7fe26d9e37b10eeac1 to your computer and use it in GitHub Desktop.
fileOperations.md

###Table of Contents

[TOC]

Read from one file and write to another file

#include <stdio.h>

int main(int argc, char** argv) {

   /* STEP 1 */ 
   //open the necessary file pointers
   FILE* fileIn  = fopen(argv[1], "r");
   FILE* fileOut = fopen(argv[2], "w"); 
   
   /* STEP 2 */
   // read from 'fileIn' and write to 'fileOut'
   char c;
   while((c = fgetc(fileIn)) != EOF) {
      fputc (c, fileOut); 
   }
   
   /* STEP 3 */
   // clean up by closing all the file pointers
   fflush(fileOut);
   fclose(fileOut);
   fclose(fileIn);
   return 0;
}

Count number of lines in a file

#include<stdio.h>

void main(int argc, char** argv)
{
    FILE *fp;
    char ch;
    int character = 0, space = 0, tab = 0, line = 0;
    fp = fopen(argv[1],"r");
    if(fp == NULL) {
        printf("File Not Found\n");
        exit(1);
    }
    else {
        while(1) {
            ch = fgetc(fp);
            if(ch == EOF)
                break;
            character++;
            if(ch == ' ')
                space++;
            else if(ch == '\t')
                tab++;
            else if(ch == '\n')
                line++;
        }
    }
    printf("\nNumber of Characters = %d\n", character);
    printf("\nNumber of Tabs       = %d\n", tab);
    printf("\nNumber of New Lines  = %d\n", line);
    printf("\nNumber of Spaces     = %d\n", space);
 
    // Using fseek and ftell to find size of file
    fseek(fp, 0, SEEK_END);  // SEEK_SET, SEEK_CUR are other options
    int len = ftell(fp); 
    fclose(fp);
    printf("\nSize of file         = %d\n", len);
}

Read and Sort numbers in a file and write to another file

#include <stdio.h>


struct student_info {
   char name[30]; 
   int marks;
} students[10];
typedef struct student_info Student; 

int cmpfunc (const void * a, const void * b) {
   Student *s1 = (Student *)a; 
   Student *s2 = (Student *)b; 

   //printf ("%d %d \n", s1->marks, s2->marks); 
   return s2->marks - s1->marks;
}


int main(int argc, char** argv) {

   /* STEP 1 */ 
   //open the necessary file pointers
   FILE* fileIn  = fopen(argv[1], "r");
   FILE* fileOut = fopen(argv[2], "w"); 
   
   /* STEP 2 */
   // read integers from 'fileIn'
   int count = 0;
   while((fscanf(fileIn, "%s %d\n", 
         students[count].name, &(students[count].marks) )) != EOF) {
      //fprintf (fileOut, "%d %d\n", x[i], x[i]);
      count++;
   }
   
   /* STEP 3 */
   // sort them into ascending order
   qsort (students, count, sizeof(struct student_info), cmpfunc); 

   /* STEP 4 */
   // write the sorted array into a new file
   int j;
   for (j = 0; j < count; j++)
      fprintf(fileOut, "%s\t%d\n", students[j].name, students[j].marks); 
      
      
   /* STEP 5 */
   // clean up by closing all the file pointers
   fflush(fileOut);
   fclose(fileOut);
   fclose(fileIn);
   return 0;
}

UnsortedSorted

"Find and Replace" tabs with spaces

This is an excellent example for use of fseek and ftell to accomplish random access into a file to replace all tabs into spaces.

#include <stdio.h>

int main(int argc, char** argv)
{
   FILE* filePtr = fopen(argv[1], "r+");
   char c;
   int i = 0, j = 0;
   int loc[100]; // to remember where tabs were found
   while((c = fgetc(filePtr)) != EOF) {
      if(c == '\t') {
         loc[j++] = ftell(filePtr)-1; 
      }
   }
   
   // print the offsets in the file where 
   // tabs were located 
   int k;
   for (k=0; k < j;k++) printf ("%d ", loc[k]); 
   printf ("\n"); 
   
   // replace all tabs with spaces 
   for (k = 0; k < j; k++) {
      fseek (filePtr, loc[k], 0);
      fputc (' ', filePtr); 
   }
   fflush(filePtr);
   fclose(filePtr);
   return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment