Count number of lines and file handling in text file coderinme

function to count number of lines and file handling in text file.

This function also displays vowel starting words.Assuming that a text file named TEXT1.TXT already contains some text written into it so it should also have a function named vowel_words(), that reads the file TEXT1.TXT and creates a new file named TEXT2.TXT , which shall contain only those words from the file TEXT1.TXT which don’t start with an uppercase vowel.

let’s begin
#include<stdio.h>
#include<string.h>
#include<ctype.h>
FILE *outp,*inpt;
void count(char a[])
{
inpt=fopen("text1.text","r");
int i=0,d=0;
char c;
do
{
c = fgetc(inpt);
if(c=='\n')
d++;
a[i]=c;
i++;
}
while (c != EOF);
a[i]='\0';
printf("Your number of lines = %d\n",d);
printf("\n");
}
void vowel(char a[])
{
int d=1,t=0,temp,f;
inpt=fopen("text1.text","r");
outp=fopen("text2.txt","w");
int i=0;
f=strlen(a);
for(i=0;i<f-1;i=t+i)
{
temp=i;
t=0;
if(a[i]=='A'||a[i]=='E'||a[i]=='O'||a[i]=='I'||a[i]=='U')
{
for(;!isspace(a[i]);i++)
t++;
fprintf(outp," ");
}
else
{
for(;!isspace(a[i]);i++)
{
fprintf(outp,"%c",a[i]);
t++;
}
fprintf(outp," ");
}
t=t+1;
i=temp;
}
}
int main()
{
char a[2000];
count(a);
vowel(a);
getch();
return 0;
}

the output would be according to file

you can read more
for more details about file handling so you should read following

file handling

What is file and file handling

Data files are two types: one is input data file and another is output data file. If you want that your input data should be permanently store in input data file and you want to use this data file any later time. For this first create input data file in some secondary storage device then input data and save data in this input file permanently (in place of simply taking data temporarily from keyboard) doing this our input data will be permanently store in this input data file and we can simply use this file any time. Similarly we can save our outputs in Output data file permanently in place of simply displaying outputs only on the screen of computer.
Important Statements/Functions for handling I/O Data

files

List of Some useful Functions/Statements and their purpose is explained below :
FILE *inp, *outp; // Makes cells named inp and outp for storage of file pointers.
inp=fopen( “num.dat”, “r”);//It opens num.dat as an input file, storing file pointer in inp. If this file is not opened properly then it returns NULL value.
outp= fopen( “num.out”, “w”); // It opens num.out as an output file and stores file pointer in outp. If this file is not opened properly then it returns NULL value.

fscanf(inp, “%d %d”, &mid, &low); // It copies input data from file num.dat into the type int variable mid and low.
fprintf(outp, “%d %d %d”, high,mid,low) ; // It stores in the file num.out a line containing the values of high , mid, and low.
fclose(inp); // Closes created input file num.dat
fclose(outp); // Closes newly created output file num.out

More on files

Opening Files: The system must prepare the file for input or output before permitting access i.e., the file should be opened for reading or writing by using the fopen function and a pointer to the file should be assigned to a FILE pointer variable. Consider the following
FILE *inpfile, *outfile ;
inpfile = fopen (“input.dat”, “r”) ;
outfile = fopen (“output.dat”, “w”) ;
In the above example, two pointers inpfile, outfile are declared as pointers to FILE. Next, the input.dat file is opened for reading (“r” indicates reading) and the FILE pointer inpfile is pointing to that input file input.dat. Next, the output.dat file is opened for writing (“w” indicates writing) and the FILE pointer outfile is pointing to that output file output.dat.

More about pointer to file

Now, the file can be accessed anywhere in the program by using the corresponding FILE pointers. If you want to add to the end of the file use the mode “a” as
outfile = fopen (“output.dat”, “a”) ;
Similarly, use the modes “r+” for read/write, “w+” for read/write as
outfile = fopen (“output.dat”, “r+”) ;

Note
Opening a file for read operations requires the file to exist. If no file exists, an error is returned. If you open a file for output and if no file by that name exists, then one is created; if any file by that exists then that file will be erased and new file will be created. If a file is opened for read/write operations it is not erased if it exists, if no file exists, one is created.

If the fopen function is unable to open the file, the file pointer that it returns will have a NULL value. The following statement detects for an unsuccessful attempt to open a file
if (inpfile == NULL)
printf (“Cannot open the file\n”) ;

Closing Files

When a program has no further use for a file, it should close the file by using the function fclose with the file pointer pointing to the file to be closed as fclose (inpfile) ; The feof function (end of file):
In order to test for an end of file condition on a file when a file is being read,
the function feof is provided. The single argument to the function is a FILE
pointer. The function returns an integer value that is non-zero if all of the data
from the specified file has been read, and is zero otherwise. So, the statement
if ( feof (inpfile) )
printf (“Ran out of data\n”) ;

will display the message if the end of file is reached when feof returns a zero.
As seen above the feof takes the FILE pointer inpfile as its argument.

The fscanf and fprintf functions

The fprintf and fscanf functions are provided to perform the analogous
operations of the printf and scanf functions on a file. The fscanf reads from a
file and fprintf writes to a file, whereas the scanf and printf reads and writes
from/to the screen. The functions fprintf and fscanf takes an additional
argument, which is the FILE pointer that identifies the file to which that data
is to be written or from which the data is to be read.

So, to write the line “I like C Programming” to the file pointed by outfile,
we write

fprintf

fprintf (outfile, “I like C Programming\n”) ;
As seen above, the fprintf takes the pointer to FILE outfile as its first argument and the string to be written as its second argument. To read the next float value from a file pointed by inpfile, we write
fscanf (inpfile, “%f”, &x) ;

As seen above, the fscanf function takes the pointer to FILE inpfile as its first argument, the format string as its second argument and the variable as its third argument. So, it is reading a float value from a file pointed by inpfile and assigning it to the variable x.
The fscanf function returns EOF (end of file) value if the end of file is reached. So, to read from the file pointed by inpfile and check whether it has reached end of file we write
if ( fscanf (inpfile, “%f”, &x) == NULL )
printf (“End of file\n”) ;

The fgets and fputs function

For reading and writing entire lines of data from and to a file, the fgets and fputs functions can be used. The fgets function is called as follows:
fgets (buffer, n, file_pointer) ;

buffer

buffer is a pointer to a character array where the line that is read will be stored; n is an integer value that represents the maximum number of characters that are to be stored into buffer, and file_pointer identifies the file from which the line is to be read. The fgets function reads characters from the specified file until a newline character has been read or until n-1 characters have been read, whichever occurs first. The function automatically places a null character after the last character in the buffer and returns the value of the buffer if the read is successful and the value NULL if an error occurs on the read or an attempt is made to read past the end of the file. The fputs function writes a line of characters to a specified file.
fputs (buffer, file_pointer) ;

array file

Characters stored in the array pointed to by buffer are written to the file identified by file_pointer until the null character is reached. The terminating null character is not written out to the file.
The getc and putc functions: The function getc enables us to read in a single character from a file and the putc function enables us to write a single character to a file. Both these functions take the FILE pointer as one of their arguments. The getc function returns the value EOF when the end of file is reached.
char c ;
c = getc (inpfile) ;
putc (‘a’, outfile) ;
The above getc reads a single character from the file pointed by inpfile and
assigns it to the character variable c, the putc function writes the single
character a to the file pointed to by outfile FILE pointer.

At last

Note: Before you read from a file make sure that the file is opened in the read mode and before you write to a file make sure that it is opened in the write mode

Leave a reply:

Your email address will not be published.