0

I am planing to make a C program using array of structures which takes input as strings i.e., name of movie and genre(4 different genres):

struct movie
   {
      char name[30];
      char genre[4][10];
   }m[30];

void main()
{
   int i,j;
   for(i=0;i<30;++i)
   {
      scanf("%s",m[i].name);                     //Removing gets
      for(j=0;j<4;++j)
      {
          scanf("%s",m[i].genre[j]);             //Removing gets
      }
   }
}

I want to automate the user input of the program from a pre-defined source e.g a text file, so that I don't have to insert all the input manually. Is there a script(python/bash) to do this or any other method that can make my job easier for 100s of input.

The reason behind using the C program is to store the inputs in a file for future use.

7
  • 3
    Never ever use gets. It is insecure. Commented Apr 24, 2018 at 15:16
  • 1
    Since your program is just reading from standard input, you can use the file redirection or pipeline features of the shell to feed it input. Commented Apr 24, 2018 at 15:18
  • 1
    On a Unix/Linux system, use input redirection, as in abelenky's answer. (Dunno how easy/convenient this would be under Windows, though.) While you're at it, create a test input file with name and genre strings longer than expected, and watch your program explode as the obsolete gets function fails to notice the overflow, and clobbers adjacent parts of memory. Commented Apr 24, 2018 at 15:19
  • 1
    @glennjackman gets has no way to prevent buffer overrun. For that reason, it has been removed from C11. Commented Apr 24, 2018 at 15:25
  • 2
    @glennjackman Do a google search on "gets unsafe". You'll get plenty of answers. Commented Apr 24, 2018 at 15:29

1 Answer 1

7

Write it to accept input from the keyboard. Then use input redirection:

myprogram.out < SampleTestData.txt

Uses the SampleTestData.txt file as if it were keyboard input.

Sign up to request clarification or add additional context in comments.

2 Comments

i am using gcc on linux, so should my steps be like this : 1st step: gcc myprogram.c 2nd step: SampleTestData.txt > ./a.out
you might want to read on unix redirection: link ./a.out < SampleTestData.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.