contents of dinamically allocated arrays are not being copied correctly to the file......

here is the code:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
#include<conio.h>

class data
{
public:
int *n;
char **c;
void allocate(int,int);
void dealloc(int);
};

void data::allocate(int a, int b)
{n = new int[a];
c = new char *[b];
for(int i=0;i<b;i++)
c[i] = new char[80];
}
void data::dealloc(int b)
{
delete [] n;
for(int i=0;i<b;i++)
delete [] c[i];
delete [] c;
}


main()
{
clrscr();
int a,b,r,i,j,k;
data x;
fstream f,d;
char name[20],nd[20];
y:
cout<<"enter the name of record file to be opened ";
gets(name);

f.open(name,ios::in);

if(!f)
{
cout<<"\ncould not open file, enter valid file name \n\n";
goto y;
}

f>>a>>b;
f.close();
x.allocate(a,b);
cout<<"how many records do you want to enter";
cin>>r;
cout<<"\nname the data file to be created or opened ";
gets(nd);
d.open(nd,ios::out|ios::app);

if(!d.tellp())
d<<name<<endl;

for(i=1;i<=r;i++)
{cout<<"enter record no. "<<i;
 cout<<"\nenter "<<a<<" integer fields ";
 for(j=0;j<a;j++)
  {
  cin>>x.n[j];
  }
 cout<<"\nenter "<<b<<" strings ";
 for(k=0;k<b;k++)
  {
  gets(x.c[k]);
  }
 d.write((char*)&x, sizeof(x));
}

x.dealloc(b);
d.close();
getch();
return 0;
}
someone pls reply...........
This is some hideous code. You have created code that is very hard to read and as such it's very difficult to guess what you're trying to do, and if we can't guess what you're trying to do, helping you fix it is going to be impossible.

You are not being charged by the letter. It is foolish to create variables like this:
int a,b,r,i,j,k;
What is a? What is b? What is r? What is i? What is j? What is k? Get the point? I should be able to read a variable name and know what it is for. Help us to help you; write code that people can actually read.

If I had to guess, I'd say you've gone wrong here:
d.write((char*)&x, sizeof(x));
where you are trying to write the object x to file, but you only write a single byte of it. This seems odd as x is more than one byte in size.
main problem here is i want write the contents of object x to a file,
but when i dynamically allocate the variables of structure the data is not written correctly,
bt if i take for eg.

int n[10];
char c[10][80];
instead of *n and **c.
i get the desired result.
Last edited on
You have a structural problem. Your class data should be initialised by a constructor, cleaned up by a destructor and you should probably disable copies. And of course, the member data should be private. That would force you to think about what the object represents and how it's used.

Regarding file access, only use read/write on binary files. You're not using binary files and you've written the header using the formatted output, so why not the numbers?


The problem is that your object contains pointers to other places in memory that you have to get the data from, and write that data to file. Your object doesn't contain that data; it only contains a POINTER to that data, so if you just write your object, you only write the POINTER.

Here is a simplified version of your problem:

1
2
3
4
5
6
7
8
9
class works
{
  int a[10];
}

class doesNotWork
{
  int* a;
}



So, what happens if I write the first object to file? I get all those ten integer values written. What happens if I write the second object to file? I get that one pointer written to file. Anything it points to is not written.
Last edited on
so wht should i do to write the data even if i dynamically allocate class variables?
so wht should i do to write the data even if i dynamically allocate class variables?
You could start by telling us what you're trying to do rather than saying "this doesn't work".

That way we can check that what you've done matches what you ought to be doing.
Write a function to deal with it. It should be a class function.

It will do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
void data::writeSelfToFile(string filename)
{

  ofstream outputFile(filename.c_str());

  for (int looper=0; looper < sizeOfArraya; ++looper)
  {
    ofstream << a[looper];
  }

  // similar for other arrays
}
actually i have to do these ques:
a) There are two data types - integer and ascii string[upto 80 characters], either of which can form a record field. You are supposed to form a user defined record file. Ask user -- 1.the name of file, 2. The number of fields in record, type of each record field(int or string). Save this information for later retrieval in a record file.
b)Write a program to ask user a record filename. Load the record structure from the record file; Ask user number of records to enter data for. Get data for each record and fields and save them in a data file along with record file name.
c)Ask user the name of data file to parse and display data from. Display the Record information in start and then all data stored in data file record by record and field by field.


the code i have posted is for 2nd part.
Great. Don't forget to do the first part and the third part as well.

You would find it much easier if you used vectors.
Topic archived. No new replies allowed.