Tuesday, October 28, 2014

C lines file program



Join Lines of Two given Files and Store them in a New file

#include 
#include 
int joinfiles(FILE *, FILE *, FILE *);
 
char ch;
int flag;
 
void main(int argc, char *argv[])
{
    FILE *file1, *file2, *target;
    file1 = fopen(argv[1], "r");
    if (file1 == NULL)
    {
        perror("Error Occured!");
    }
    file2 = fopen(argv[2], "r");
    if (file2 == NULL)
    {
        perror("Error Occured!");
    }
    target = fopen(argv[3], "a");
    if (target == NULL)
    {
        perror("Error Occured!");
    }
 
    joinfiles(file1, file2, target);        
 
    if (flag == 1)
    {
        printf("The files have been successfully concatenated\n");
    }
}
 int joinfiles(FILE *file1, FILE *file2, FILE *target)
{
    while ((fgetc(file1) != EOF) || (fgetc(file2) != EOF))
    {
        fseek(file1, -1, 1);
        while ((ch = fgetc(file1)) != '\n')
        {
            if (ch == EOF)
            {
                break;
            }
            else
            {
                fputc(ch, target);
            }
        }
        while ((ch = fgetc(file2)) != '\n')
        {
            if (ch == EOF)
            {
                break;
            }
            else
            {
                fputc(ch, target);
            }
        }
        fputc('\n', target);
    }
    fclose(file1);
    fclose(file2);
    fclose(target);
    return flag = 1;
}

 
 
 
Output
 
            The files have been successfully concatenated
 
 #include 
#include 
int sum(int,  int);
void main()
{
    int num1, num2;
    printf("Enter Number1 and Number2:");
    scanf("%d %d ", num1, num2);
    sum(num1, num2);
}
 
int sum(int a, int b)
{
    return a + b;
}
 
#include 
#include 
int sqrt(int);
void main()
{
    int num;
    printf("enter the number:");
    scanf("%d", &num);
    sqrt(num);
    printf("The square of the given number is:", num);
}
int sqrt(int num)
{
    return num*num;
}
 
#include 
#include 
int sqrt(int);
{
int sum(int,  int);    int num;
void main()    printf("enter the number:");
{    scanf("%d", &num);
    int num1, num2;    sqrt(num);
    printf("Enter Number1 and Number2:");    printf("The square of the given number is:", num);
    scanf("%d %d ", num1, num2);}
    sum(num1, num2);int sqrt(int num)
}
{
    return num*num;
int sum(int a, int b)}
{
    return a + b;
}

Collect Statistics of a Source File like Total Lines, Total no. of Blank Lines, Total no. of Lines ending with Semicolon

#include 
#include 
#include 
 
void main(int argc, char *argv[])    
{
    int ncount = 0, ccount = 0, scount = 0, blank = 0;
    char ch;
    FILE *fp;
    fp = fopen(argv[1], "r");
    if (fp == NULL)
    {
        perror("Error Occured");
    }
    else
    {
        while(1)
        {
            ch = fgetc(fp);
            if (ch == EOF)
            {
                break;
            }
            if (ch == 10)
            {
                ncount++;
                if (ch = fgetc(fp) == '\n')
                {
                    fseek(fp, -1, 1);      
                    blank++;
                }
            }        
            else if (ch == 59)
            {
                scount++;
            }
            else if (ch == '/' || ch == '*')
            { 
                ccount++;
            }
        }
    }
    printf("\nThe Total number of lines are %d", ncount);
    printf("\nThe Total number of Commented lines are %d", ccount);
    printf("\nThe Total number of blank lines are %d", blank);
    printf("\nThe total number of lines that end with Semicolon %d", scount);
    printf("\nThe length of Actual code is %d ", ncount-blank-ccount);
    fclose(fp);
}
 
Output
 
The Total number of lines are 23
The Total number of Commented lines are 6
The Total number of blank lines are 4
The total number of lines that end with Semicolon 6
The length of Actual code is 13

No comments:

Post a Comment