Articles by "Files Programing"
Showing posts with label Files Programing. Show all posts
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
File handling in Object Oriented Programming:

To perform input and output from files, C++ provides the fstream library. It defines several classes including ifstream and ofstream.
We connect stream object to the physical file by creating a stream object by specifying the name of the file as the argument.
#include <fstream>
ofstream outfile(“output”);
ifstream infile(“input”);
Alternatively, the file can be connected by using the open member function with one or more arguments
#include <fstream>
ofstream outfile;
ifstream infile;
infile.open(“input”);
outfile.open(“output”, ios:: app | ios :: nocreate); // do not create a new file, append at the end of file
The file should be disconnected by using close member function. To access and manipulate contents of the file, get, put, read and write methods are provided.


Thanks
Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Write the c program to display the function names in the given text file.

Rule to implement the above problem:
The input file name should be input.txt and display the output in the console.

Sample Input file (input.txt):

#include<stdio.h>
void swapping(int a, int b);         
int main()
{
int m = 10, n = 20;
printf(" Values before swap  m = %d \nand n = %d", m, n);
swap(m, n);                         
}
void swapping(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf(" \nValues after swap m = %d\n and n = %d", a, b);
}

Sample Output:
main
swapping 

Implementation of the above problem:
#include <stdio.h>
#include <string.h>
void check(char *c,int p1, int p2);
void display(char *c, int p1);
int main(int argc, char **argv)
{
FILE *fp;
char ch[100];
char *pos1, *pos2, *pos3;
fp=fopen("input.txt", "r");
if (fp == NULL)
{
printf("\nFile unable to open");
//return;
}
else
 // printf("\nFile Opened to display function names :\n");
while (1)
{

if ((fgets(ch, 100, fp)) != NULL)
{
if ((strstr(ch, "/*")) == NULL)
{
pos1 = strchr(ch, '(');                /* check opening brace */
if (pos1)
{
pos2 = strchr(ch,')');            /* check oclosing brace */
if (pos2)
{
pos3 = strchr(ch,';');        /* check for semicolon */
if (((pos1 < pos2)) && ((pos3 == NULL) || (pos3 < pos1)))
{
check(ch, pos1 - ch, pos2 - ch);
}
else    continue;
}
else    continue;
}
else    continue;
}
else    continue;
}
else    break;
}
fclose(fp);
return 0;
}

/* To check if it is a function */
void check(char *c, int p1, int p2)
{
int i, flag = 0, temp = p1;
 if ((c[p1 + 1] == ')'))
{
display(c, p1);
return;
}
for (i = p1 + 1; i < p2; i++)
{
if ((c[i] != ' ') || (c[i] == ')'))
{
flag = 1; 
}
if (flag == 0)
{
display(c, p1);
return;
}
else
{

flag = 0;
while (c[--temp] != ' ');
for (i = 0; i < temp; i++)
if (c[i]==' ')
{
flag = 1;
}
if (flag == 0)
{
display(c, p1);
return;
}
else
return;
}
}
}

/* To display function name */
void display(char *c,int p1)
{
int temp = p1, i;
while (c[--temp] != ' ');
for (i = temp + 1; i < p1; i++)            /* Print name of function character by character */
printf("%c", c[i]);
printf("\n");
return; 
}


Thanks
Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Write the c program to create a file "input.txt". Open the file and read the content to count the number of lines.



Rule to implement the above problem:

The file name should be input.txt.
Input format:
Give the input as a file.
Output format:
The output will be the integer which is the number of lines in the file. Display the output in the console. 



Sample Input file (input.txt):

C was invented to write an operating system.
C is a successor of B language.
C is a Structured language.
It produces efficient programs.
It can handle low-level activities.
It can be compiled on a variety of computers.



Implementation of the above problem

#include <stdio.h>
#define MAX_FILE_NAME 100
int main()
{
FILE *fp;
int count = 0;  // Line counter (result)
char filename[MAX_FILE_NAME];
char c;  // To store a character read from file



// Open the file

fp = fopen("input.txt", "r");

// Check if file exists
if (fp == NULL)
{
printf("Could not open file %s", filename);
return 0;
}

// Extract characters from file and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n') // Increment count if this character is newline
count = count + 1;

// Close the file
fclose(fp);
printf("The file has %d lines",count);
return 0;
}



Output:

The file has 6 lines




Thanks

Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Write a program to copy the content from one file to another file.

Rules to implement the above problem:
The input file should be named as "input.txt".
The output file should be named as "output.txt".
Sample Input file (input.txt):
Hello World!
Welcome to C Programming


Implementation of the above problem:
#include <stdio.h>
#include <stdlib.h> // For exit()
 int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
fptr1 = fopen("input.txt", "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Open another file for writing
fptr2 = fopen("output.txt", "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}


Output file (output.txt)
Hello World!
Welcome to C Programming


Thanks
Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Consider an input file "abc.txt" with integer values. Write a program to Open the file and read the content to find the sum of all values in the file.

Rule to implement the problem:
The file name should be abc.txt.
Input format to the program:
Give the input as a file which contains the integer values.
Output format:
The output will be the integer which is the sum of the integers.Display the output in the console.
Sample Input file (abc.txt):
11
21
33


Implementation of the above problem:
#include <stdio.h>
int main()
{
int i, sum=0;
FILE *file;
file = (fopen("abc.txt", "r"));
if(file == NULL)
{
printf("Error!");
}
fscanf (file, "%d", &i);
sum=sum+i;
while (!feof (file))
{
fscanf (file,"%d", &i);      
sum=sum+i;
}
printf("The sum of the integers in the file abc.txt is:%d",sum-6);
fclose (file); 
return 0;
}


Output:
The sum of the integers in the file abc.txt is:65


Thanks
Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Explain the following functions in a c program fscanf(), fprintf(), fgets(), fputs(), fread(),fwrite(), feof().

fscanf()- This function is used to read data from a file.
Syntax : fscanf(filepointer,”format specifier”,&v1,&v2,….);
Example : fscanf(fp,”%d%d%d”,&a,&b,&c);

fprintf()- this function is used to write data in to a file.

Syntax: fprintf(fileprinter,”format specifier”,v1,v2,…);
Example : fprintf(fp,”%d\t%d\t%d\t”,a,b,c);

fgets()- this function reads s strinf from a file.
Syntax: fgets(string variable,size,filepointer);
Example :  fgets(str,40,fp);

fputs()- this function is used to write a string in to a file.
Syntax: fputs(string variable,size,filepointer);
Example : fputs(str,40,fp);

fread()- this function is used to read data into a structure from a file.
Syntax: fread( &structure varable, size of(struct structurevariable), 1, filepointer);
Example : fread(&s,size of(struct s),1,fp);

fwrite()-This function writes a structure in to a file.
Syntax: fwrite(&structure variable,sizeof(struct structure variable),1,filepointer);
Example : fwrite(&s,sizeof(struct s),1,fp);

feof()- this function returns a value true or false. When a file is being read, if there is no more data in the file, then feof() function returns a value true. When a file is being read, if there is more data in the file then the function returns a value false.


Thanks
Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Short answer type questions on Files.

1. What is a file?
A file is a collection of data that is available in permanent storage.


2. Write the syntax for file declaration.
syntax : FILE *filepointer;
Example : FILE *fp;

3. What are modes in file?
Mode tells about the types operations like read,write or append that can be performed on a file that is being opened.

4. Write the syntax to open a file.
syntax : filepointer=fopen(FILENAME, MODE);
Example : fp=fopen(“in.dat”,r);

5. What is the significance of fclose() function?
This function closes a file that has been opened for an operation. Syntax:
fclose(filepointer);
Example : fclose(fp);


Thanks
Mukesh Rajput