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

Processing pathnames listed in a text file.

P: n/a
I want to open a file with a known location and process its contents. The
contents are to be treated as a list of pathnames. Each pathname is an
absolute or relative pathname in the following sense:

- the current directory is the directory of the opened file
- the root directory is the program directory (i.e., working directory of
the program under execution)

A pathname is processed by opening a file and displaying its contents. The
location of this file is specified by the pathname in either absolute or
relative form.

I worked with a friend to come up with the main function:

#include <iostream>
#include <string>

void process_file(std::string name);

int main(int argc, char **argv)
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " <filename>" << std::endl;
return 1;
}
process_file(std::string(argv[1]));
return 0;
}

We don't know where to go from here. Any advice or code is appreciated.
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Jason Heyes wrote:

#include <iostream>
#include <string>

void process_file(std::string name);

int main(int argc, char **argv)
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " <filename>" << std::endl;
return 1;
}
process_file(std::string(argv[1]));
return 0;
}

We don't know where to go from here. Any advice or code is appreciated.


Look up the file stream classes ifstream and ofstream.

Seriously. If this is all you two have nailed down by now, then you should try
your hands on something simpler.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2

P: n/a
> I want to open a file with a known location and process its contents.
The
contents are to be treated as a list of pathnames. Each pathname is an absolute or relative pathname in the following sense:

- the current directory is the directory of the opened file
- the root directory is the program directory (i.e., working directory of the program under execution)

A pathname is processed by opening a file and displaying its contents. The location of this file is specified by the pathname in either absolute or relative form.

I worked with a friend to come up with the main function:

#include <iostream>
#include <string>

void process_file(std::string name);

int main(int argc, char **argv)
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " <filename>" << std::endl; return 1;
}
process_file(std::string(argv[1]));
return 0;
}
We don't know where to go from here. Any advice or code is

appreciated.

You need to implement the process_file() function. (which IMHO should
take a std::string const & as a parameter instead). Is this your
homework assignment? It doesn't make since to me that somebody could
write the above main and not know what to do next.

Jul 23 '05 #3

P: n/a
Jason Heyes wrote:

Ok we tried again and got a partial implementation of process_file.

void process_pathname(std::string pathname, std::string name);

void process_file(std::string name)
{
std::ifstream in(name);
if (!in.is_open())
return;

std::string pathname;
while (in >> pathname)
process_pathname(pathname, name);
}

Can you help us figure out how to process the pathname itself?
What do you need to do with it
You said for youself:
Each pathname is an absolute or relative pathname in the following sense:

- the current directory is the directory of the opened file
- the root directory is the program directory (i.e., working directory of
the program under execution)


So it seems to me you need to do some string manipulations and checkings
to figure out:

is this an absolute path name or a relative one?

If it is an absolute one, what else needs to be done?
(Append it to the program directory. You may check if
your system passes that information as argv[0] to main())

If it is a relative one, what else needs to be done?
(Append it to the directory of the opened file. That
one might get a little bit tricky to get on.)

In any case you finally come up with a pathname that is
valid for the operating systems file structure. Open
the file and dump its content.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.