www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/11/14/18:00:14

From: dontmailme AT iname DOT com (Steamer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: problem reading file
Date: Tue, 14 Nov 2000 22:50:34 GMT
Organization: always disorganized
Lines: 51
Message-ID: <3a11c19e.51139068@news.freeserve.net>
References: <aYgQ5.11$BL DOT 145 AT bgtnsc05-news DOT ops DOT worldnet DOT att DOT net>
NNTP-Posting-Host: modem-241.false-clownfish.dialup.pol.co.uk
X-Trace: newsg4.svr.pol.co.uk 974242235 24449 62.137.9.241 (14 Nov 2000 22:50:35 GMT)
NNTP-Posting-Date: 14 Nov 2000 22:50:35 GMT
X-Complaints-To: abuse AT theplanet DOT net
X-Newsreader: Forte Free Agent 1.11/32.235
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Minoff wrote:

> I'm trying to get this dos app to open and display an already existing .dat
> file using C++. The file resides in the same folder as the .cpp files. Here
> is the
> code I  am working with:
> # include <iostream.h>
> # include <fstream.h>
> void main ()
> {
> ifstream File;
> 
>  File.open("transactions.dat",ios::in);
> 
>  if (!File.fail())
>  {
>   File>> ;
>   while (!File.eof())
>  }
> 
>  File.close ();
> }

You have lots of mistakes there.  

> Anyone know what I need to add to display the data that is in the .dat file?

I don't know what format your data file is in, so the following
probably won't be appropriate, but it should give you an idea of
what to do:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream File;
    string s;
    File.open("transactions.dat", ios::in);
    if (File.fail())
	cerr << "Failed to open file!\n";
    else {
	while (File >> s)
	    cout << s << '\n';
	if (!File.eof())
	    cerr << "Error reading file!\n";
	File.close();
    }
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019