0

I want take input from user and then save that input into the file. In my below code if I remove my while loop then the file is appended but I want this loop so that user can enter data upto 500 characters.

int main()
{
    char Buffer1[5];

    FILE *ot;
    fopen_s(&ot, "D:\\export1.txt", "a+");

    fseek(ot, 0L, SEEK_END);

    int sz = ftell(ot);

    printf("Enter Data.\n");

    while (sz<500) {

        for (int i = 0; i < 5; i++) {
            scanf_s("%c", &Buffer1[i]);
        }

        // write data to file

        for (int i = 0; i < 5; i++) {
            fputc(Buffer1[i], ot);
        }

         sz = ftell(ot);
    } 

    fclose(ot);
    _gettch();
    return 0;
}
Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
  • 4
    Might be handy to check that `fopen` worked. Ditto with the rest of those function calls – Ed Heal Nov 13 '17 at 19:18
  • What if file is already filled with 500 bytes? – user7860670 Nov 13 '17 at 19:20
  • 1
    With the `while` loop, your program *insists* that the user enter 500 characters. If you kill the program prematurely, so that the `fclose()` is not executed, then you might not see any changes to the file. – John Bollinger Nov 13 '17 at 19:22
  • If you want to allow the user to enter fewer characters, then you need to implement a mechanism by which the user can tell the program when he's done. – John Bollinger Nov 13 '17 at 19:24
  • So, How can do it? I want to save user input in chunks. – Sachin Khokhar Nov 13 '17 at 19:24
  • 2
    Note: `scanf_s("%c", &Buffer1[i]);` is missing an argument, please enable compiler warnings, and this may show up other errors too. – Weather Vane Nov 13 '17 at 19:25
  • Another error: what is `_gettch();`? This might not be your *actual code*. – Weather Vane Nov 13 '17 at 19:27
  • It is a console application in visual studio. – Sachin Khokhar Nov 13 '17 at 19:28
  • @SachinKhokhar - There is a way of running programs via VS that it will ask to press a key to continue. Off the top of my head cannot remember – Ed Heal Nov 13 '17 at 19:29
  • https://stackoverflow.com/questions/1775865/preventing-console-window-from-closing-on-visual-studio-c-c-console-applicatio – Ed Heal Nov 13 '17 at 19:30
  • @SachinKhokhar, is there a character or character sequence that a user can enter from the keyboard that you can safely treat as a command to quit instead of data to be transferred? Say a newline, or a five-character block containing only spaces? – John Bollinger Nov 13 '17 at 19:32
  • user can enter any type of input from keyboard. And that should be saved to file. The condition is that data must be stored in file when the Buffer1 is filled but the user must not be stopped from entering the data. – Sachin Khokhar Nov 13 '17 at 19:34
  • @JohnBollinger . You are right that my program is insisting user to enter at least 500 characters. So , how can I save the user data? As it is console application . So what can be done? – Sachin Khokhar Nov 13 '17 at 19:41
  • Well, your requirements seem to be in conflict. You have a program that takes its input from the keyboard, and you need to append all data entered to a file until the file exceeds a given size. But you say you *also* want the user to be able to terminate input before the full amount of data is transferred. How, exactly, then, do you imagine the user could signal an early end of input to the program? I mean from the user's perspective. What would the user do? – John Bollinger Nov 13 '17 at 19:43
  • @SachinKhokhar, I seem to have been too indirect in my early comments. There is nothing obviously wrong with how you are writing data to the file. My supposition is that you do not see the result because the program has not closed the file -- either because it is still running or because you killed it early. – John Bollinger Nov 13 '17 at 19:47
  • My exact requirement is that if user closes the application the whole data should not be lost. The data that he have entered should be saved continuously in file (like 5 char at one time). – Sachin Khokhar Nov 13 '17 at 19:47

2 Answers2

0

This implementation only works if the user appends exactly the amount of bytes necessary to make 500 bytes in total.

It might be easier to first check the filesize, then let the user enter at most 500 - filesize characters, and only after that append the userinput to the file.

Unh0lys0da
  • 196
  • 8
  • what if i want to save data in chunks. Like autosave. – Sachin Khokhar Nov 13 '17 at 19:44
  • One way or another you have to know when the user has finished typing. Normally this is done by recognizing the enter key. scanf checks for the enter key when using the %s format and stops scanning for characters upon detecting it. Using chunks is fine as long as you detect the enter key. – Unh0lys0da Nov 13 '17 at 19:55
0
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <conio.h>

#define ARRAY_LIMIT 10
int main()
{
    char array[ARRAY_LIMIT];
    int i = 0;
    FILE *ot;
    fopen_s(&ot, "export1.txt", "a+");

    fseek(ot, 0L, SEEK_END);

    int sz = ftell(ot);

    printf("Enter Data.\n");

    while (sz < 500)
    {
        while (i < ARRAY_LIMIT)
        {
            array[i] = getch();
            printf("%c", array[i++]);
        }
        i = 0;
        fwrite(array, sizeof(array), 1, ot);

        sz = ftell(ot);
        //be on the safe side...
        if (sz != 500 && 500 - sz < ARRAY_LIMIT)
            i = ARRAY_LIMIT - (500 - sz);
    }

    fclose(ot);
    return 0;
}
Elad Hazan
  • 331
  • 2
  • 7