This blog is under construction

Thursday, 2 January 2014

Formatted input and output functions in C

The following are built-in functions available in C language which can be used to perform formatted input output operations.

Formatted input functions in C:
int fscanf(FILE *stream, const char *format, ...)
Performs input format conversion.  It reads the input from the stream and does format conversion.  Then, it will assign those converted values to the subsequent arguments of format correspondingly.  And it returns the number of inputs matched and got assigned with values successfully.

int scanf(const char *format, ...)
scanf() is equivalent to fscanf(stdin, ...)

int sscanf(char *str, const char *format)
sscanf() is identical to sscanf() except that the inputs are taken from the string str.


scanf fscanf sscanf example in C

  #include <stdio.h>
  int main() {
        FILE *fp;
        int i, age[3], rollno[3];
        char name[3][32], str[128] = "13:103:Ram";
        fp = fopen("input.txt", "r");

        /* input data for student 1 from user */
        printf("Enter age rollno and name for student 1:\n");
        scanf("%d%d%s", &age[0], &rollno[0], name[0]);

        printf("Reading age, rollno and name from file(input.txt)..\n");
        /* input data for student 2 from file */
        fscanf(fp, "%d%d%s", &age[1], &rollno[1], name[1]);

        printf("Reading age, rollno and name from string..\n");
        /* input data for student 3 from string */
        sscanf(str, "%d:%d:%s", &age[2], &rollno[2], name[2]);
                printf("          Age  Rollno name\n");

        /* print the result */
        for (i = 0; i < 3; i++) {
                printf("Student %d: %d    %d    %s\n",
                                i, age[i], rollno[i], name[i]);
        }
        fclose(fp);
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ cat input.txt 
  10 102 Raj

  jp@jp-VirtualBox:~/$ ./a.out
  Enter age rollno and name for student 1:
  11 101 Tom
  Reading age, rollno and name from file(input.txt)..
  Reading age, rollno and name from string..
                 Age  Rollno name
  Student 0: 11    101    Tom
  Student 1: 10    102    Raj
  Student 2: 13    103    Ram




Formatted output functions in C:
int fprintf(FILE *stream, const char *format, . . .)
Performs formatted output conversion and writes the output to stream.  Return value is the number of characters printed (excludes '\0').

int printf(const char *format, . . .)
printf() is identical to fprintf(stdout, . . .)

int sprintf(char *str, const char *format, . . .)
sprintf() is similar to printf() except that the output is written into the string str.

int vfprintf(FILE *stream, const char *format, va_list arg)
vfprintf() is equivalent to fprintf() except that the variable argument is replaced by arg.

int vprintf(const char *format, va_list arg)
vprintf() is equivalent to printf() except that the variable argument is replaced by arg.

int vsprintf(char *str, const char *format, va_list  arg)
vsprintf() is equivalent to sprintf() except that the variable argument is replaced by arg.


printf fprintf sprintf example in C

  #include <stdio.h>
  int main() {
        FILE *fp;
        int rollno = 101, age = 10;
        char name[32] = "Ram", str[128];
        fp = fopen("input.txt", "w");

        /* printing rollno, age and name in stdout */
        printf("Writing output to stdout:\n");
        printf("Roll no: %d\n", rollno);
        printf("age    : %d\n", age);
        printf("Name   : %s\n", name);

        /* printing rollno, age and name in a file */
        printf("\nWriting rollno, age, name to file(input.txt)\n");
        fprintf(fp, "Roll no: %d\nage    : %d\nName   : %s\n",
                        rollno, age, name);

        /* printing rollno, age and name in a string */
        printf("Writing rollno, age, name to string(str)\n");
        sprintf(str, "Roll no: %d\nage    : %d\nName   : %s\n",
                        rollno, age, name);
        printf("\nContents in string str:\n%s\n", str);
        fclose(fp);
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Writing output to stdout:
  Roll no: 101
  age    : 10
  Name   : Ram

  Writing rollno, age, name to file(input.txt)
  Writing rollno, age, name to string(str)

  Contents in string str:
  Roll no: 101
  age    : 10
  Name   : Ram

  jp@jp-VirtualBox:~/$ cat input.txt 
  Roll no: 101
  age    : 10

  Name   : Ram




vprintf vfprintf vsprintf example in C

  #include <stdio.h>
  #include <stdarg.h>
  #include

  void vformat(char *format, ...) {
        FILE *fp;
        char str[128];
        va_list arg;

        /* intializing variable argument list */
        va_start(arg, format);

        /* writing output data to stdout using vprintf */
        printf("Writing output to stdout using vprintf\n");
        vprintf(format, arg);

        /* writing data to stdout using vfprintf */
        printf("\nWriting output to stdout using vfprintf\n");
        vfprintf(stdout, format, arg);

        /* writing data to string str using vsprintf */
        printf("\nWriting output to string using vsprintf\n");
        vsprintf(str, format, arg);
        printf("Printing the contents in string str\n");
        printf("%s", str);
        va_end(arg);
  }

  int main() {
        int age = 10, rollno = 101;
        char name[32] = "Ram";
        vformat("Age: %d\nRollno: %d\nName: %s\n",
                        age, rollno, name);
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Writing output to stdout using vprintf
  Age: 10
  Rollno: 101
  Name: Ram

  Writing output to stdout using vfprintf
  Age: 10
  Rollno: 101
  Name: Ram

  Writing output to string using vsprintf
  Printing the contents in string str
  Age: 10
  Rollno: 101
  Name: Ram




No comments:

Post a Comment