Thread: i'm stuck

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    1

    i'm stuck

    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.
    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;        
    
    }
    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.

    thanx
    Last edited by flanders; 08-24-2006 at 06:21 AM.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    sqm_ is a pointer to an array of segments, to read or write all segments you will need to loop from sqm_[0] to sqm_[count_-1] and perform the correct operation on each. The C++ way to do this is to overload the segment types ostream << operator and istream >>. keeping with your mixed theme you would probably want .read(FILE *) and .write(FILE *) methods to be added to segment.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int disk::access (const char fname[]){
    //...
    	char c;
    //...
    	do{
    //...
    	}while(c!=EOF);
    //...
    }
    EOF is an int value - you can't store it in a char.
    FAQ > Explanations of... > Definition of EOF and how to use it effectively
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM