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). - #include <iostream>
-
#include <conio>
-
#include <fstream>
-
#include <string>
-
-
using namespace std;
-
-
int main()
-
{
-
ifstream inFile;
-
int people;
-
-
inFile.open("f:\\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;
-
}
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.
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?
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.
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.
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??
sicarie 4,677
Recognized Expert Moderator Specialist - int a;
-
inFile.open("f:\\people.dat");
-
if (inFile.fail())
-
{
-
cout << "File failed to open - bailing (Press enter to continue)" << endl;
-
return 1;
-
}
-
cin >> a;
-
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).
- int a;
-
inFile.open("f:\\people.dat");
-
if (inFile.fail())
-
{
-
cout << "File failed to open - bailing (Press enter to continue)" << endl;
-
return 1;
-
}
-
cin >> a;
-
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.
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.
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!!:-)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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:
|
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.
|
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...
| |
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
|
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?
|
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...
|
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"
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |