I have a file called file.dat and have to write a program called sdev.c which adds up all of the numbers in file.dat from each line. (Assuming that one number is per line)

which has to open like this

$ ./sdev < file.dat

Now I can do all of the work that adds up the numbers but how the hack can I open it and scan each number from each line?

Recommended Answers

All 3 Replies

Your program doesn't have to open the file, the operating system redirects the file contents to stdin of your program, just as if you are typing the data from the keyboard. Get your program working while typing the data on your keyboard, then when that works ok just run it from a command prompt as you posted.

OK now I kinda get it.

So first I have to write the program then simply call the function by a.out < file.dat right?

Yes :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.