fstream and '&'

I try to compile my code from a book, can be seen at here (page 49) :
http://books.google.com/books?id=4jHUX-7Qw-UC&pg=PA34&lpg=PA34&dq=where+I+can+find+inputfile+for+common.cc+computational+granular&source=bl&ots=Tzom6aft9o&sig=cuHCSV2XoKC2KcC5Ukk6nS4kqg0&hl=en&ei=EcgSTv3YI4j3rQetm6yIBA&sa=X&oi=book_result&ct=result&resnum=1&ved=0CBQQ6AEwAA#v=onepage&q&f=false

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <fstream.h>
#include <stdlib.h>
void dump_particle(ofstream & os, double x, double y, double vx, double vy, double radius, double mass, int type)
{   os << x << "\t" << y << "\t0\t" << vx << "\t" << vy << "\t0\t" << radius << "\t" << mass << "\t" << type << "\t0\t0\t0\t0\t0\t0\t0\t0\n";
}

int main()
{
    ofstream fout("closed_hopper.random");
    fout << "#gravity: 0 -9.81 0\n";
    fout << "#timestep: 1e-6\n";
    fout << "#nstep: 5000000\n";
    fout << "#nprint: 1000\n";
    fout << "#nenergy: 1000\n";
    fout << "#Time: 0\n";
    for(int i=0;i<11;i++) dump_particle(fout,0.45+i*0.01,0.19,0,0,0.005,1,1);
    for(int i=0;i<50;i++){
        dump_particle(fout,0.55+(i+0.5)*0.005,i*0.01+0.2,0,0,0.005,1,1);
        dump_particle(fout,0.45-(i+0.5)*0.005,i*0.01+0.2,0,0,0.005,1,1);
    }
    const double Rmax=0.006, Rmin=0.004;

    for(int i=0;i<67;i++){
        for(int k=0;k<30;k++){
            double centerx=0.3115+0.013*i;
            double centery=0.6+0.013*k;
            double z=drand48();
            double r=Rmin*Rmax/(Rmax-z*(Rmax-Rmin));
            dump_particle(fout,centerx,centery,0,0,r,r*r/(Rmax*Rmax),0);
        }
    }
}


I'm using codeblocks. I got many error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
D:\CodeBlocks\init file\main.c|1|fstream.h: No such file or directory|
D:\CodeBlocks\init file\main.c|3|error: syntax error before '&' token|
D:\CodeBlocks\init file\main.c||In function `dump_particle':|
D:\CodeBlocks\init file\main.c|4|error: `os' undeclared (first use in this function)|
D:\CodeBlocks\init file\main.c|4|error: (Each undeclared identifier is reported only once|
D:\CodeBlocks\init file\main.c|4|error: for each function it appears in.)|
D:\CodeBlocks\init file\main.c|4|error: `x' undeclared (first use in this function)|
D:\CodeBlocks\init file\main.c|4|error: `y' undeclared (first use in this function)|
D:\CodeBlocks\init file\main.c|4|error: `vx' undeclared (first use in this function)|
D:\CodeBlocks\init file\main.c|4|error: `vy' undeclared (first use in this function)|
D:\CodeBlocks\init file\main.c|4|error: `radius' undeclared (first use in this function)|
D:\CodeBlocks\init file\main.c|4|error: `mass' undeclared (first use in this function)|
D:\CodeBlocks\init file\main.c|4|error: `type' undeclared (first use in this function)|
D:\CodeBlocks\init file\main.c||In function `main':|
D:\CodeBlocks\init file\main.c|9|error: `ofstream' undeclared (first use in this function)|
D:\CodeBlocks\init file\main.c|9|error: syntax error before "fout"|
D:\CodeBlocks\init file\main.c|10|error: `fout' undeclared (first use in this function)|
D:\CodeBlocks\init file\main.c|16|error: 'for' loop initial declaration used outside C99 mode|
D:\CodeBlocks\init file\main.c|17|error: redefinition of 'i'|
D:\CodeBlocks\init file\main.c|16|error: previous definition of 'i' was here|
D:\CodeBlocks\init file\main.c|17|error: 'for' loop initial declaration used outside C99 mode|
D:\CodeBlocks\init file\main.c|23|error: redefinition of 'i'|
D:\CodeBlocks\init file\main.c|17|error: previous definition of 'i' was here|
D:\CodeBlocks\init file\main.c|23|error: 'for' loop initial declaration used outside C99 mode|
D:\CodeBlocks\init file\main.c|24|error: 'for' loop initial declaration used outside C99 mode|
||=== Build finished: 23 errors, 0 warnings ===|


The most that I can't understand is about fstream and '&'.
can anyone help me?

1
2
3
4
5
6
7
// change these...
//#include <fstream.h>
//#include <stdlib.h>

// to these...
#include <fstream>
#include <cstdlib> 


EDIT:

Also, you're using C++, but your filename has a .c extension (C). The above code will not compile in C. You must change the extension to a C++ extension (.cpp will work)
Last edited on
1
2
3
4
5
6
7
8
9
10
I changed
#include <fstream.h>
#include <stdlib.h>

with this 

#include <iostream>
#include <stdlib.h> 
#include <fstream>
using namespace std;


and thats work di my Dev-C++. also changed the extension file into .cpp
btw, thanks a lot :)
Topic archived. No new replies allowed.