Welcome, Guest

Go Back   TechieHQ - Computer Help Forum · » Software & Internet · HTML & Coding
Reload this Page C++ ifstream pointer

C++ ifstream pointer

HTML & Coding HTML, XHTML, CSS, XML and other coding issues you may code into.

Reply
 
Thread Tools Display Modes
  #1  
Old Dec 11th, 03, 2:47 AM
haileris23
THQ Newbie
Posts: 16
Status: Offline
 
From: San Antonio, TX
Joined: Nov 2003
Rep: haileris23 is on a distinguished road to becoming a computer geek
C++ ifstream pointer

Here's the code, i just want to count the lines of a file. I get a bunch of "invalid simple type name destructor" errors..
It makes a pointer to an input stream to the file specified by the command line parameter. Then it passes the pointer to countlines() to try to count the lines. anyone know why it doesn't work??

#include <fstream.h>

#define NULL 0
#define WIDTH 257

int countlines(ifstream *in);

int main(int numArgs, char *args[])
{
if (numArgs != 2)
{
cout << "Usage:\n reader filename.ext";
return 1;
}
ifstream *fin;
fin = new ifstream(args[1], ios::in | ios::nocreate);
cout << countlines(fin);
//char buf[WIDTH];

//for(int i=0; !*fin.eof(); i++)
//{
// *fin.getline(buf, WIDTH);
// cout << buf << "\n";
//}
//
*fin.close();
return 0;
}

int countlines(ifstream *in)
{
int lines = 0;
char null[WIDTH];
while (! in.eof())
{
in.getline(null, WIDTH);
lines++;
}
in.seekg(0, ios::beg);
delete null;
return lines;
}
Reply With Quote
Advertisement
  #2  
Old Dec 11th, 03, 2:50 AM
haileris23
THQ Newbie
Posts: 16
Status: Offline
 
From: San Antonio, TX
Joined: Nov 2003
Rep: haileris23 is on a distinguished road to becoming a computer geek
Oops, thats not the code, forgot to put it back so it makes sense: the countline() function's references to the pointer have been changed.

#include <fstream.h>

#define NULL 0
#define WIDTH 257

int countlines(ifstream *in);

int main(int numArgs, char *args[])
{
if (numArgs != 2)
{
cout << "Usage:\n reader filename.ext";
return 1;
}
ifstream *fin;
fin = new ifstream(args[1], ios::in | ios::nocreate);
cout << countlines(fin);
//char buf[WIDTH];

//for(int i=0; !*fin.eof(); i++)
//{
// *fin.getline(buf, WIDTH);
// cout << buf << "\n";
//}
//
*fin.close();
return 0;
}

int countlines(ifstream *in)
{
int lines = 0;
char null[WIDTH];
while (!*in.eof())
{
*in.getline(null, WIDTH);
lines++;
}
*in.seekg(0, ios::beg);
delete null;
return lines;
}
Reply With Quote
  #3  
Old Dec 11th, 03, 5:13 PM
Core's Avatar
Core
voyeur
Posts: 1,097
Status: Offline
shield_mod.gifreviewst.png
 
From: San Antonio, TX
Joined: Jun 2003
Rep: Core is a splendid one to beholdCore is a splendid one to beholdCore is a splendid one to beholdCore is a splendid one to beholdCore is a splendid one to beholdCore is a splendid one to behold
im_gtalk.gif
Quote:

#include iostream
#include fstream
using namespace std;

static const int width = 257;

int countlines(ifstream &in);

int main(int numArgs, char *args[]) {
if (numArgs != 2) {
cout << "Usage:\n reader filename.ext";
return 1;
}
ifstream fin(args[1]);
cout << countlines(fin);

fin.close();
return 0;
}

int countlines(ifstream &in) {
int lines = 0;
char null[width];
while (!in.eof()){
in.getline(null, width);
lines++;
}
in.seekg(0, ios::beg);

return lines;
}
...you can't do "delete null;", as it's not dynamic memory.

Using an ifstream object instead of an ifstream pointer seems clearer to me.

C++ really doesn't like it when you redefine constants for it - such as NULL.

Hope that helps.



__________________
Posting Guidelines

Last edited by Core; Dec 11th, 03 at 5:28 PM.
Reply With Quote
Reply

Tags
ifstream, pointer


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mouse pointer went crazy! Ken R. General Hardware 4 Mar 14th, 04 9:24 PM


All times are GMT +1. The time now is 2:15 PM.