-1

The data in file is random and both alphabet and numbers.

/**** Program to APPEND one file after another file ****/

#include <stdio.h>
#include <fcntl.h>

int main()
{
    FILE *fp, *fc;
    char data[200];

    fp = fopen("students.DAT", "ab");

    if (fp == NULL)
    {
        printf("Can't open file");
        exit(1);
    }

    fc = fopen("pr1.DAT", "rb");

    if (fc == NULL)
    {
        printf("Can't open file");
        exit(2);
    }

    fseek(fp, 0, SEEK_END);

    while (fgets(data, 199, fc) != NULL)
    {
        fputs(data, fp);
    }
    fputs("\n", fp);

    return 0;
}

Is the use of fputs and fgets in this program OK? If not kindly tell the what to use and why fgets/puts is not ok?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    Does your code work or not? Please [edit] and show the first 4-5 lines of `pr1.DAT`. – Jabberwocky Jun 03 '22 at 06:36
  • Does the file contain newlines? – interjay Jun 03 '22 at 06:38
  • Does the file contain 0 bytes? – Gerhardh Jun 03 '22 at 06:43
  • If your file only contains alphabets and digits, why do you want to use binary? What does the concept of a "line" mean in a binary file? – Gerhardh Jun 03 '22 at 06:44
  • 1
    Note that `fseek(fp, 0, SEEK_END);` isn't necessary. A file opened for append writes at the end regardless of how you position the file pointer (that's what "append" means). – Weather Vane Jun 03 '22 at 06:54
  • 1
    With `fputs("\n",fp);` why are you adding a newline that wasn't present in the original file? Anyway this means you are *expecting* a text file, so why did you open in binary? – Weather Vane Jun 03 '22 at 06:57
  • `fgets(data, 199, fc)` can be `fgets(data, 200, fc)` or (better) `fgets(data, sizeof data, fc)` – Weather Vane Jun 03 '22 at 06:59
  • @WeatherVane thank you for suggesting sizeof data approach. – Nitin Bhartiya Jun 06 '22 at 13:16
  • @Jabberwocky what if just want to read the content of any file without knowing if it has text or numbers or symbols or newline in binary.what should i use then.Also can i read pdf through fputs/fgets? – Nitin Bhartiya Jun 08 '22 at 06:29
  • @interjay what if just want to read the content of any file without knowing if it has text or numbers or symbols or newline in binary.what should i use then.Also can i read pdf through fputs/fgets? – Nitin Bhartiya Jun 08 '22 at 06:34
  • @Gerhardh what if just want to read the content of any file without knowing if it has text or numbers or symbols or newline in binary.what should i use then.Also can i read pdf through fputs/fgets? – Nitin Bhartiya Jun 08 '22 at 06:34
  • If you want to be able to read any data, then you cannot use any string or line oriented functions. No `fgets`/`fputs` etc. – Gerhardh Jun 08 '22 at 07:04
  • @Gerhardh then what should be used for that? kindly explain.Thank you for replying to the comment – Nitin Bhartiya Jun 08 '22 at 13:44
  • 1
    Sewe2000 already explained in an answer what functions you should use. – Gerhardh Jun 08 '22 at 13:49
  • Please [edit] and show the first 4-5 lines of pr1.DAT. – Jabberwocky Jun 08 '22 at 14:11

1 Answers1

1

In binary mode you should use fread instead of fgets and fwrite instead of fputs. The difference is if you use fwrite function you should provide as arguments:

  1. pointer to the array ( in your case data)
  2. size of each element of the array in bytes
  3. number of elements in the array
  4. pointer to FILE
sewe2000
  • 11
  • 3