Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6861575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:33:08+00:00 2026-05-27T02:33:08+00:00

In my class File, I’ve: class File { std::vector<char> name, timeOfCreation, timeOfLastEdit, content; std::vector<char>::const_iterator*

  • 0

In my class File, I’ve:

class File
{
    std::vector<char> name, timeOfCreation, timeOfLastEdit, content;
    std::vector<char>::const_iterator* pPos, *pPosOfEnd;
    int *pSize;
    bool *pForcedSize;
    void setTimeOfLastEdit();
    int* indexOfLastChar, *indexOfCurrentChar;

    friend class Interface;
    friend class Directory;
public:
    File(std::string, int argSize = 0, std::string arg_content = 0); // constructor
    File(); // constructor
    File(const File&); // copy constructor
    ~File(); // destructor
    char* returnName(); 

    File& operator = (const File&);
    File operator += (const int);
    File operator -= (const int);
    File& operator ++ (); // prefix
    File& operator -- (); // prefix
    File operator ++ (int); // postfix
    File operator -- (int); // postfix

    bool operator ! ();

    int operator () (char*, int);

    friend  bool operator == (const File&, const File&);
    friend  bool operator != (const File&, const File&);
    friend  bool operator >= (const File&, const File&);
    friend  bool operator <= (const File&, const File&);
    friend  bool operator < (const File&, const File&);
    friend  bool operator > (const File&, const File&);

    friend std::istream & operator >> (std::istream&, File&);
    friend std::ostream & operator << (std::ostream&, File);

};

and my operator () is like this:

int File::operator () (char* contentNextNBytes, int n)
{
    int i; int j = 0;
    std::vector<char>::const_iterator it = content.begin();
    for(j = 0; j < *indexOfCurrentChar; j++)
        ++it;

    for(i = 0; i < n; i++)
    {
        contentNextNBytes[i] = *it;
        if(i == *indexOfLastChar-1) 
        {
            contentNextNBytes[i+1] = '\0';
            *indexOfCurrentChar = i+1;
            return i+1;
        }
        ++it;
    }

    contentNextNBytes[i] = '\0';
    *indexOfCurrentChar = n;
    return n;
}

In my other class, Interface, I call the operator () with int i = dir[index](buffer, n), where n is num of bytes, and index is the index of the File in directory.

Now, as is implemented in the operator, the value *indexOfCurrentChar points to should (and does) become the position of the last character extracted from the File. However when I call the same operator again, for the same index, with int j = dir[index](buffer, n1), right when the program enters the operator {} it changes the value of *indexOfCurrentChar to 0 again, while my code should continue from the last char and read the next n1 bytes in File

Why does this happen? 🙁

Here’s part of the code I’m using to call the operator in the class Interface:

 buffer = new char[n+1];
    k = d[index](buffer, n);
    std::cout<<"Extracting of "<<k<<" bytes succeeded, and here is the content extracted:\n";
std::cout<<"\""<<buffer<<"\"";

Edit1: Here’s where I change my indexOfCurrentChar:

1) In the assignment operator = (however, I’m not calling it in my Interface class)
2) In the copy constructor (Also not using it in Interface class, and it just copies the value)
3) In the File constructor:

File::File()
{
    // stuff
    indexOfCurrentChar = new int;
} //I'm not setting its value, just allocating memory

4) In the File constructor:

File::File(std::string arg_name, int arg_size, std::string arg_content)
{
     // stuff
     indexOfCurrentChar = new int;
     *indexOfCurrentChar = 0;
    //setting it to 0, but its just at creation time
}

5) In the >> operator
//getting file content

std::cout<<"Enter file content: ";
    *object.indexOfCurrentChar = 0;
    while( in.get(c) && c != '\n');
    i = 0;
    in.get(c);
    if(*(object.pSize) == 0)
    {
        while(c != '\n')
        {
            object.content.push_back(c);
            in.get(c);  
            ++i;
        }
        *(object.pSize) = (int)object.content.size();
        *(object.pPosOfEnd) = object.content.end();
        *object.indexOfLastChar = i;
    }
    else
    {
        i = 0;
        std::vector<char>::const_iterator it = object.content.begin();
        while(c != '\n')
        {
            if(i == *object.pSize)
            {
                *object.pPosOfEnd = it;
                *object.indexOfLastChar = i;
            }
            if(i >= *object.pSize)
            {
                in.get(c);
                continue;
            }
            object.content.push_back(c);
            in.get(c);
            ++i;
        }
    }


    *(object.pPos) = object.content.begin();

And thats it 🙂

Aaaaand although I’m sorry to show this to the world, here’s my ugly Interface class method. All of my program input/output is managed through Interface.writeOutput method, which is here:

Interface::Interface()
{
    menu = new char*[12];
    menu[0] = "Welcome, please select an option from the following, by pressing the respective numbers:";
    menu[1] = "1  Create a Directory\n2  Create a single File\n3  Exit program";
    menu[2] = "Ok, now please select a further option:";
    menu[3] = "Enter the number of Files you want to import: ";
    menu[4] = "Enter the files...";
    menu[5] = "1  Write out the Directory details\n2  Extract N bytes from a desired file\n3  Remove a file with desired name from the Directory\n4  Write content of a file\n0  Go back to the beginning";
    menu[6] = "Enter the index of the file: ";
    menu[7] = "Enter how many bytes to extract: ";
    menu[8] = "Content of the extracted bytes: ";
    menu[9] = "Enter the name of the file: ";
    menu[10] = "What do you want to do next?";
    menu[11] = "Please enter a regular number...";
}


void Interface::writeMenu()
{
    Error e;
    char c, tmp[5], name[30];
    int n, k, index, i, numDir;
    char *buffer; 
    int *haveReadFiles;

    std::cout<<menu[0];
    std::cout<<"\n\n";
startMenu: 
    std::cout<<menu[1];
    std::cout<<"\n\n";
    std::cin>>c;
    if((!isdigit(c)) || ( c != '1' && c!= '2' && c!='3'))
    {
        std::cout<<"\n"<<menu[11]<<"\n\n";
        goto startMenu;
    }
    if(c == '1')
    {
        std::cout<<"Enter the name of the directory: ";
        std::cin>>name;
        Directory d(name);
        std::cout<<"\n\n\nEnter number of files of directory: ";
        std::cin>>numDir;
        haveReadFiles = new int[numDir];
        if(!haveReadFiles)
        {
            e.writeToOutput(7);
            exit(1);
        }

        for(i = 0; i < numDir; i++)
        {
        haveReadFiles[i] = 0;   
            //std::cin.ignore(5,'\n');
        //  std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
            File f;
            std::cin>>f;
            d += f;
        }

    menu1:
        std::cout<<"\n\n"<<menu[2]<<"\n\n";
menuRepeat:
        std::cout<<menu[5];
        std::cout<<"\n\n";
        std::cin>>c;
        if(!isdigit(c) || (c != '1' && c!= '2' && c!='3' && c != '4' && c != '0'))
        {
            std::cout<<"\n"<<menu[11];
            goto menu1;
        }
        switch(c)
        {
        case '0': goto startMenu;break;
        case '1': std::cout<<d;break;
        case '2': 
            {
                menu2:
                std::cout<<"\n\n"<<menu[6];

                std::cin>>tmp;
                for(i = 0; i < (int)strlen(tmp); i++)
                    if(!isdigit(tmp[i]))
                    {
                        std::cout<<"\n\n"<<menu[11];
                        goto menu2;
                    }
                index = atoi(tmp);
                menu3:
                std::cout<<"\n\n"<<menu[7];
                std::cin>>tmp;
                for(i = 0; i < (int)strlen(tmp); i++)
                    if(!isdigit(tmp[i]))
                    {
                        std::cout<<"\n\n"<<menu[11];
                        goto menu3;     
                    }
                n = atoi(tmp);


                buffer = new char[n+1];
                k = d[index](buffer, n);
                std::cout<<"Extracting of "<<k<<" bytes succeeded, and here is the content extracted:\n";
                std::cout<<"\""<<buffer<<"\"";
            } break;
        case '3':
            {
                std::cout<<"\n\nEnter the name of the file: ";
                std::cin>>name;
                d -= name;
                std::cout<<"...file removed!\n";
                std::cout<<"Directory without the removed file looks like this: \n\n"<<d;
            }break;
        case '4': 
            {
                menuFF:
                std::cout<<"\n\nEnter the index of the desired file: ";
                std::cin>>tmp;
                for(i = 0; i < (int)strlen(tmp); i++)
                    if(!isdigit(tmp[i]))
                    {
                        std::cout<<"\n\n"<<menu[11];
                        goto menuFF;
                    }
                index = atoi(tmp);
                std::cout<<"\nHere's the content:\n";
                File ff = d[index];
                std::vector<char>::const_iterator iter = ff.content.begin();
                for(; iter < ff.content.end(); ++iter)
                {
                    std::cout<<*iter;
                }
                std::cout<<"\n\n";
            }
        }
        menu4:
        std::cout<<"\n\nWhat do you want to do now?\n\n1  Exit program\n2  Go back to start menu\n3  Do more stuff with your Directory\n\n";
        std::cin>>c;
        if((!isdigit(c)) || ( c != '1' && c!= '2' && c!='3'))
        {
            std::cout<<"\n"<<menu[11];
            goto menu4;
        }
        if(c=='1')
        {
            "Thank you, goodbye...";
            exit(0);
        }
        if(c=='2')
            goto startMenu;
        if(c=='3')
            goto menuRepeat;
    }
    else if(c=='2')
    {
        std::cout<<"Enter your file...\n";
        File g;
        std::cin>>g;
        menuZZ:
        std::cout<<"\n\nHere's what you can do with your file: \n\n1  Extract N bytes of content from it\n2  Write its content out\n3  Write its properties out\n4  Return to start menu\n\n";
        std::cin>>c;
        switch(c)
        {
        case '1':
            {

                menuYY:
                std::cout<<"\n\n"<<menu[7];
                std::cin>>tmp;
                for(i = 0; i < (int)strlen(tmp); i++)
                    if(!isdigit(tmp[i]))
                    {
                        std::cout<<"\n\n"<<menu[11];
                        goto menuYY;        
                    }
                n = atoi(tmp);
                buffer = new char[n+1];
                k = g(buffer, n);
                std::cout<<"Extracting of "<<k<<" bytes succeeded, and here is the content extracted:\n";
                std::cout<<"\""<<buffer<<"\"";
                std::cout<<"\n\n";
                goto startMenu;
            } break;
        case'2':
            {
                std::vector<char>::const_iterator it = g.content.begin();
                for(; it < g.content.end(); ++it)
                {
                    std::cout<<*it;
                }
                std::cout<<"\n\n";
            }
        case'3':
            {
                std::cout<<g<<"\n\n";
                goto startMenu;
            } break;
        case'4': 
            {
                std::cout<<"\n\n";
                goto startMenu;
            } break;
        default:
            {
                std::cout<<menu[11];
                goto menuZZ;    
            }

        }
    }
    else
    {
        std::cout<<"Thank you, goodbye... ";
        exit(0);
    }
}

Edit 2:

Last but not least, the Directory::operator [] implementation:

In header: File& operator [] (int);

Implementation:

 File& Directory::operator [] (int index)
{
    std::vector<File>::const_iterator i = arr.begin();
    for(int j = 0; j < index; ++j)
        ++i;
    File* temp = new File;
    *temp = *i;
    return *temp;
    delete temp;
}

enjoy ^^

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:33:09+00:00Added an answer on May 27, 2026 at 2:33 am

    My guess is that the problem is with the Directory class and the way that it stores and returns File objects. For example, if Directory::operator[] returns a File instead of a File &, then any changes made to the returned object will not be persisted to the Directory‘s internal copy of it, so making two calls in a row like

    int i = dir[index](buffer, n);
    int j = dir[index](buffer, n1);
    

    actually causes File::operator() to be called on two different temporary File objects. This could explain why the value of *indexOfCurrentChar is not what you expect.

    EDIT

    Now that you’ve shown the implementation of Directory::operator[], I can confidently say that my suspicions were correct. Even though you changed the signature of Directory::operator[] so that it returns a File & now, your implementation still won’t behave the way you want it to because it doesn’t return a reference to the correct File object (the Directory object’s internal copy of it). The three statements

    File* temp = new File;
    *temp = *i;
    return *temp;
    

    create a new copy of the Directory object’s internal File object and return a reference to that, so any changes made to the returned File object’s member variables aren’t reflected in the corresponding entry of the Directory::arr vector. Since vector::operator[] returns a reference, changing the body of Directory::operator[] to simply

    return arr[index];
    

    should give you the desired behavior.

    I should probably also note that your current implementation of Directory::operator[] leaks memory: delete temp; never runs because return *temp; causes control to leave the function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does a class need to have the same name as it's file. Ex. class.mysql.php
class File { String name File parent; static belongsTo =[parent:File ] static hasMany =
I have two entities: @Entity public class File ....... @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id;
I have a class file that contains all the classes that are needed for
I need a CSVParser class file A Class File which parses csv and returns
I currently have a class file with the following enumeration: using System; namespace Helper
I have created a C# class file by using a XSD-file as an input.
i am using a WSDL file to create a the proxy class file, this
When I create a new class file in Flex 3 it warns me that
I've got one really big .java class file that has a lot of members.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.