Question

In: Computer Science

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

}

  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

#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 above program, the statement: fp1= fopen ("C:\\myfiles\\newfile.txt", "r") will open the file in read mode.

2. Syntax for opening a file: *fp = FILE *fopen(const char *filename, const char *mode); Here, *fp is the FILE pointer ( FILE *fp ), which will hold the reference to the opened(or created) file.

3. The mode being used is read mode. example: fp1= fopen ("C:\\myfiles\\newfile.txt", "r"); The 'r' specifies that file is opened in read mode.

4. With reference to the above program, the file pointer fp1 holds the reference to the opened or created file.

5. If the file is successfully opened, then the file pointer fp1 will hold the reference of the created file. If it is not opened,then file pointer will hold NULL.

6. //Code that will check the file has opened successfully:
if (fp1 == NULL) {
perror("ERROR: ");// here perror will print the error
return 1;
}

7. To read a file, the function to be used is fgetc() or fgets(). It reads upto n-1 characters. If the file is a binary type of file, then the function used to read that file is fread();

8. To write in a file, first, the file should be opened in write mode("w"). Then we use the function fputc() to write individual characters to the stream or fputs() to write string to the file.

/*C program to write in the file*/
#include <stdio.h>
  
int main() {
FILE *fp;
  
fp = fopen("sample.txt", "w");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
return 0;
}
  
9. The function used for closing the file is fclose(); This would close the file associated with the file pointer.

10. //Program that contains open, read, write and close operation in C.
#include <stdio.h>
int main()
{
FILE * fp;
char c;
// to open a file in write mode
fp = fopen("sample.txt", "w");
//writing operation on the file sample.txt character by character
while ((c = getchar()) != EOF) {
putc(c, fp);
}
//to close the file
fclose(fp);
printf("Data Entered in the file:\n");
//to open the file for reading
fp = fopen("sample.txt", "r");
while ((c = getc(fp)) != EOF) {
printf("%c", c);
}
fclose(fp);// to close the file which is opened
return 0;
}

11. To read strings in files, we use the function fgets() and to write in the file, we use fputs().

12. //Program to read the strings from a file in C programming.
#include <stdio.h>
#define maxCharacter 1000
  
int main() {
FILE *fp;
char s[maxCharacter];
char* name = "c:\\temp\\demo.txt";

fp = fopen(name, "r");
if (fp == NULL){
printf("Could not open file %s",name);
return 1;
}
while (fgets(s, maxCharacter, fp) != NULL)
printf("%s", s);
fclose(fp);
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> #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...
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 <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)       ...
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; }
#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...
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...
Please convert the following C program into the RISC-V assembly code 1) #include <stdio.h> int main()...
Please convert the following C program into the RISC-V assembly code 1) #include <stdio.h> int main() { int i = 2, j = 2 + i, k; k = i * j; printf("%d\n", k + j); return 0; } 2) #include <stdio.h> int main() { int i = 2, j = 2 + i, k = j / 2; if (k == 1) { printf("%d\n", j) k = k * 2; if ( k == j) { printf("%d\n|, j); }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT