By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,502 Members | 1,620 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,502 IT Pros & Developers. It's quick & easy.

Reading from file redirect

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Max wrote:
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';
}

Jul 22 '05 #2

P: n/a

"Max" <ma*@mxserve.com> wrote in message
news:pe********************@comcast.com...
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::ostream_iterator<std::string>(std::cout, "\n"));

return 0;
}
Jul 22 '05 #3

P: n/a
Max wrote:
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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.