0

I'm in a coding competition where I upload my code to the competition host's platform to be compiled and run. The host then supplies my program with data to work on, through stdin. My code uses scanf("%u", &data) to grab this data.

Before I upload my code to the competition though, I have a chance to test it on my own platform. I'd like my program to supply itself with problem data that I wrote myself and gather this data using the same scanf() code.

I'm writing my test data in a string, located in the same program that needs to grab it using scanf(). Is this possible at all, or do I need to change where or how I'm creating the test data?

ksohan
  • 1,165
  • 2
  • 9
  • 23
Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
  • 5
    Pipe in a file. `./myprog < mydata` – user4581301 Apr 07 '22 at 22:31
  • if its c++ you should be using a stream, in which case its easy to redirect that to read a file or a string – pm100 Apr 07 '22 at 22:39
  • @user4581301 - Ah, I'm a huge idiot outside of C, I suppose I'd need VS to pipe the file for me, since it runs the executable to debug it? – Anne Quinn Apr 07 '22 at 22:49
  • 1
    @AnneQuinn https://superuser.com/questions/654574/pipeline-input-to-executable-with-powershell – Taekahn Apr 07 '22 at 22:52
  • 2
    Never even considered you'd be using Visual Studio, [but Michael Burr has you covered](https://stackoverflow.com/questions/9613059/piping-input-into-a-c-program-to-debug-in-visual-studio) – user4581301 Apr 07 '22 at 23:08
  • 1
    Seriously this is forcing me to reconsider some of my assumptions. Windows and Visual Studio? Someone doing a coding competition and debugging? Unheard of! Wanting to test their code ahead of time? Inconceivable! No, I don't want any iocane powder, thank you. I can't wait to see the other competitor's faces when they see code without bits/stdc++.h and variable length arrays! – user4581301 Apr 07 '22 at 23:23
  • @user4581301 - I um, iunno, I just like solving puzzles in C, I'll probably wash out in the later rounds due to being bad at tools. (I did not know about bits/stdc++.h!) I was in the top 300ish in the qualifier though, so I'm excited! – Anne Quinn Apr 07 '22 at 23:32
  • bits/stdc++.h was intended as a joke. Don't touch that sucker. [It's toxic if not used exactly right](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). We see it all the time from people competing and wondering why the program won't work. – user4581301 Apr 07 '22 at 23:37
  • 1
    That said the whole comment was intended as a joke. You're approaching programming correctly rather than the slap-dash, quick-and-dirty approach used in competitions. That might actually cause you problems later, but you're a lot more likely to get a good answer earlier than most. They'll get logjammed by some bad engineering and not have the right tools or mindset to get around it. – user4581301 Apr 07 '22 at 23:40

0 Answers0