473,834 Members | 1,740 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 1439

"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
13786
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
2374
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
3399
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
3741
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
6253
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
18069
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
7242
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
5019
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
3235
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
15338
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
9649
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
10515
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10554
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
10224
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
9338
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...
0
6960
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
5799
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3985
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3084
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.