Reading from file redirect

M

Max

Yea this is probably a n00b question, but I haven't programmed C++ in at
least 2 years and have never programmed for unix, sorry :)

Anyway, I have a project in which a program is required to read from an
input file, process it and spit some stuff back out. The program will be
called from UNIX using "a.out < primary.input". That primary.input file
has a number of lines that I need to read. How do I go about reading
them into a string type? Is this anything related to the way command
line arguments work in windows, or completely different?

Thx for your help.
 
K

Kai-Uwe Bux

Max said:
Yea this is probably a n00b question, but I haven't programmed C++ in at
least 2 years and have never programmed for unix, sorry :)

Anyway, I have a project in which a program is required to read from an
input file, process it and spit some stuff back out. The program will be
called from UNIX using "a.out < primary.input". That primary.input file
has a number of lines that I need to read. How do I go about reading
them into a string type? Is this anything related to the way command
line arguments work in windows, or completely different?

Thx for your help.

Unix specifics are off-topic in this group, but since this is a quick one,
here it goes:

When you type something like

a.out < primary.input

at a typical shell prompt in a unix system, the file primary.input will be
connected to std::cin and thereby available to your program. You can,
therefore, use std::getline() to read your file primary.input via std::cin
line by line.


Best

Kai-Uwe Bux
 
R

Rolf Magnus

Max said:
Yea this is probably a n00b question, but I haven't programmed C++ in at
least 2 years and have never programmed for unix, sorry :)

Anyway, I have a project in which a program is required to read from an
input file, process it and spit some stuff back out. The program will be
called from UNIX using "a.out < primary.input". That primary.input file
has a number of lines that I need to read. How do I go about reading
them into a string type? Is this anything related to the way command
line arguments work in windows, or completely different?

The command line arguments are passed to main() through the argv pointer
indepenant of the OS you're using it on. But I wonder what reading strings
from a file (or from cin, which is what you seem to want) has to do with
command line arguments.
An easy way to read from cin line-by-line is:

#include <iostream>
#include <string>

int main()
{
std::string line;
while (std::getline(std::cin, line))
std::cout << "line: " << line << '\n';
}
 
M

Mike Wahler

Max said:
Yea this is probably a n00b question, but I haven't programmed C++ in at
least 2 years and have never programmed for unix, sorry :)

C++ is topical here, operating systems are not.
C++ is a platform independent language, so it
doesn't matter which OS you use.
Anyway, I have a project in which a program is required to read from an
input file, process it and spit some stuff back out. The program will be
called from UNIX using "a.out < primary.input".
That primary.input file
has a number of lines that I need to read. How do I go about reading
them into a string type?

See below.
Is this anything related to the way command
line arguments work in windows, or completely different?

No, it has nothing to do with command line arguments, regardless
of the OS you use.

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

int main()
{
std::string line;
std::vector<std::string> data;
while(std::getline(std::cin, line))
data.push_back(line);

if(!std::cin.eof())
std::cerr << "error reading input\n";

std::cout << "file contents:\n";
std::copy(data.begin(), data.end(),
std::eek:stream_iterator<std::string>(std::cout, "\n"));

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,945
Members
47,494
Latest member
ooty

Latest Threads

Top