Showing posts with label reading a text file. Show all posts
Showing posts with label reading a text file. Show all posts

Sunday, 15 May 2016

Reading a file using FileInputStream in java

import java.io.FileInputStream;

public class ReadFile {
   
   
   
    public static void main(String args[])
        
           
    {  FileInputStream fin ;
    try{
  
    fin= new FileInputStream("C:\\Users\\user\\Desktop\\ash.txt");
    int c;
    while((c=fin.read())!=-1)
    {
       
       
        System.out.print((char)c);
    }
   
    }
    catch(Exception e){}
   
    } 
   
}

Sunday, 21 February 2016

Filehandeling :Reading a text file

Q) Program to  read a text file and calculate number of tabs,new lines,spaces,characters in it.

#include <iostream>
#include<stdio.h>
using namespace std;

int main()

{
int tab=0,lines=0,pa=0,charec=0;
FILE *fp;
char ch;
fp=fopen("mim.txt","r");
ch=getc(fp);
    while(ch!=EOF)
    {   if(ch=='\t')
       tab++;
       if(ch=='\n')
       lines++;
       if(ch==' ')
       pa++;
       charec++;
       ch=getc(fp);
    }

cout<<"number of tabs ="<<tab<<endl;
cout<<"number of new lines ="<<lines<<endl;
cout<<"number of spaces ="<<pa<<endl;
cout<<"number of characters ="<<charec<<endl;
fclose(fp);
return 0;
}