Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Custom file input?

+4 votes
asked Dec 15, 2020 by Rahul Choubey (550 points)
I would like to get user input from a file.

The way this would work is that the user would get their own (invisible-to-owner) copy of the file if editing is allowed by the owner. The user could then run the code with their file copy. Is this just a pipe dream?

1 Answer

0 votes
answered Dec 16, 2020 by Peter Minarik (84,720 points)
edited Dec 16, 2020 by Peter Minarik

OnlineGDB Specific

The easiest you can achieve something like this via OnlineGDB is to select "Text" instead of "Interactive Console" at the bottom of the editor, so you can provide the input as a text (file).

A more sophisticated way would be to create a proper project in OnlineGDB and add a new file to it ("New File" icon left to the RUN button).

I've made a quick project sample for you: https://onlinegdb.com/NLIY0TgfC

#include <stdio.h>

int main()
{
    FILE * file = fopen("input.txt", "r");
    char line[255];
    fgets(line, sizeof(line), file);
    printf("Line read: %s\n", line);
    fclose(file);
    return 0;
}

Read/Write Access

So what you need are:

  • A file to be read by your program (a.k.a. input file)
  • Owner can edit the input file
  • User can not edit the input file

If your program doesn't need to change this file, just read from it, then it's simple. You just have to enable read access on these input files for everyone else, while only the owner would have write access (either Linux or Windows or Mac, etc)

You can read how to use the chmod command here.

Also you need a delivery system to distribute the file to the user.

commented Dec 17, 2020 by Rahul Choubey (550 points)
I would rather have the user also be able to edit _their_ copy of the file(though it will look like the original). Text input is something I've tried before, but I'd rather not have to type `input()` until I hit an EOF.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...