Reading data from files

N

Naya

Hello, everyone!!!

Well, I have a situation here. I am trying to read this data from a
file, but the wrong values keep spitting out at me. Here's what I
mean:

Program:
int main()
{
ifstream inFile;
int people;

inFile.open("people.dat");
cout <<"Reading information from the file. \n\n";

inFile >> people;
cout << people <<endl;

inFile >> people;
cout << people << endl;

inFile >> people;
cout << people << endl;

inFile.close();
cout << "\nDone.\n";

getch();
return 0;
}

My output goes like this:
Reading information from the file.

4219055
4754128
4754128

Done.

However, the data that is in the file is as follows:
2000 4000 5000 9000 14000 18000

How can I get this exact output to appear on my output screen?

PLEASE HELP!!
 
L

Lars Weber

Naya said:
Hello, everyone!!!

Program:
int main()
{
ifstream inFile;
int people;

inFile.open("people.dat");
cout <<"Reading information from the file. \n\n";

inFile >> people;
cout << people <<endl;

inFile >> people;
cout << people << endl;

inFile >> people;
cout << people << endl;

inFile.close();
cout << "\nDone.\n";

getch();
return 0;
}

My output goes like this:
Reading information from the file.

4219055
4754128
4754128

Done.

However, the data that is in the file is as follows:
2000 4000 5000 9000 14000 18000

How can I get this exact output to appear on my output screen?

PLEASE HELP!!

When you read data from a ifstream you'll get them as binary data...
Your "people" variable is a Integer. if you just read the data into "people"
using ">>" this is not what you want.

Read the data into a string first and then cast this data to integer.
http://www.mkssoftware.com/docs/man3/strtol.3.asp

Hope this helps,
Happy easter,

Lars
 
U

utab

Program:
int main()
{
ifstream inFile;
int people;

inFile.open("people.dat");
cout <<"Reading information from the file. \n\n";
inFile >> people;
cout << people <<endl;

inFile >> people;
cout << people << endl;

inFile >> people;
cout << people << endl;

inFile.close();
cout << "\nDone.\n";

getch();
return 0;

}

My output goes like this:
My output is this and it is normal because you read three times from
the file

02:00 PM utab@PMA-05-013 ~/C++_working $ ./a.out
Reading information from the file.

2000
4000
5000

Done.

So it is normal. But I could not understand the values

4219055
4754128
4754128

Here is the simple version

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream inFile;
int people;

inFile.open("people.dat");
cout <<"Reading information from the file. \n\n";

while(inFile >> people)
cout << people << endl;
inFile.close();
cout << "\nDone.\n";
return 0;
}

Greetings,
Umut
 
J

Junheng Zang

Hello, everyone!!!

Well, I have a situation here. I am trying to read this data from a
file, but the wrong values keep spitting out at me. Here's what I
mean:

Program:
int main()
{
ifstream inFile;
int people;

inFile.open("people.dat");
cout <<"Reading information from the file. \n\n";

inFile >> people;
cout << people <<endl;

inFile >> people;
cout << people << endl;

inFile >> people;
cout << people << endl;

inFile.close();
cout << "\nDone.\n";

getch();
return 0;

}

My output goes like this:
Reading information from the file.

4219055
4754128
4754128

Done.

However, the data that is in the file is as follows:
2000 4000 5000 9000 14000 18000

How can I get this exact output to appear on my output screen?

PLEASE HELP!!

I have run the program in MSVC6 and got the currect output as follows,

Reading information from the file.

0012FEE0
4000
5000

Done.

Nothing is wrong.
 
J

Junheng Zang

When you read data from a ifstream you'll get them as binary data...
Your "people" variable is a Integer. if you just read the data into "people"
using ">>" this is not what you want.

Read the data into a string first and then cast this data to integer.http://www.mkssoftware.com/docs/man3/strtol.3.asp

Hope this helps,
Happy easter,

Lars

'Read the data into a string first and then cast this data' is not
necessary because ifstream will do the work automatically.
 
L

Lars Weber

Junheng said:
'Read the data into a string first and then cast this data' is not
necessary because ifstream will do the work automatically.

Oh... Good to know.

Thank you,
Lars
 
N

Naya

My output is this and it is normal because you read three times from
the file

02:00 PM utab@PMA-05-013 ~/C++_working $ ./a.out
Reading information from the file.

2000
4000
5000

Done.

So it is normal. But I could not understand the values

4219055
4754128
4754128

Here is the simple version

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream inFile;
int people;

inFile.open("people.dat");
cout <<"Reading information from the file. \n\n";

while(inFile >> people)
cout << people << endl;
inFile.close();
cout << "\nDone.\n";
return 0;

}

Greetings,
Umut- Hide quoted text -

- Show quoted text -


When I run this program, this is what happens:
Reading information from the file.


Done.

It's not reading any values.
 
A

Alf P. Steinbach

* Naya:
When I run this program, this is what happens:
Reading information from the file.


Done.

It's not reading any values.

That means it doesn't find the file (most likely), or the file is empty,
or the program doesn't have access to the file.

Place the file or a link to the file in the same directory as the
executable.

Or, depending on the OS, run the executable from the file's directory.
 
Z

Zeppe

Naya said:
Hello, everyone!!!
hi!

inFile.open("people.dat");
if(!inFile.is_open()){
std::cout << "error: the file people.dat cannot be opened\n";
return 1;
}
cout <<"Reading information from the file. \n\n";

inFile >> people;
if(!inFile.good()){
std::cout << "error: unexpeced file format\n";
return 2;
}

etc.

You have to perform checks on the input stream state. In particular, by
default the fstream doesn't raise any exception if you try to read data
from files that don't exist, are not open, etc., but the result is
undefined. That's what's happening to you. Check that the file
people.dat is in the working directory of the executable, and the
program will work.

Regards,

Zeppe
 
U

utab

When I run this program, this is what happens:
Reading information from the file.

Done.

It's not reading any values.

Check the input file(Seems the file is not there or empty , with the
version without error checks) and as mentioned before put some error
checking.(I did not put that in the previous code)

Greetings
 
A

Andre Kostur

[snip]
Here is the simple version

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream inFile;
int people;

inFile.open("people.dat");

if(!inFile.is_open()){
std::cout << "error: the file people.dat cannot be opened\n";
return 1;
}
When I run this program, this is what happens:
Reading information from the file.


Done.

It's not reading any values.

As a previous poster has mentioned (Zeppe), add the above code in as
shown and see what happens.
 
D

David Harmon

On Sun, 8 Apr 2007 13:05:30 +0000 (UTC) in comp.lang.c++, Andre
Kostur said:
if(!inFile.is_open()){
std::cout << "error: the file people.dat cannot be opened\n";
return 1;
}

Bad.

if(!inFile){
perror("people.dat");
return 1;
}
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

On Sun, 8 Apr 2007 13:05:30 +0000 (UTC) in comp.lang.c++, Andre


Bad.

if(!inFile){
perror("people.dat");
return 1;
}

That's C and would work if "inFile.open("people.dat");" had set errno,
but it does not.
 
P

Pete Becker

Erik said:

Well, yes, perror is part of the standard C library. But it's also part
of the standard C++ library.
and would work if "inFile.open("people.dat");" had set errno,
but it does not.

It's not required to. But it usually will, and when it does, using
perror can provide more information.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
B

BobR

David Harmon said:
On Sun, 8 Apr 2007 13:05:30 +0000 (UTC) in comp.lang.c++, Andre


Bad.

if(!inFile){
perror("people.dat");
return 1;
}

More Badder!

#includes <iostream>
#includes <fstream>

int main(){

std::ifstream inFile;

if( ! inFile){
std::cerr<<"you might think this will error!\n";
return 1;
}

if( ! inFile.is_open() ){
std::cerr<<"error: file is not open\n";
return 1;
}

std::cerr<<"\n done"<<std::endl;
return 0;
} // main() end

Compile, run, and show us your output, please. (OS?)
 
S

Siddhartha Gandhi

More Badder!

#includes <iostream>
#includes <fstream>

int main(){

std::ifstream inFile;

if( ! inFile){
std::cerr<<"you might think this will error!\n";
return 1;
}

if( ! inFile.is_open() ){
std::cerr<<"error: file is not open\n";
return 1;
}

std::cerr<<"\n done"<<std::endl;
return 0;
} // main() end

Compile, run, and show us your output, please. (OS?)

Eh, #includes?
 
Z

Zeppe

David said:
Bad.

if(!inFile){
perror("people.dat");
return 1;
}

Questionable.

At first, if the open() fails, I can check it with is_open() with the
same result, and the code is imho clearer. Moreover, I may prefer to
print out my phrase than the perror default (that by the way is C, and
as the others said is not reliable with streams).

Regards,

Zeppe
 
B

BobR

// > > #includes <iostream>
// > > #includes <fstream>

#include <iostream>
#include said:
Eh, #includes?

Grrrrrr!!! I started to put "// #includes go here", then decided to do it
right (which turned out wrong. ;-{ ).

Thanks for the good 'catch'.

Uhhh (excuses), I was thinking ahead to the year 2011, when the ISO was
changed to accept:
#includes <string>, <fstream>, <iostream>, 0;
<G>

I promise it will never happen again, until the next time!
 
J

Jeff

Hello, everyone!!!

Well, I have a situation here. I am trying to read this data from a
file, but the wrong values keep spitting out at me. Here's what I
mean:

Program:
int main()
{
ifstream inFile;
int people;

inFile.open("people.dat");
cout <<"Reading information from the file. \n\n";

inFile >> people;
cout << people <<endl;

inFile >> people;
cout << people << endl;

inFile >> people;
cout << people << endl;

inFile.close();
cout << "\nDone.\n";

getch();
return 0;

}

My output goes like this:
Reading information from the file.

4219055
4754128
4754128

Done.

However, the data that is in the file is as follows:
2000 4000 5000 9000 14000 18000

How can I get this exact output to appear on my output screen?

PLEASE HELP!!

hi£¬Naya£¬ this my first reply and I'm not good at english £¬hope you can
understander:) £¬ just now I had sent once£¬maybe not success¡£

I run your code in my computer£¬and the output as follows£¬
Reading information from the file.

2000
4000
5000
9000
14000
18000

Done.


I just change the path to "d:\\test\\people.dat",and the file was suer
in this folder.
 
J

Jeff

I have another question. just now, I had read Ilia Yordanov's article
<All About: File I/O in C++>. (download from :http://www.cpp-home.com/
FileIO_tutorial.doc)

In his article,there're code:

#include <fstream>
using namespace std;

int main()
{
ofstream SaveFile("cpp-home.txt");
SaveFile << "Hello World, from www.cpp-home.com and Loobian!";
SaveFile.close();
return 0;
}

he said:"This program will create the file cpp-home.txt in the
directory from where you are executing it, and will put "Hello World,
from www.cpp-home.com and Loobian!" into it."

why we could not open it as this: inFile.open("people.dat"); ???

I change it to "inFile.open("d:\\test\\people.dat");",only in this way
it can be right.


anybody can tell me the reason, thanks!!:)
 

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

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top