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

Answer

here is your answer if you have any doubt please comment, i am here to help you.

*In the program above which statement is functioning for opening a file ,Write the syntax for opening a file?

Here,

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

this line is the opening a file for reading mode. where fp1 is the file handler or file pointer. using fopen() method we can open any file in c. Here is the syntax for opening a file.

fopen(filename,mode)

//  where mode is r-read, w-write, a,r+,w+

*What mode that being used in the program. Give the example from the program?

i already said that the above problem , file opened for read mode. Thats why 'r' is used.

Other modes are,

  1. a – Opens a file in append mode. It returns null if file couldn’t be opened.
  2. w – Opens a file in write mode. It returns null if file could not be opened. If file exists, data are overwritten.
  3. a+ – Opens a file for read and write mode and sets pointer to the first character in the file. But, it can’t modify existing contents.
  4. r+ – Opens a file for read and write mode and sets pointer to the first character in the file.
  5. w+ – opens a file for read and write mode and sets pointer to the first character in the file.

*Referring to the program above what has been stored in the pointer fp1.?

fp1 is a special pointer called File pointer is used which is declared as FILE *fp1;. which is used to handle and keep track on the files being accessed. You must use stdio.h header file for this.

*Explain how to check whether the file has opened successfully?

*Write code that will check the file has opened successfully.

if ( fp1 == NULL ) 
    { 
        printf( "Failed to open." ) ; 
    } 

you can use this code, because the file opened successfully, the file pointer must have value, not null, if it is null then ,we can say the file is not opened successfully.

Explain the function that can be used to read a file?

In this above code, each character is reading from the text file using while loop. Here is the code for printing the value from the text file.

while(1)

     {

        c = fgetc(fp1); //file pointer will update each iteration
 
        if(c==EOF) //if file pointer will end of the file the loop will break

            break;

        else

            printf("%c", c);

     }

output of the above code.

I answered 4 sub question here , and any doubt please comment,

thanks in advance


Related Solutions

#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; } 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 the program above what...
*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 int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");     ...
#include 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 the program above what has...
#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 "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE *fp;        char c;        errno_t err;        err = 0;        err = fopen_s(&fp,"Desktop/test.txt", "r"); file is on my desktop but I err=2 but I don't think it is opening the file?        printf("%d\n", err);        if (err == 2)        {            printf("The file was opened\n");            while (1)       ...
#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); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT