473,395 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Having problems reading data from files

namcintosh
I really need some help.

I am trying to read some information from a file in C++. Here is the program that I wrote. (Beware, I am very new to this, so don't freak out if the program seems a little off).

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <conio>
  3. #include <fstream>            
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.        ifstream inFile;
  11.        int people;
  12.  
  13.        inFile.open("f:\\people.dat");
  14.        cout <<"Reading information from the file. \n\n";
  15.  
  16.        inFile >> people;
  17.        cout << people <<endl;
  18.  
  19.        inFile >> people;
  20.        cout << people << endl;
  21.  
  22.        inFile >> people;
  23.        cout << people << endl;
  24.  
  25.        inFile.close();
  26.        cout << "\nDone.\n";
  27.  
  28.        getch();
  29.        return 0;
The data in the file is:
2000 4000 5000 9000 14000 18000

However, when I compile my program, all I get is this:

Reading information from the file.

4219055
3771088
3771088

Done.

Why am I seeing two different types of data? And what can I do to fix this??

Any output is appreciated.
Apr 5 '07 #1
8 2313
Ganon11
3,652 Expert 2GB
Are you sure it's opening the file correctly? Could the file name be mispelled either in the file or in your program?
Apr 5 '07 #2
Are you sure it's opening the file correctly? Could the file name be mispelled either in the file or in your program?
I have the file saved on my hard drive. The file location is f:\\people.dat. My program runs, but the numbers on my program is extremely messed up, and I don't know how they got there.
Apr 5 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Probably the file is not opening.

Do this:

inFile.open("f:\\people.dat");
if (inFile.fail())
{
cout << "File failed to open - bailing" << endl;
return 1;
}

I ran your code and it works fine.
Apr 5 '07 #4
Probably the file is not opening.

Do this:

inFile.open("f:\\people.dat");
if (inFile.fail())
{
cout << "File failed to open - bailing" << endl;
return 1;
}

I ran your code and it works fine.

Well, I did that, but all that came up was a black screen (which flashed for about 1 second) and that was it. It still didn't come out right. What am I doing wrong??
Apr 5 '07 #5
sicarie
4,677 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. int a;
  2. inFile.open("f:\\people.dat");
  3. if (inFile.fail())
  4. {
  5.         cout << "File failed to open - bailing (Press enter to continue)" << endl;
  6.         return 1;
  7. }
  8. cin >> a;
  9.  
Try the above - it'll keep that screen open until you hit enter, but if it's flashing up, I'm betting it's trying to display the above message, which means it's not finding the file (or not able to open the .dat filetype).
Apr 5 '07 #6
Expand|Select|Wrap|Line Numbers
  1. int a;
  2. inFile.open("f:\\people.dat");
  3. if (inFile.fail())
  4. {
  5.         cout << "File failed to open - bailing (Press enter to continue)" << endl;
  6.         return 1;
  7. }
  8. cin >> a;
  9.  
Try the above - it'll keep that screen open until you hit enter, but if it's flashing up, I'm betting it's trying to display the above message, which means it's not finding the file (or not able to open the .dat filetype).

So, should I save my file to another location (like my hard drive) and try to run the program again? The file that I am trying to read from is located on my flash drive.
Apr 5 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
About your flashing black screen.

That was your program executing.

You see, Visual Studio has two kinds of builds: a debug build where the .exe has code in it to run the debugger and a release build without that debugger code.

The default is debug. Probably this is your case.

Next, when you run tour program, you are selecting "Start" from the "Debug" menu. Visual Studio sees the debug code in your exe and ASSUMES you are using your debugger so ti just runs the code expecting you have inserted any pauses. Hence the black screen as your program screams by becuse you are not using your debugger.

The correct this is to select "Start Without Debugging". Im this case, you are telling Visual Studio that while it is a debug build you are no usiong your debugger. Visual Studio will run the code and at the end of main() will pause with:

Press Any Key To Continue...

on your screen so you can see what happened.
Apr 6 '07 #8
About your flashing black screen.

That was your program executing.

You see, Visual Studio has two kinds of builds: a debug build where the .exe has code in it to run the debugger and a release build without that debugger code.

The default is debug. Probably this is your case.

Next, when you run tour program, you are selecting "Start" from the "Debug" menu. Visual Studio sees the debug code in your exe and ASSUMES you are using your debugger so ti just runs the code expecting you have inserted any pauses. Hence the black screen as your program screams by becuse you are not using your debugger.

The correct this is to select "Start Without Debugging". Im this case, you are telling Visual Studio that while it is a debug build you are no usiong your debugger. Visual Studio will run the code and at the end of main() will pause with:

Press Any Key To Continue...

on your screen so you can see what happened.
I got it to open. Thanks!!:-)
Apr 18 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: MLH | last post by:
I am having failures processing the following command and I wonder if you can tell me what I must do in order to have success. When I try to run source mysql_dump.sql.txt ==> it is a problem...
4
by: Erpman | last post by:
I am trying to access the data with in a wav file. I am testing with very small files in order to keep the code simple to start with. Basically, im writing the entire wav file to a byte using a...
6
by: an0011 | last post by:
Hi static double *var = (double*) malloc(100000*sizeof(double)); is used among other memory allocations to store data that comes from a ..mat file The declarations are done on the main .cpp...
3
by: akang2005 | last post by:
When I write 'double' data to the file, it seems working fine, but when I read it later, it returns a eof when it encounters a particular number even the file is not at the end yet. However, while...
9
by: Sheldon | last post by:
Good day Everyone, I am a still very new at learning C and I have thrown myself in the deep end. I started with a simple program and kept widening the scope. This has taught me many things about...
2
by: patrickdepinguin | last post by:
Hi, I use zlib to write data structures to a compressed file, using the gzwrite function. Afterwards I read the data back with gzread. I notice that this works well when the data written is not...
12
by: xamdam | last post by:
Hi fellas, I am experiencing problems reading a 2GB zipfile consisting of multiple zipped files. I found a thread http://mail.python.org/pipermail/python-dev/2005-April/053027.html that mentions...
6
by: Pep | last post by:
Firstly, I'm not sure if this is the right group for this query, so please forgive me if I am wrong. My problem is that most users I distribute my software to cannot install it on their systems...
3
by: DaveJ | last post by:
I have these two related problems. First one is that I have a webservice that returns a DataSet that works the first time I use it, maybe twice, and then after that fails to work for a few...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.