Tuesday, November 16, 2010

File Read/Write through C/C++


A few of my friends keep asking me about working with C/C++ , and consider me to have a good command over the language ( C++ ). However, this is not completely true, even though I am practicing it for past 5 years, there are still is a lot of thing to be known. 
The First and foremost thing to be comfortable with any language is to get used to working on its IDE (Integrated Development Environment), just like we learn how to float before we learn to swim. For C++ , the Turbo C++ IDE is quite common, and very good for practicing.
And of course practicing it regularly is the most important aspect of learning. One may read hundreds of books remember thousands of things, but that is different from actually practicing it.
So coming to the point. There was a problem to be solved using C language, in a test conducted by a renowned IT Company. The problem statement was:
"Write a Program in C, which should
  1. Read a text file, say "input.txt"
  2. Cut of the extra white spaces.
  3. Write it again to another file, say "output.txt". "
It was to be solved within 15-20 minutes, and most of the aspirants including me could not complete the task. After that my friend and I found a correct solution to the problem.
To solve the problem it may be divided into three parts, and the complete solution can be obtained by putting them together.


PART I: To read a file.
Firstly, make use of FILE structure that acts as a link between your program and the operating system.
(A FILE structure can store the details of the file being used.)
Then declare two FILE structure pointer variables as you need to open 2 files(one for input and the other for output. A FILE pointer variable can store the address of the structure FILE which is found in header file "stdio.h". And to open the file(s) ( input and output ) function fopen("filename","mode") can be used. It returns File pointer variable ). Syntax:  FILE *p; p=fopen("abc.txt","rt");

PART II: Get characters from the opened file.
To get a character from the text file, the function fgetc() can used. ( Its details can be known by taking the cursor to fgetc() in Turbo C++ IDE and pressing Ctrl+F1.)
And once character ch=fgetc() is fetched, logical statements can be used to decide whether the character is to be kept in the output file or not. Then comes the next part that is writing it to another file.

PART III: Write in the output file character by character.
For this the file which the same FILE structure pointer (a new one) can be used and the function fputc() can be used to write the character.

After combining all these parts, the program required is ready!


# include "stdio.h"
# include "stdlib.h"
void main()
{
    FILE *input,*output;
    char ch;
    input = fopen("input.txt","rt");
    output = fopen("output.txt","wt");
    if(input ==NULL)
    {
        printf("file can not open.");
        exit(1);
    }
    int count=0;
    do
    {
        ch=fgetc(input);
        if(ch==' ')
        {   count++;
            if(count==1)
             { fputc(ch,sec);
             }
        }
        else
        {
          fputc(ch,output);
          count=0;
        }
    }while(ch !=EOF);
    fclose(input);
    fclose(output);
}

No comments:

Post a Comment