Processing pathnames listed in a text file.

J

Jason Heyes

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

Karl Heinz Buchegger

Jason said:
#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.
 
B

BigBrian

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

Jason Heyes

Karl Heinz Buchegger said:
Jason said:
#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.

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?
 
K

Karl Heinz Buchegger

Jason said:
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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top