Getting file content after being redirected in C - c

I am using a C program which takes file contents as input. I am using input redirection at the command line to read the file. Can I use getchar() to obtain the content of the file? Or should I be reading it into a certain array and then access it?

Yes sure you can use any method to read input from file while running program through command.
You do not need to format it at all. the data in file should appear as if typed on command line while providing inputs. May be this helps.
Please provide more insight if this does not help.Thanks

Related

Read file to standard in for parser

I am trying to implement a shell program in a linux environment. The part I am having trouble with is reading a setup_file inside of a shell before running the shell, to do things like set environment variables.
Currently the shell has a parser_results = parse() function which does a "getchar" and waits until the user types something into stdin, then does an execute(parser_result) which executes the command using the output of the parser.
What I want to do is to read the setup_file which has commands inside of it, have the parser read them in and give me the data structures I need. Then I can run execute.
My question is is how do I redirect the contents of the file to stdin? And how do I call the parser to parse this redirected input? I have been playing with dup and dup2 to no avail.
Short answer (to the question 'how do I redirect the contents of the file to stdin') is "You Don't".
You revise your input function to read from a given file stream instead of stdin, and then for reading from the file, you open it and pass that file pointer to your parsing code (and close when the parsing code is done), and then when you're ready for user input, you call the parsing code with stdin instead of the file. That saves fiddling with stdin.

Looping over an input line and calling a function in C?

I wrote a small rgrep function and I'll have another text file with words that I am checking a pattern against.
My text file will be in the following format.
a
because
cat
7.*
How do i write a loop that will call a function for each of those words in the file? Thanks!
Make a state machine for that pattern first, then every char drives that state machine to next state. Check state to check pattern match.
Try using fopen to open file and read line by line using fgets
Check this post too
c, Import text file with different lines and handle the lines

Redirection in Linux

Input files to test project 2. Intended to be used via redirection.
My professor gave us a txt file to use to test if our program works. It reads in ~1000 numbers (so we wouldn't have to manually enter them). But I don't know the linux command on how to use this txt file.
ccarri7#ubuntu:~/C$ ls
ccarri7lab2 ccarri7lab2.c lab2input.txt
ccarri7#ubuntu:~/C$
This is the folder where my executable/source/txt file are.
./ccarri7lab2 < lab2input.txt
will use the text in lab2input.txt as arguments for ccarri7lab2
http://linux.about.com/od/itl_guide/a/gdeitl42t01.htm
Did you try
ccarri7lab2 < lab2input.txt
Hope this is what you want, else you can give some more info.

Which method to use in searching for a line in a file

I have a file with path names to files:
/my/path1
/my/path11
/my/path12
/my/path13
The file structure is that it has individual paths in each line. All I want to do is search for the existence of a string /my/path1 or anyother in the above file many times
I could think of 2 methods.
every time get file contents line by line and then search the string. Advantage is that the file can be of anysize and I dont need to worry about buffer overflow.
Load the contents into a buffer and search it using the buffer. But as I dont have control over the file size I should be cautious here.
What is the best approach? I am working in unix. Is there any in-build library commands in C that I can make use of for this purpose? Or how can I accomplish the same task using awk in C code.
If you use stdio it will do the buffering for you. You can change its operation by using the function setvbuf to buffer more than a single line. getline can by used to check line by line.
I think loading all the file in memory is not a good idea. Using fgets and strcmp is the best way, I guess.

How to read output and give input to a program from c program?

This is with reference to the below question:
Execute program from within a C program
How do I do the same on Windows with Tiny C Compiler?
I need to execute a .exe fro c program and give input to it from within the same C program by using a file or string as source and read the output from it into a string or file.I have been using system() frunction. Any suggesstions or examples are welcome..Thanks in advance
The simplest way if you don't have popen() etc, or you want to avoid the complexity, is to simplly write a data file eg. infile with fwrite() execute the external program with system() and then read outfile.
system("prog.exe <infile >outfile")
Your prog.exe only has to read stdin and write stdout.
This way you can easily test it with the contents of in/out file.
You would normally do this in your tmp directory and then delete them when you are finished.
The only thing to be careful of is the path to the .exe
Google for "windows popen" and you find this link:
lists.trolltech.com/qt-interest/1999-09/thread00282-0.html
The C runtime library also has _popen(), but I would recommend against it.

Resources