Tuesday, October 28, 2014

C text file program



Replace a specified Line in a Text File

#include 
#include 
int main(void)
{
    FILE *fileptr1, *fileptr2;
    char filechar[40];
    char c;
    int delete_line, temp = 1;
 
    printf("Enter file name: ");
    scanf("%s", filechar);
    fileptr1 = fopen(filechar, "r");
    c = getc(fileptr1);
    while (c != EOF)
    {
        printf("%c", c);
        c = getc(fileptr1);
    }
    printf(" \n Enter line number to be deleted and replaced");
    scanf("%d", &delete_line);
       rewind(fileptr1);
       fileptr2 = fopen("replica.c", "w");
    c = getc(fileptr1);
    while (c != EOF)
    {
        if (c == 'n')
        {
            temp++;
        }
        
        if (temp != delete_line)
        {
            putc(c, fileptr2);
        }
        else
        {
            while ((c = getc(fileptr1)) != 'n')
            {
            }
            
            printf("Enter new text");
            
            fflush(stdin);
            putc('n', fileptr2);
            while ((c = getchar()) != 'n')
            putc(c, fileptr2);
            fputs("n", fileptr2);
            temp++;
        }
                c = getc(fileptr1);
    }
    fclose(fileptr1);
    fclose(fileptr2);
    remove(filechar);
    rename("replica.c", filechar);
    fileptr1 = fopen(filechar, "r");
    c = getc(fileptr1);
    
    while (c != EOF)
    {
        printf("%c", c);
        
        c = getc(fileptr1);
    }
    fclose(fileptr1);
    return 0;
}

Output
 
Enter file name: pgm3.c
 
#include 
#include 
#include 
 
int main()
{
 
    long int octal, decimal = 0;
    int i = 0;
 
    printf("Enter any octal number: ");
    scanf("%ld", &octal);
    while (octal != 0)
    {
        decimal =  decimal +(octal % 10)* pow(8, i++);
        octal = octal / 10;
    }
    printf("Equivalent decimal value: %ld",decimal);
    return 0;
}
 
Enter line number to be deleted and replaced 13 replaced
Enter new text
 
#include 
#include 
#include 
 
int main()
{
 
    long int octal, decimal = 0;
    int i = 0;
 replaced
    printf("Enter any octal number: ");
    scanf("%ld", &octal);
    while (octal != 0)
    {
        decimal =  decimal +(octal % 10)* pow(8, i++);
        octal = octal / 10;
    }
    printf("Equivalent decimal value: %ld",decimal);
    return 0;
}
 
Find the Number of Lines in a Text File
 
#include 
#include  
int main()
{
    FILE *fileptr;
    int count_lines = 0;
    char filechar[40], chr;
    clrscr();
    printf("Enter file name: ");
    scanf("%s", filechar);
    fileptr = fopen(filechar, "r");
    chr = getc(fileptr);
    while (chr != EOF)
    {
        
        if (chr == 'n')
        {
            count_lines = count_lines + 1;
        }
        
        chr = getc(fileptr);
    }
    fclose(fileptr); 
    printf("There are %d lines in %s  in a file\n", count_lines, filechar);
    return 0;
}

Output
 
Enter file name: pgm2.c
There are 43 lines in pgm2.c  in a file
 
Append the Content of File at the end of Another
 
#include 
#include 
#include 
 
 void main()
{
    FILE *fsring1, *fsring2, *ftemp;
    char ch, file1[20], file2[20], file3[20];
 
    printf("Enter name of first file ");
    gets(file1);
    printf("Enter name of second file ");
    gets(file2);
    printf("Enter name to store merged file ");
    gets(file3);
    fsring1 = fopen(file1, "r");
    fsring2 = fopen(file2, "r");
    if (fsring1 == NULL || fsring2 == NULL)
    {
        perror("Error has occured");
        printf("Press any key to exit...\n");
        exit(Exit_Failure);
    }
    ftemp = fopen(file3, "w");
    if (ftemp == NULL)
    {
        perror("Error has occures");
        printf("Press any key to exit...\n");
        exit(EXIT_FAILURE);
    }
    while ((ch = fgetc(fsring1)) != EOF)
        fputc(ch, ftemp);
    while ((ch = fgetc(fsring2) ) != EOF)
        fputc(ch, ftemp);
    printf("Two files merged  %s successfully.\n", file3);
    fclose(fsring1);
    fclose(fsring2);
    fclose(ftemp);
    return 0;
}

Output
 
Enter name of first file a.txt
Enter name of second file b.txt
Enter name to store merged file merge.txt
            Two files merged merge.txt successful

No comments:

Post a Comment