Question

In: Computer Science

#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;

}

  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.

please answer all

Solutions

Expert Solution

1)The statement  fp1= fopen ("C:\\myfiles\\newfile.txt", "r"); is for opening the file newfile.txt from the path C:\\myfiles.

2)The general Syntax for opening a file is

FILE *ptr ///declaring the pointer to ensure effective cconnection and communication between the program and the file.

fopen("file path", "mode"); /// filepath is the loaction of the file in the system eg:C:\\myfiles\\newfile.txt as in the given program. Mode is in which mode the file opens in the programme.the most common modes are r(read) and w(write).

3)In the given program the mode used is 'r' i.e., it opens the file in read mode. The contents of the file can be read.

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

4)The pointer fp(*fp1) stores the information regarding the file we are currently handling. This pointer acts as a link between the programme and file.

5)We can check if the file opened succesfully by giving a if function for checking the vakue of the declared pointer is NULL or not

6)eg: if(fp1==NULL) //then not opened succesfully.

7)The function for reading a file is fopen("filename","mode") where mode is r that is for read.

8)To write a file the function is fopen("filename","w") where w indicated write mode.

int main()

{

     FILE *fp1;

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

if(fp1== NULL){

printf("error"); // not opened succesfully

exit(1);

}

int n= 10;

fprintf(fp1,"%d",n);

fclose(fp1);

return (0);

}

9)fclose(pointer) is used to close a file, where pointer is the file pointer name.

10)int main()

{

     FILE *fp1,*fp2;

     char c;

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

     while(1)

     {

        c = fgetc(fp1);

        if(c==EOF)

            break;

        else

            printf("%c", c);

     }

fp2= fopen ("C:\\myfiles\\newfile1.txt", "w");

if(fp1== NULL){

printf("error"); // not opened succesfully

exit(1);

}

int n= 10;

fprintf(fp2,"%d",n);

fclose(fp1);

fclose(fp2);

return 0;

}

11)fgets(char *s, int len, FILE *fpr) is used to read a string from a file.

s= array of characters to store the string, len= length of input, fpr= pointer to the file.

fputs(char *s, FILE *fpr) is used to write to a file.

s= array, fpr= pointer to the file.

12)

int main()

{

    FILE *fr,*fw;

    /*array to store string */

    char str[100];

    /*Opening the file in "r" mode*/

    fr = fopen("C:\\readfile.txt", "r");

   /*Opening the file in "w" mode*/

    fw = fopen("C:\\writefile.txt", "w");

    /*Error handling*/

    if (fr == NULL || fw == NULL)

    {

       printf("error");

    }

    /*reading the file till end*/

    while(1)

    {

       if(fgets(str, 10, fr) ==NULL)

            break;

       else

            printf("%s", str);

    }

/* writing to file*/

    fputs(str,fw);

    /*Closing the input file after reading*/

    fclose(fr);

/*Closing the input file after writing*/

    fclose(fw);

    return 0;

}


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...
#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 #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...
FILE *fin = NULL; fin = fopen(in.txt, "r"); int *arr = (int *) malloc (sizeof (int)...
FILE *fin = NULL; fin = fopen(in.txt, "r"); int *arr = (int *) malloc (sizeof (int) * fin); ***************************************************** If I want to open a in.txt, only the integer in the file, is the malloc code correct? What if the in.txt includes the string and integer, how can I use the malloc?   Thanks
#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...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT