I have a program disk which has a (has a ) relationship with segment. I need to write all the segments stored in the disk to a file, but I'm able to write only the first element in segment instead of all the segment elements contained in disk.
here is a partial code from the disk program.
I can't seem to return all of the segment array elements in this fn ->const segment* disk::get_all_segments() const. to be written to a file. I was hoping someone could shed light on how I might possibly solve this problem.Code:#include <stdio.h> #include <string.h> #include <ctype.h> #include "disk.h" disk::disk(int num_of_segments, const char* mode){ size_=num_of_segments; sgm_ = new segment[size_]; //initializes num_of_segments in a disk for(int i=0; i<size_; i++){ sgm_[i].initialize(); } count_=0; if( strcmp(mode_, "w")!=0||strcmp(mode_, "a")!=0){ //mode="w"; strcpy(mode_, "w"); } printf("the mode is set to %s, and the #ofsegments is %d\n", mode_, num_of_segments); } disk::disk(){ sgm_ = new segment[20]; //initialize 20 segments in a disk for(int i=0; i<20; i++){ sgm_[i].initialize(); } size_=20; //keeps track of how many are initialized count_=0; //keeps track of how many are added strcpy(mode_, "w"); //initialize disk access mode to w } disk::~disk(){ delete[] sgm_; } const char* disk::get_mode() const{ return mode_; } segment disk::get_segment(int pos) const{ return sgm_[pos]; } int disk::get_segment_count() const{ return count_; } const segment* disk::get_all_segments() const{ return sgm_; } int disk::access (const char fname[]){ int char_count=77; int actual_count=0; //just to pass the tests. char c; //printf("the file name is %s, and the mode is %s\n", fname, mode_); if(strcmp(mode_, "w")==0){ printf("sgm to be written is %s\n", sgm_); fp=fopen(fname, "w"); fprintf(fp, "%s\n",sgm_); fclose(fp); } if(strcmp(mode_,"a")==0){ fp=fopen(fname, "a"); fprintf(fp, "%s\n", sgm_); fclose(fp); } fp=fopen(fname, "r"); do{ c=fgetc(fp); if(c!=' ' && c!='\n') actual_count++; }while(c!=EOF); fclose(fp); return actual_count; }
thanx



LinkBack URL
About LinkBacks


