Problem with file reading (ifstream)

I am trying to load the content of a file using the following code. The file has 3 words in each line. The code should read the file and output each line. Please let me know what I am doing wrong here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<fstream>
#include<string>
#include "file_p.h"

using namespace std;

int main(){
	loadContent();
	return 0;
}

void loadContent(){
	cout << "hello" << endl;
	ifstream input("file.txt", ios::in);
	string tmp1, tmp2, tmp3;

	while(input >> tmp1 >> tmp2 >> tmp3){
		cout << "::" << tmp1 << "\t" << tmp2 << "\t" << tmp3 << endl;
	}
}


"file.txt" content:
artless base-court apple-john
bawdy bat-fowling baggage
beslubbering beef-witted barnacle
If I moved things around a bit it worked for me.
I was taught your functions should be first. (30 years ago) Main should be last.
When you compile a program just like when you run a program, things at the top happen first. If main needs a value, and it's not part of main, it should be above it.

Modern compilers may not care and it may not be taught that way anymore but when it doesn't work the new way try the old way ;P

I also did not have file_p.h which is not used in this program anyway.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<fstream>
#include<string>
// #include "file_p.h"

using namespace std;

void loadContent(){
	cout << "hello" << endl;
	ifstream input("file.txt", ios::in);
	string tmp1, tmp2, tmp3;

	while(input >> tmp1 >> tmp2 >> tmp3){
		cout << "::" << tmp1 << "\t" << tmp2 << "\t" << tmp3 << endl;
	}
}

int main(){
	loadContent();
	return 0;
}



C:\Dev-Cpp\Code>test123
hello
::artless base-court apple-john
::bawdy bat-fowling baggage
::beslubbering beef-witted barnacle
Last edited on
The code looks ok, however you need to check if the stream opened correctly before you try to read from it.
I was taught your functions should be first. (30 years ago) Main should be last.

I normally prefer main() to be first. Of course this means that you will need function prototypes before main() which is good thing since it makes using multiple files easier. Function prototypes would go into an include file and the implementations would go into the various implementation files.

I also did not have file_p.h which is not used in this program anyway.

Are you sure? It might provide the prototypes for the functions which would make moving the functions before main() unnecessary.


Thank you very much for the prompt replies, everyone! As Thomas rightly pointed out, the problem was related to the opening of the file correctly. The "file.txt" was in the "src" folder of my Eclipse project directory. Therefore, modifying "file.txt" to "src/file.txt" while creating the "ifstream" instance resolved the problem.
Last edited on
Are you sure?


yes I'm sure I do not have the file_P.h

I normally prefer main() to be first. Of course this means....


Of course you do. Yes you can make a work around for most anything...
Last edited on
While it is a side-issue for this thread, I do agree with SamualAdams about the wonky ordering C programmers have been taught to use. Of course, I am still a fan of Pascal...
yes I'm sure I do not have the file_P.h

But are you sure it's not being used by this program?

Yes you can make a work around for most anything...

Actually the work around is always placing main() last.

Topic archived. No new replies allowed.