0

Hey i have a problem with my write and readFile methods. The methods do not do anything. The method is supposed to write the elements of a dynamic array into a file called C++. And then also for the read file, The method is supposed to read in the elements of the ArrayList.

Here is my code: Methods

//---------------------------------------------------------------------------------------
//  Name:         Array::WriteFile
//  Description:  Writes an array to disk
//  Arguments:    The Filename
//  Return Value: true on success, false on failure
//---------------------------------------------------------------------------------------
   void writeFile(string file)//saves the array elements into a text file
    {
        //sort();
        ofstream outfile(file);//allows you to write to the document passed in
        for(int i = 0; i < m_size; i++)//loops through the array
        {
            outfile << m_array[i] << endl;//saves each element into a single line in the text document
        }
        outfile.close();
    }


//---------------------------------------------------------------------------------------
//  Name:         ReadFile.
//  Description:  Reads an array from disk.
//  Arguments:    The Filename.
//  Return Value: True on success, false on failure.
//---------------------------------------------------------------------------------------
   void readFile(string file)
    {
        ifstream inFile(file);//reads the file
        string line;//creates a string to take in information from the text deocument
        int numLines=0;//number of lines read in text document so far

        while(getline(inFile, line))//loops through the text document counting how many lines are in it.
        {
            numLines++;//increments every time a line is read
        }
        Datatype object;//creates a variable of type DataType to hold whatever is in the text document be it int, float, string etc...
        inFile.clear() ;//these two lines reset the inFile and go back to the start allowing to read through it again
        inFile.seekg(0, ios::beg) ;
        for(int i = 0; i < numLines; i++)//loops for whatever the number of lines there is in the document.
        {
            inFile >> object;//pushes the line into the document into object
            push(object);//calls the push function that will push object into the array in the right order
        }
        inFile.close();

    }

If anything else is needed, just ask and i will post.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Pendo826
  • 1,002
  • 18
  • 47
  • You should pass strings by `const&` if you only want to read them, like in your example. Look at http://stackoverflow.com/questions/10836221/should-i-write-constructors-using-rvalues-for-stdstring – Bartek Banachewicz Aug 11 '12 at 15:03
  • These are member functions of a class? – tmpearce Aug 11 '12 at 15:04
  • Have you checked the value of `m_size`? – juanchopanza Aug 11 '12 at 15:07
  • 1
    After creating the output stream I'd suggest to check whether the file is opened: `if (outfile.is_open()) ...`. – Mehrwolf Aug 11 '12 at 15:07
  • Writing and reading is only a part of what i am doing with the array. I am also letting the user interact with the elements of the array. and at tmpearce yes correct. I didnt post the entire class as i didnt want StackOverflow members to be hit by 100 lines of code. – Pendo826 Aug 11 '12 at 15:08
  • `// Return Value: True on success, false on failure.`, with `void readFile(...` also isn't very precise, in fact. You might not think it's important, but in programming, everything matters. – Bartek Banachewicz Aug 11 '12 at 15:09
  • 1
    You're not checking for errors on any of your stream usages. If you did then you'd know where it was going wrong, which would help with the why. – Alan Stokes Aug 11 '12 at 15:10
  • Im sorry i am a noob c++ programmer. What is a stream usage ? – Pendo826 Aug 11 '12 at 15:13
  • just let us know what is m_array?? – perilbrain Aug 11 '12 at 15:13
  • Ok so i put if(outfile.is_open()) cout<< "The file has been written"; into the writeFile method and there is nothing being outputted. So there is nothing actually being written. – Pendo826 Aug 11 '12 at 15:15
  • m_array is the Datatype:Datatype* m_array; – Pendo826 Aug 11 '12 at 15:16
  • So stream is never opened....check if you have proper right to write in the directory. – perilbrain Aug 11 '12 at 15:25
  • 1
    @Pendo826: So your file fails to open, that's one reason why nothing gets written but there could be more. Have you checked the contents of `file`. More to the point have you stepped through your code with a debugger? – jahhaj Aug 11 '12 at 15:26
  • @Anonymous How do i check everything looks fine when i use windows to look at the folder. – Pendo826 Aug 11 '12 at 15:28
  • Actually i just found the folder was read_only so i got that bit but the file is completely empty. – Pendo826 Aug 11 '12 at 15:30
  • As I understand,your file is not being created , see if in the directory properties your account has privilege to write. – perilbrain Aug 11 '12 at 15:31
  • Yeah i got that part working but there is absolutely nothing writing to the file. – Pendo826 Aug 11 '12 at 15:35
  • @Pendo826: The next thing I would do is to check if a POD can be written to the file. Try a `outfile << 42;` an check that the file is non-empty. If this is the case you probably got the `operator<<` wrong. – Mehrwolf Aug 11 '12 at 19:29

1 Answers1

1

The problem with this was the directory permissions. Because it was only read and not write the document was never written.

Pendo826
  • 1,002
  • 18
  • 47