Question

In: Computer Science

#include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");...

#include <stdio.h>

int main()

{

     FILE *fp1;

     char c;

     fp1= fopen ("C:\\myfiles\\newfile.txt", "r");

     while(1)

     {

        c = fgetc(fp1);

        if(c==EOF)

            break;

        else

            printf("%c", c);

     }

     fclose(fp1);

     return 0;

}

  1. In the program above which statement is functioning for opening a file
  2. Write the syntax for opening a file
  3. What mode that being used in the program. Give the example from the program
  4. Referring to the program above what has been stored in the pointer fp.
  5. Explain how to check whether the file has opened successfully?
  6. Write code that will check the file has opened successfully.
  7. Explain the function that can be used to read a file
  8. Explain how to write a file. Write C program to with the file.
  9. Explain the function used for closing a file.
  10. Write a program that contain open, read, write and close operation in C.
  11. How to read/write (i/o) strings in files?
  12. Write a program to read the strings from a file in C programming.

Solutions

Expert Solution

1) In the above program the statement which is functioning for opening a file is:-
fp1= fopen ("C:\\myfiles\\newfile.txt", "r");

2) Syntax for opening a file is:-
ptr= fopen("fileopen","mode");

3) The mode that is basically used in files is:-
"r" - Open for reading
"w" - Open for writing
"a" - Open for append, Data is added to the end of file
"r+" - Open for both reading and writing
"w+" - Open for both reading and writing
"a+" - Open for both reading and appending
The Opening mode that being used in the program is "Open for Reading".
Ex:- fp1= fopen ("C:\\myfiles\\newfile.txt", "r");
Here "r" denotes that file is in Reading mode only.

4) In pointer "fp" it points to the file from where we are reading the data, the address of this
("C:\\myfiles\\newfile.txt") file is located in this fp.

5) In the above program if(c==EOF) return true then it means file is not opened. Otherwise if this condition is false declare that file is opened.
Here, C is a pointer points to the file and EOF denotes End-of-file.

6) The Code that can be written to check whether a file is opened or not is as follow:-
if(c==EOF)
{
// Condition true means file is not opened.
printf("File is not opened");
break;
}
else
{
// File is opened and it reads the character in the files.
printf("%c",c);
}

7) The file read operation can be performed by both fscanf and fgets. It also perform the same operation as
function scanf and gets but with additional parameter that is file pointer. So, it depends on you whether you want
to read file line by line or character by character.
Ex:-
FILE *fp1
fp1= fopen ("C:\\myfiles\\newfile.txt", "r");
// let the file having text written as January 2020.
fscanf(fp1, "%s %d", str1,&year);

8) The file write operation can be performed by the function fprintf and fputs and fputc (for character)
works as same that of printf and puts.
having an additional parameter that is file pointer.
Ex:-
  FILE *fp1;
  fp1 = fopen(“fileName.txt”, “w”);
  

​#include <stdio.h>
int main()
{
   char c;
   FILE *fp;
   fp = fopen("C:\\myfile.txt","w");
   if(fp == NULL)
   {
      printf("File not opened");   
      break;             
   }
   printf("Enter any character: ");
   scanf("%c",&c);
   //Input you entered is stored in the file
   fputc(c, fp);
   fclose(fp);
   return 0;
}
​


  

9) Function used for closing a file is fclose function:-
Ex:-
  FILE *fp1
fp1= fopen ("C:\\myfiles\\newfile.txt", "r");
// .............some operation performed...............
fclose(fp1);

Note:- To close file after doing file operation is must necessary at the end of program.

10)

​#include <stdio.h>
int main() {
        FILE * fp;
        char c;
        //open a file
        fp = fopen("myfiles.txt", "w");
        //writing operation
        while ((c = getchar()) != EOF) {
            putc(c, fp);
        }
        //close file
        fclose(fp);
        printf("Data Entered:\n");
        //reading operation
        fp = fopen("myfiles.txt", "r");
        while ((c = getc(fp)) != EOF) {
            printf("%c", c);
        }
        //close file
        fclose(fp);
        return 0;
    }


11) Read a I/O string file in C:-
// Read text until new line is encountered:-
fscanf(fp,"%[^\n]",c);
printf("Data from the file:\n %s",c);
fclose(fp);

Write a I/O string file in C:-
printf("Enter your string:");
// Stored the input string into array "str"
gets(str);
   // Copied the content of str into file using pointer "fp"
fputs(str, fp);
   fclose(fp);

12)

​#include <stdio.h>
int main()
{
    FILE *fp;
    //Char array to store string
    char str[100];
    //Opening the file in mode "r"
    fp = fopen("C:\\mynewtextfile.txt", "r");
    //Check if there is any issue in opening the file
    if (fp == NULL)
    {
       puts("Issue in opening the input file");
    }
    //Loop for reading the file till end
    while(1)
    {
       if(fgets(str, 10, fp) ==NULL)
            break;
       else
            printf("%s", str);
    }
    //Closing the input file
    fclose(fp);
    return 0;
}

Here, we check condition fgets(str,10,fp) because fgets returns NULL when there is no more records are available to be read.
str - Represent the string in which it stores the string reading it from file.
10 - length of string that need to be read everytime
fp - pointer to the file, which is going to read the file


Related Solutions

*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10];...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); while(1) {    i=0;    printf("\nPlease enter a positive whole number (or EOF to quit): ");    scanf("%s", input); // user inputs value as a string for separate values    if(strcmp(input,"")==0) {        printf("\n\tThank you for using the CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");    return(0); } num=atoi(input); if (num<=0) {    printf("\n\tSorry, that was...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number;...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number; Scanf (“%d”, & number); if (number % 2 ==0) { printf (“Even\n”); } else { printf(“Odd\n”); } Return 0; }
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc,...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc, char **argv) { int n, k, l, r, t, d, i; char str[65]; /* Make the user enter a non-negative integer */ printf("Please enter a non-negative integer: "); scanf("%d", &n); while (n < 0) { printf("Sorry, your input is incorrect.\n"); printf("Please enter a non-negative integer: "); scanf("%d", &n); } /* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */ if...
#include <stdio.h> int main () { int value= 10; int value2= 5; value = value %2;...
#include <stdio.h> int main () { int value= 10; int value2= 5; value = value %2; printf("he FInal =value=%d\n", value); value +=3; printf("he FInal =value=%d\n", value); value ++; printf("he FInal =value=%d\n", value); value= ++value2; printf("he FInal =value=%d\n", value); value= value2--; printf("he FInal =value=%d\n", value); } what is output explain each print statement? exlain why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT