473,657 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Having problems reading data from files

namcintosh
67 New Member
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 2326
Ganon11
3,652 Recognized Expert Specialist
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
namcintosh
67 New Member
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 Recognized Expert Moderator Expert
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
namcintosh
67 New Member
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 Recognized Expert Moderator Specialist
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
namcintosh
67 New Member
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 Recognized Expert Moderator Expert
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
namcintosh
67 New Member
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
11495
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 for me. 1) I put the file in /home/mlh/public_html/credifree/sql_script/. 2) I made that directory my current directory 3) I typed mysql and pressed ENTER 4) I then typed source mysql_dump.sql.txt and pressed ENTER A bunch of error...
4
12794
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 fileStream. The problem is that when reading back the data word by word and printing the results on a general form, once the data chunk header has been read, i.e, "data" and "chunk size", all the rest of the bytes in the array have the value...
6
2054
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 file and then used by other ..cpp files one of those files is similar to the following:
3
2926
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 I write a different set of data, there is no problem to read it. Attached is the file: try change the #define RANGE 1000 into 10000, the code works when the number is 1000, and does not work when the number is 10000.
9
2191
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 C and some, I must admit, have not really sunk in yet. Still, I push on. Now I am taken a library of C programs that were designed to read HDF files. I work on a Unix server and in Mandrake10. The program below is most likely broken but I cannot...
2
2957
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 that much, but when there is more data to write, after a while I get data errors when reading back the data. Error in main: couldn't read stat zlib error -3: test512-20070531-18h10m02.stat.gz: data error
12
4657
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 a problem on the writing side, does such a problem exist on a reading side? I am using 2.4.1, perhaps there is a fix in a later version?
6
2032
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 due to not having the correct administrator rights. I'm perplexed over this as I cannot reasonably ask corporations to allow the local secretary to have admin or power user rights just to install my software. My software is intended for user...
3
5311
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 hours, then it works again. This is the message: "An unhandled exception of type 'System.ServiceModel.FaultException`1' occurred in mscorlib.dll"
0
8411
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.