Question

IN C-PROGRAMMING: What is Structured Programming; what is machine and assembly coding; what is pseudo coding?...

IN C-PROGRAMMING:

What is Structured Programming; what is machine and assembly coding; what is pseudo coding?

What do the following do:   compiler, preprocessor, return 0, { }?

Describe data types and what distinguishes each;  how would one combine data types?

Explain functions and what are they for?

Explain:  scanf (gets, fgets, getchar, fgetchar), printf( puts, fputs, putchar, fpetchar)

What do the following mean:   strlen, strcmp, strcat, strcpy?

What does fopen and fclose mean?

Explain the difference between stdin and file.

What if your file is not read or opened?   What is likely the problem?

Explain bubble sort.

What do we use an editor versus word for example?

What are major decision processes:  IF, IF/ELSE, SWITCH, ||, &&.

What is assignment and what is the distinction between it and typical arithmetic operations?

How does one construct a loop?

What is the difference between a global and a local declaration?

What is a key feature that ends a statement?

What is an array?

What is a pointer?

What is pass by value and pass by reference?

How does one read, write or append a file?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q) How does one read, write or append a file?

Answer:

Reading data from a File

There are three different functions dedicated to reading data from a file

  • fgetc(file_pointer): It returns the next character from the file pointed to by the file pointer. When the end of the file has been reached, the EOF is sent back.
  • fgets(buffer, n, file_pointer): It reads n-1 characters from the file and stores the string in a buffer in which the NULL character '\0' is appended as the last character.
  • fscanf(file_pointer, conversion_specifiers, variable_adresses): It is used to parse and analyze data. It reads characters from the file and assigns the input to a list of variable pointers variable_adresses using conversion specifiers. Keep in mind that as with scanf, fscanf stops reading a string when space or newline is encountered.

The following program demonstrates reading from fputs_test.txt file using fgets(),fscanf() and fgetc () functions respectively :

#include <stdio.h>
int main() {
        FILE * file_pointer;
        char buffer[30], c;

        file_pointer = fopen("fprintf_test.txt", "r");
        printf("----read a line----\n");
        fgets(buffer, 50, file_pointer);
        printf("%s\n", buffer);

        printf("----read and parse data----\n");
        file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer
        char str1[10], str2[2], str3[20], str4[2];
        fscanf(file_pointer, "%s %s %s %s", str1, str2, str3, str4);
        printf("Read String1 |%s|\n", str1);
        printf("Read String2 |%s|\n", str2);
        printf("Read String3 |%s|\n", str3);
        printf("Read String4 |%s|\n", str4);

        printf("----read the entire file----\n");

        file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer
        while ((c = getc(file_pointer)) != EOF) printf("%c", c);

        fclose(file_pointer);
        return 0;
    }

Writing to a File

In C, when you write to a file, newline characters '\n' must be explicitly added.

The stdio library offers the necessary functions to write to a file:

  • fputc(char, file_pointer): It writes a character to the file pointed to by file_pointer.
  • fputs(str, file_pointer): It writes a string to the file pointed to by file_pointer.
  • fprintf(file_pointer, str, variable_lists): It prints a string to the file pointed to by file_pointer. The string can optionally include format specifiers and a list of variables variable_lists.

The program below shows how to perform writing to a file:

fputc() Function:

#include <stdio.h>
int main() {
        int i;
        FILE * fptr;
        char fn[50];
        char str[] = "Guru99 Rocks\n";
        fptr = fopen("fputc_test.txt", "w"); // "w" defines "writing mode"
        for (i = 0; str[i] != '\n'; i++) {
            /* write to file using fputc() function */
            fputc(str[i], fptr);
        }
        fclose(fptr);
        return 0;
    }

If we open the file in "a" mode instead of "w", we can append to the file.

Q) What is pass by value and pass by reference?

Answer:

By definition, pass by value means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter. Use pass by value when when you are only "using" the parameter for some computation, not changing it for the client program.

In pass by reference (also called pass by address), a copy of the address of the actual parameter is stored. Use pass by reference when you are changing the parameter passed in by the client program.

Consider a swapping function to demonstrate pass by value vs. pass by reference. This function, which swaps ints, cannot be done in Java.

   main() {
      int i = 10, j = 20;
      swapThemByVal(i, j);
      cout << i << "  " << j << endl;     // displays 10  20
      swapThemByRef(i, j);
      cout << i << "  " << j << endl;     // displays 20  10
      ...
   }

   void swapThemByVal(int num1, int num2) {
      int temp = num1;
      num1 = num2;
      num2 = temp;
   }

   void swapThemByRef(int& num1, int& num2) {
      int temp = num1;
      num1 = num2;
      num2 = temp;
   }

Q) What is a pointer?

Answer:

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is −

type *var-name;

Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of the pointer variable.

Q) What is an array?

Answer:

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

NOTE: As per Chegg policy, I am allowed to answer only 4 questions (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.

Add a comment
Know the answer?
Add Answer to:
IN C-PROGRAMMING: What is Structured Programming; what is machine and assembly coding; what is pseudo coding?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple...

    Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple program. Generally, it reads a file as specified by the user and prints its contents. A typical usage is as follows, in which the user wants to see the contents of my-cat.c, and thus types: prompt> ./my-cat my-cat.c #include <stdio.h> ... As shown, my-cat reads the file my-cat.c and prints out its contents. The "./" before the my-cat above is a UNIX thing; it...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • C++ 1. How do functions facilitate modular programming? 2. What does a function prototype do? 3....

    C++ 1. How do functions facilitate modular programming? 2. What does a function prototype do? 3. What does "calling" a function mean? 4. If you have two or more functions with the same name, what has to be true of them? 5. How would you explain the difference between pass-by-value and pass-by-reference?

  • need this in c programming and you can edit the code below. also give me screenshot...

    need this in c programming and you can edit the code below. also give me screenshot of the output #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define LINE_SIZE 1024 void my_error(char *s) { fprintf(stderr, "Error: %s\n", s); perror("errno"); exit(-1); } // This funciton prints lines (in a file) that contain string s. // assume all lines has at most (LINE_SIZE - 2) ASCII characters. // // Functions that may be called in this function: // fopen(), fclose(), fgets(), fputs(),...

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • Please, please help with C program. This is a longer program, so take your time. But...

    Please, please help with C program. This is a longer program, so take your time. But please make sure it meets all the requirements and runs. Here is the assignment: I appreciate any help given. Cannot use goto or system(3) function unless directed to. In this exercise you are to create a database of dogs in a file, using open, close(), read, write(), and Iseek(). Do NOT use the standard library fopen() ... calls! Be sure to follow the directions!...

  • C programming course. Cannot use global variables. This is the assignment: In this project you will...

    C programming course. Cannot use global variables. This is the assignment: In this project you will calculate the maximum profit you could have made by trading a single stock over a period of one month. You will assume that you bought rounded stock units (usually in hundreds, or tens) worth between $3000 and $5000 at the best possible“closing” price during the period and sold it at the best possible “closing” price. You will assume that you are permitted to buy...

  • Pease help ASAP C programming course. Cannot use global variables. This is the assignment: In this...

    Pease help ASAP C programming course. Cannot use global variables. This is the assignment: In this project you will calculate the maximum profit you could have made by trading a single stock over a period of one month. You will assume that you bought rounded stock units (usually in hundreds, or tens) worth between $3000 and $5000 at the best possible“closing” price during the period and sold it at the best possible “closing” price. You will assume that you are...

  • Write a complete C program that inputs a paragraph of text and prints out each unique...

    Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...

  • Some of the questions relate to Visual C# Tell what each of the following parts of...

    Some of the questions relate to Visual C# Tell what each of the following parts of a computer system does. CPU Main memory Secondary storage Input devices Output devices What are is the difference between system software and applications software? What is machine level language? What is high level language? What is the difference between compilers and interpreters? How do these relate to programming using C#? Compare a GUI with a command line interface. Which of these does C# support?...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT