23500

How do you use input redirection from a file in C?

Question:

I'm very beginner-level in coding, C is the only language I have been learning. I've done thorough research on input redirection to a file to try to figure out how it works, but I do not understand where to use the command or exactly how it is used. My question is, where do I put the redirection command line in the program exactly? I know that it looks something like this: ./a < filename.txt , but I have no idea where to put it in the program, or if it even goes in the program? I want to read data from the files into a scanf using a simple loop. Also, the 'a', is that the exact name of the C program you are writing?

Answer1:

If you want to read from a redirection, then the program needs to read from stdin:

int main(void) { char line[1024]; fgets(line, sizeof line, stdin); puts(line); return 0; }

If you execute the program like this:

$ ./readline

then the user must enter the text and press <kbd>ENTER</kbd>.

If you execute the program like this:

$ echo "Hello World" | ./readline Hello World $ ./readline < filename First line of filename

then stdin will be connected to the pipe / redirection. You don't have to worry about this, the shell executing the command does the work (connecting stdin to the pipes, etc) so that your program only need to read from stdin.

Same thing applies for stdout, if you want that the user calls your program and uses the output in a pipe or redirection, then just write normally to stdout. The shell takes care of connecting stdout to the pipe / redirection.

Recommend

  • How to remove element(s) from std::vector without resizing it
  • Wordpress form with file upload submit with Ajax
  • Parse JSON with optional field
  • Setting up rails dev env
  • Creating separate data source for my session spring using JDBC and spring data jpa in spring boot
  • How to add tag during runtime in C#
  • Can't access web service when connected to the network :: HTTP 407
  • How can I find the difference between two times [duplicate]
  • in Gwt, there are 2 different packages (or 2 options) for doing drag n Drop? Which one is better?
  • Bulk loading into PostgreSQL from a remote client
  • JavaFX Embed Custom Font Not Working
  • Using django-multiupload within a ModelForm
  • integrity constraint violation: NOT NULL check constraint
  • Opening tel: links from UIWebView
  • Jenkins sending notifications to the wrong commit id
  • Could not resolve all files for configuration ':react-native-vector-icons:classpath'
  • Facebook friend list in Facebook Android SDK 3.14
  • Multiple canvases (pages) in Fabric.js
  • Pick Out Specific Number from Array? [duplicate]
  • Stop an element moving with padding on hover
  • How to write seo friendly url's using htaccess?
  • Django, uWSGI & nginx: Process dies for “no reason”
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • How to include associated objects using gon in Rails/jQuery
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • How to call jQuery function in HTML returned by AJAX
  • php “page caching” solution suggestions for CMS Applications
  • Google App Engine backend servlet not responding
  • Background transfer download task failed when app was closed
  • VLOOKUP in IMPORTRANGE
  • XEP-0166: Jingle protocol implementation for voice/video chat in iOS
  • Sign a Pdf using custom digital signature in Java
  • Using Service Component Runtime
  • Jersey serializes character value to ASCII equivalent numeric string
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • Firebase: How to read from external DB?
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • Spring Boot not autowiring @Repository
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}