473,770 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ifstream problem

Hi there,
I want to read an ASCII file ("magnetfile.da t") consisting of doubles into
an array. The ASCII file looks like this:

1.0 1.0 1.0 0.5
2.0 1.0 1.0 0.4
usw.

My code:

double magnefield[200]; //this is the array
int i;

ifstream magnetic_field_ array;
magnetic_field_ array.open("mag netfile.dat");
for (i=0;i<20;i++)
{
magnetic_field_ array >> magnefield[i]; //should write 1.0 , 1.0, 1.0,
0.5 ... into the array
}
magnetic_field_ array.close();

I use g++ on SuSE 9.1. The executable runs, however, when I print out my
array magnefield, strange numbers appear. What is going wrong?
By the way: my file "magnetfile.dat " is in my working directory. However,
g++ does not give our an error message if I remove the file, so I am not
sure if the program finds the file after all. Might this be the problem?

Thanks for your help,
Bernhard

Jul 22 '05 #1
1 1435

"Bernhard Hidding" <hi*****@uni-duesseldorf.de> wrote in message
news:cn******** **@news1.rz.uni-duesseldorf.de. ..
Hi there,
I want to read an ASCII file ("magnetfile.da t") consisting of doubles into
an array. The ASCII file looks like this:

1.0 1.0 1.0 0.5
2.0 1.0 1.0 0.4
usw.

My code:

double magnefield[200]; //this is the array
int i;

ifstream magnetic_field_ array;
magnetic_field_ array.open("mag netfile.dat");
for (i=0;i<20;i++)
{
magnetic_field_ array >> magnefield[i]; //should write 1.0 , 1.0, 1.0,
0.5 ... into the array
}
magnetic_field_ array.close();

I use g++ on SuSE 9.1. The executable runs, however, when I print out my
array magnefield, strange numbers appear. What is going wrong?
By the way: my file "magnetfile.dat " is in my working directory. However,
g++ does not give our an error message if I remove the file, so I am not
sure if the program finds the file after all. Might this be the problem?


It probably is. You should test to see if you open the file successfully.

ifstream magnetic_field_ array("magnetfi le.dat");
if (!magnetic_fiel d_array.is_open ())
cerr << "could not open file\n";

It is your responsibility to print an error message if you cannot open a
file, not the compilers.

john
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13780
by: Dave Johnston | last post by:
Hi, I'm currently trying to create a wrapper that uses C functions but behaves like ifstream (from fstream.h) - this is because the platform I'm using (WinCE) doesn't support streams and this is the easiest way to take a huge project across onto it. Basically, I've hit a problem. I have no idea how the ifstream class handles directories. In the code I have (which I didn't write), there are several places whereby an ifstream stream is...
1
2372
by: Jim Phelps | last post by:
Hello all, I am in a bit of a pickle using the getline function with an ifstream. It does not seem to work as advertised. Here is my scenario. In a nutshell, my code needs to pick up a fixed record length flat file that is generated by an old IBM mainframe. These data fields need to be read in by my program EXACTLY as they are represented in the file. Here is an example text file that I used in my test. ...
6
3396
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber" 10,20,30
4
3739
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream in("in.txt"); if(!in) {cout << "error\n";} in.close(); if(!in) {cout << "error\n";} in.close(); if(!in) {cout << "error\n";}
1
6252
by: inkapyrite | last post by:
Hi all. I'm using ifstream to read from a named pipe but i've encountered an annoying problem. For some reason, the program blocks on reading an ifstream's internal buffer that's only half-filled. Only when the buffer becomes full does it resume execution. Here's my test code for reading from a pipe: //(compiled with g++ -std=c++98) //--------------------------------------------- #include <iostream>
10
18017
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
3
7241
by: toton | last post by:
Hi, I want to unread a few things while I am reading a file. One solution I know is putback the characters read to the buffer. But as I need frequent moving file pointer , to a few steps back, I was thinking of seekg & tellg function. But it is not workings as I expect.... here is the test code, std::string word; std::string value;
1
5017
by: SantaClaus | last post by:
Hi all, Im getting an exception in the middle of reading a file and here is my code std::ifstream inf; const int bufSize=1024; char tbuffer; std::string filex = path; // may be C:\test.txt const char* stx = &filex; inf.open(stx,ios_base::in|std::ios::binary); if(!inf.good()) { inf.clear();
7
3233
by: Boltar | last post by:
Hi I'm using ifstream (which I hardly ever use) to read an ascii text file (containing numbers delimited by newlines) in a loop using something like: ifstream infile("filename") int value; infile >value;
2
15309
by: mpalomas | last post by:
Hi C++ folks, I have trouble to open files whose path contains non-ascii characters with std::ifstream. For instance let's say i just have a file which has Japanese characters either in the file path or the file name : 疑問.dat. The file itself does not contains unicode characters or whatever, it is a binary file, but the file name, or path, contains non-ascii characters, here it is Japanese but it could be anything else, i know...
0
9591
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
1
10002
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
9869
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
8883
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 projectplanning, coding, testing, and deploymentwithout 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...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
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
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
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.