Hello ladies and gents,

Have a few questions about the following example that is given:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	char ch;
	int n=0;
	ifstream ff("aa");

	if (!ff) 
	{
		cout << "Can't open file aa for input.\n";

		return 1;
	}

	ofstream gg("bb");

	if (!gg) 
	{
		cout << "Can't open file bb for output.\n";

		return 1;
	}

	// Copying starts here:
	while (ff.get(ch)) 
	{
		gg.put(ch); 
		n++;
	}

	cout << n << " characters copied.\n";

	return 0;
}

When talking about "can't open file aa for input". Could this be for example a Word document? Is that what 'a file' could point to???

Also,

ifstream ff("aa");

Is it the intention to put an actual link towards an existing file like for example C:/MyPrograms/Microsoft Visual/ ... within this part (" ") If this type of code would be used in a real example :?:

Yes, it could be a word document, it represents a real file. (Though it's more likely to be a pure text file)

Ok, but am I correct in assuming that where ("aa") is written, there could/should be a link like this in for example: C:/MyPrograms/Microsoft Visual/ ... ?

Also, if this is correct, is this piece of code

ifstream ff(...

just a variable of ifstream then?

Additional question,

When I have this program, what additional info do I have to enter to let this program effectively output the desired solutions?

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{  
	string filename, s, longest = "";
	int maxlen = 0;

	cout<<"Name inputfile: ";

	getline(cin, filename, '\n');

	ifstream in(filename.c_str());

	if (in.fail())
	{
		cout<<"Can't open inputfile.\n";
		
		return 1;
	}

	while (getline(in, s, '\n'), !in.fail())
		if (s.length() > maxlen) 
		{
			longest = s;
			maxlen = s.length();
		}

	cout<<"Longest read ruel:\n";
	cout<< longest << endl;

	cout<<"Press any key to continue!\n";
	cin.get();

	return 0;
}

What is it that I have to enter into this part to let it work when I execute this program:

getline(cin, filename, '\n');

:?:

>am I correct in assuming that where ("aa") is written, there could/should be a link like this in for example: C:/MyPrograms/Microsoft Visual/ ... ?

Yes, any valid path can be used as a file name, and sometimes the full path must be used, for example, if I remember correctly if the file to open isn't in the same directory as the file that contains the compiler then you must use the full path to the file to open.

>What is it that I have to enter into this part to let it work when I execute this program:

int the example you use, filename can be any STL string object that is in scope.

>>a variable of ifstream

Yes, ff would be a variable of type ifstream in this example. To be used, the ifstream object would need to be associated with a file. This can be done at time of declaration of the ifstream object or with the i(f?)stream open() method

>while (getline(in, s, '\n'), !in.fail())

This is an invalid expression. Conditionals (the stuff between the () after if/else if/else, while, do/while, and terminal conditions of for loops)must resolve to a single result. Therefore, you could replace the comma after the closing bracket designating the parameter for the call of getline() in the above expression with an operator such as || or && to make it valid. However, getlne() will fail and be evaluated as false, thus terminating the while loop whenever it can't read additional information secondary to a faulty file (rare in my experience) or because it encounters EOF or any condition that will cause the ifstream in to fail as well. Therefore, the !in.fail() call is really superfluous, and the better option would be to just remove it and go with the getline() call alone as the only conditional. Then, when the while loop fails, you may want to determine why, which would give you an opportunity to correct/reset/etc. if you want/need to.

>if I remember correctly if the file to open isn't in the same directory as the file that contains the compiler
Typically, the file will be relative to the current working directory, if you're in a command line environment.

>This is an invalid expression.
It looks fine to me.

>Conditionals must resolve to a single result.
And the comma operator doesn't work this way? As I understand it, the comma operator evaluates from left to right, and the result of the rightmost expression is the result of the condition. It's unconventional to use the comma operator like this, but perfectly legal.

>and the better option would be to just remove it and go with the getline() call alone as the only conditional
At least we agree on something. Using in.fail() as a loop condition is a dangerous practice, and unless you know the pitfalls, it should be avoided entirely. Of course, anyone who knows the pitfalls won't use it in the first place. ;)

Always happy to improve my knowledge base. Thank you.

>relative to the current working directory, if you're in a command line environment

And if you aren't in a command line environment?

Is the definition of the current working directory relative to the location of the compiler or the location of the file within which the ifstream is declared/opened?

>As I understand it, the comma operator evaluates from left to right, and the result of the rightmost expression is the result of the condition.

If I understand your explanation correctly, then in the example at hand, the call to getline() is irrelevant to terminating the while loop since the comma operator will return true/false based on the rightmost expression, which would the call to fail()?

>Yes, any valid path can be used as a file name, and sometimes the full path must be used, for example, if I remember correctly if the file to open isn't in the same directory as the file that contains the compiler then you must use the full path to the file to open.

Ok, just imagine that this is the file I wanted to work with: C:\Documents and Settings\Johan Berntzen\Mijn documenten\Solicitaties\Sol.Devlop

Can I enter this during the execution of the program or is this only possible when I go to Start --> Execute --> cmd --> cd c:\ ?

If this is the case, what do I enter after that, the full line: C:\Documents and Settings\Johan Berntzen\Mijn documenten\Solicitaties\Sol.Devlop ???


>in the example you use, filename can be any STL string object that is in scope.

Euh, do you mean by STL (Standard Template Library), and I know this might be a stupid question, but what does 'in scope' mean :o

Sorry to ask this probably simple questions for you all, I just want to try out this example to see what happens and try several things out, thing is, I can't get this bloody thing to work :confused:

>And if you aren't in a command line environment?
Then the current working directory is in the window that you clicked on the program icon. ;)

>Is the definition of the current working directory relative to the location
>of the compiler or the location of the file within which the ifstream is declared/opened?
Relative to wherever you are in the directory structure when you invoke the program.

>the call to getline() is irrelevant to terminating the while loop since the
>comma operator will return true/false based on the rightmost expression, which would the call to fail()?
Bingo!

>Can I enter this during the execution of the program
Absolutely. In fact, that's how the second example in this thread goes about it. However, the relative path may not always work, so you can just supply the full path and call it good.

>do you mean by STL (Standard Template Library)
Yes, though "Standard template library" is technically an outdated term for the standard library.

>but what does 'in scope' mean

for ( int i = 0; i < 10; i++ ) {
  // i is in scope, thus visible and accessible
  std::cout<< i <<'\n';
}

// i is out of scope, you can't use it
std::cout<< i <<'\n';

Pffffffff, I don't understand, when I execute the program with Ctrl + F5 and then enter the correct path: C:\Documents and Settings\Johan Berntzen\Mijn documenten\Sollicitaties\Sol.AKN

I allways get the message "Can't open the inputfile" :?:

Sol.AKN is a word document, has this got something to do with it?
I want to use this because it has text written in it, this way It can determine wich the largest alinea is in this text.

I'm obviously doing something wrong, but what :-|

try to use a filname without space :)
it worked for me then

Hi zyruz,

You mean like this:
C:\DocumentsandSettings\JohanBerntzen\Mijndocumenten\Sollicitaties\Sol.AKN

Yep, tried it, no luck.

Also tried,

C:\documentsandsettings\johanberntzen\mijndocumenten\sollicitaties\sol.akn

C:/DocumentsandSettings/JohanBerntzen/Mijndocumenten/Sollicitaties/Sol.AKN

C:\DocumentsandSettings\JohanBerntzen\Mijndocumenten\Sollicitaties\SolAKN

C:/Documents and Settings/Johan Berntzen/Mijn documenten/Sollicitaties/Sol.AKN

C:\Sol.AKN

C:\solakn

Not one was correct :confused:

And the path exists, I'm 100% certain about that :!:

>C:\Sol.AKN
Did you move the file to your root directory when you tried this? If not, do so and let us know how it goes. There shouldn't be any problem with opening the file if it's there and you're giving the stream a path that it likes. Getting something meaningful out of the open stream is another matter entirely. :)

Okie dokie, post the code you're using then.

try SOL.akn.doc

(word documents have the .doc extention, windows hides known extentions)

Note you will have a lot more luck saving something in notepad and opening the .txt since .doc is a binary format not a text format, it will basicly just output junk

try SOL.akn.doc

(word documents have the .doc extention, windows hides known extentions)

Note: you will have a lot more luck saving something in notepad and opening the .txt since .doc is a binary format not a text format, it will basicly just output junk

also if you don't want windows hiding extentions in explorer go to
Tools > Folder Options > View and unselect "Hide extentions for known file types"

@ Narue, I used the second example in this thread to try it out.

@ Paul.Esson, yep, that worked like a charm, tried it out with a Word document and I really got a bunch of gunk, then made a test document with notepad in wich I entered several sentences, worked like a charm :!:

Going to try some stuff out now and see how the code responds :cheesy:

Thank you all for your time and effort :)

good stuff and good luck :)

A good thing to try with that code would be to attempt to write a word count.

As it's topically related, can I compare the value of a FILE* object to 0xcdcdcdcd to determine if the program has exited without opening the file, or is it just too much of a risk to rely on the default value of the declared FILE* object?

How about starting a new thread and posting a bit of sample code and usage? My mindreading abilities are diminished lately.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.