Question about K&R - redirect input - c

In section 1.6 of 'The C Programming Language' (K&R) there is an example program and the authors state:
The output of this program ON ITSELF is
digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345
Then there are many similar programs etc. including exercises. I understand everything about the logic of the code and can do the exercises, but how do I actually test my program on a file like the authors or many others on the web?
Edit: The question should be: How can I redirect the input of a file to the program?

The program in chapter 1.6 reads input from stdin. To make it read from a file, you can (on most operating systems) redirect stdin to be a file by running your program like this:
myprogram < somefile
Or you can pipe the content of a file to it like so:
cat somefile | myprogram
On windows, you'd use the type program instead of cat,
type somefile | myprogram

This is using re-direction. Instead of the input to the program coming from the keyboard it comes from a file.
At the DOS prompt:-
C:>myexe < filename
Get to the DOS prompt in Windows use the command shell. Or start Run.. and enter cmd
On a Mac this is called terminal (type "terminal" into Searchlight to get to it).

By default, your program will take input from stdin, which is a buffer which is filled based on input from your keyboard (by default). However, you can also tell your program to fill stdin from a text file instead.
Using a *nix based system, you can simply create a text file, and save it as whatever you'd like, "test_input" for instance. Fill it with the input that you'd like to pass to your program, save it, and then run your program like this:
./a.out < test_input
This is called redirection because you are "redirecting" (if you will) the input to come from a file, rather than the default (keyboard). It goes both ways, you can also redirect your output to a file, rather than stdout with the other angle bracket, '>'.
Using Visual Studio, and not popping open a command prompt to do something like the command above, you can use a C++ ifstream, put the text file in the local directory, and then simply use the ifstream everywhere instead of stdin:
ifstream sin("test_input.txt" , ifstream::in);
int value;
sin >> value;
You can output to a file using an ofstream.
Note that ifstreams and ofstreams are C++ objects, and can't be used in C. While you can write to files and read from files in C, it's a little trickier than simply replacing all instances of cout and cin. You actually have to think about what you are reading and writing :)

Related

How can I access the file in C when the user used the '<' command on the shell?

I am trying to make a program that can process sentences in C in the POSIX environment. Assume that my program's name is "test". If the user entered just "./test", then my program will ask the user to enter some sentences. This one so far is easy.
However, if the user entered "./test < file.txt", the program should get the characters from that txt file. I do not know how I can get the characters of the file in C. I tried something like file = open(argv[2]);, but it did not work.
I will really appreciate it if you give me the answer to this question.
TL;DR: If you start your program like
./test
and you have to type in the input, then exactly the same program will read from file.txt if you start it as
./test < file.txt
Longer explanation starts here. (The following explanation is not 100% precise, but shall help to get an understanding what is going on in principle.)
In a C program you can open files with fopen. As a return value, fopen gives you a FILE pointer. However, when you start a program under Unix, three FILE pointers are already available. These default FILE pointers are stored in variables named stdin, stdout and stderr.
Of these, stdin can be used to read from, stdout and stderr can be written to. And, stdin is used as default in several C library calls, like, gets or scanf. Similarly, stdout is used by default for calls like printf.
Now, although they are called FILE pointers, they can in fact represent other things than just files. stdin could be a file, but it can also be a console where you can type in stuff.
This latter scenario is what you observe when you start your test program from the shell with the command
./test
In this case, the test process will be started with stdin just using the console from the shell from which you started the test program. Therefore, if in your test program you call, say, gets(), then your program will implicitly read from stdin, which represents the console input that was inherited from the shell. Consequently, in this case the user has to provide input by typing it in.
Now let's look at what happens if you start your process from the shell in the following way:
./test < file.txt
Here, the shell does a bit of extra work before it actually creates your test process. This is because the < file.txt part of your command line is interpreted by the shell - this is not passed as arguments to your program. Instead, what the shell does is, to open the file.txt and, when the test process is started, hand the opened file.txt over to the process such that in your test process stdin is connected to file.txt.
Then, the call to gets() in your program will again read from stdin, but this time stdin is not the console. This time stdin really corresponds to a file, that is, file.txt.

Giving 5 Arguments but getting just 3 in terminal

I want to pass a file into a c program.
If I am doing it in the IDE this arguments
./test string string < test.txt
return argc = 5, but on the terminal I am just getting argc = 3.
It seems, that its because of the "<" - symbol, I wanted to use this, to indicate that I am passing a file.
What does < mean? I am using Ubuntu with Tilix terminal
Redirection is performed by the shell, and is not (directly) visible to your program.
./test string string < test.txt
means,
Open test.txt for reading on file descriptor 1
Run ./test with the arguments string and string
The program run in point 2 will inherit the parent's file descriptors, so its standard input will be connected to the opened file handle (rather than the shell's current standard input, which could be your terminal, or a different file handle).
As an aside, you probably want to avoid calling your programs test, though as long as you don't forget to invoke it with an explicit path, this is harmless.
The < symbol will insert information from somewhere (a text file) as if you typed it yourself. It's often used with commands that are designed to get information from standard input only.
For example (using tr):
tr '[A-Z]' '[a-z]' < fileName.txt > fileNameNew.txt
The example above would insert the contents of fileName.txt into the input of tr and output the results to fileNameNew.txt.
Answer adapted from this page.
For similar information about all symbols, use this page

For my C assignment, I am don't understand what it means by using redirection to test the program?

I don't understand what my assignment means about redirection. My assignment says:
Use input redirection to test your program. Note that your program
reads from the user using scanf and input redirection feeds the file
contents to your program. You don’t have to use any file operations in
your program. Consider the following file a.txt:
4
10
20
15
5
-1
Does this mean that I have to make a a.text file? where does the a.txt file with the numbers even come from???
The instructor is telling you to create a text file with a series of numbers in it to feed to your program. Input redirection means using < at the shell when running your program to make it use the given file as stdin.
For example:
./myprogram < a.txt
The file a.txt is treated as the program's stdin.

test C program with some inputs

I wrote a program in C and I want to try it with some inputs. The inputs
are not numbers so I can't use a for loop or something like this in the code.
So I want to write a file with inputs and give it to the program so it will take every time (it waits to input) 1 line from the text file or something like this, is it possible?
If your program reads input from stdin - i.e. if you're using a function like gets or scanf to get your input - you can achieve what you're asking about by using input redirection. Let's say you create a file called test_input.dat which contains your test data. If this file and your program reside in the same directory, you can change to that directory using
cd \your_directory
and then run your program, telling it to read input from test_input.dat by doing the following:
your_program < test_input.dat
This will cause your program to read data from test_input.dat instead of from the terminal, and should work the same for most common operating systems (Windows or Unix variants such as Linux, etc).
Best of luck.

Redirecting standard input

If you're redirecting input for a compiled c program, what's the typical method of reading this input within the program. Is it simply scanf? Say you have a text file like the following:
1
2
3
4
With 4 numbers, one on each line. How do you redirect standard input in your program to read this file, and duplicate it to another text file? Is each new line sent through scanf?
Redirection of streams is an OS concept, it has nothing to do with the C language. If you read from stdin, which is the standard input stream, you will deal with it correctly. Functions like scanf use stdin implicitly, so your program will work fine regardless of whether it gets its input from the console or from a redirected file.
If you are on Linux, redirection is very easy. If you executable name is a.out
$ echo `a.out` > file.txt
will redirect all output from a.out to file.txt

Resources