470,573 Members | 1,668 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,573 developers. It's quick & easy.

Reading from file redirect

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
3 7356
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

"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
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.

Similar topics

1 post views Thread by Google Mike | last post: by
1 post views Thread by Michael W. Cocke | last post: by
2 posts views Thread by Jason Heyes | last post: by
4 posts views Thread by Hilary Cotter | last post: by
3 posts views Thread by Steven Thomas | last post: by
reply views Thread by Greg | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.