|
C++ fstream problems |
relay01
Member #6,988
March 2006
|
Hi everyone, Could somebody tell me what I'm doing wrong and/or show me how do it right? The project is due Monday so I'm kindof in a bind.
_____________________________________ |
adam450
Member #5,460
January 2005
|
Post the code, make sure they are looking in the same folder. And of course make sure you call close(), when you done with each function. |
bamccaig
Member #7,536
July 2006
|
What language is this? Why is the first letter in Class, Private, and Public uppercase? I would fail you for lack of spacing, poor formatting, and poor variable names. Also, post the entire program so we don't have to write our own implementation to debug it. And I think your example is missing a semi-colon after the constructor declaration. relay01 said:
Class Pdisk { Public: Pdisk(int numberofblocks, int blocksize, string& diskname) // OH NOES! int getblock(int blocknumber, string buffer);
Also I think you're missing a space in the pdisk.h #include: relay01 said: #include"pdisk.h" Also you named an ofstream object iofile... relay01 said: ofstream iofile(diskname.c_str()); You should also declare your variables at the beginning of functions. Clean up your code and come back with all of it. And if they are in separate files just copy and paste each into it's own set of code tags. (And format it next time) -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
ImLeftFooted
Member #3,935
October 2003
|
... string& diskname) Make this code into: ... const string& diskname) Why are you trying to open files for reading and writing at the same time? IIRC windows doesn't support this. ifstream iofile; iofile.open(this->diskname.c_str(), ios::in | ios::out); Why are you trying to output using an ifstream? This is not possible. |
bamccaig
Member #7,536
July 2006
|
Dustin Dettmer said: Why are you trying to open files for reading and writing at the same time? IIRC windows doesn't support this. Yes it does... Yeah, looking again your code is all messed up. How about we start with what your class has to do with a disk? -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
adam450
Member #5,460
January 2005
|
"there is a function that checks to see if a file exists, if it doesn't, then it creates it" -When you call ofstream file( "filename.txt"); it already creates that file if its not a file, so why are you checking if its valid? I mean since you dont care cuz you create it anyway if it doesnt exist. ***IN your constructor for your class, you never set the data member string diskname. //yes windows suppports reading and writing. Useful for appending mainly I guess. |
ImLeftFooted
Member #3,935
October 2003
|
Ah ok so it is supported. But do you actually need to read and write the file at the same time? Sounds like uneeded complexity if you don't actually really need it. |
relay01
Member #6,988
March 2006
|
During the time, I got it working. I apologize to everyone for the lousy code. Believe it or not, though I technically wrote it, it's not my code. A lot of this stuff was copied off the board lectures in my class. Oh god, what is this guy doing? the whole class implementation in comments on the top is his, the one I'm using actually compiles and works. My new problem is with the
Now, the backstory for the code. "Pdisk" is a class that is supposed to represent a Psuedodisk, or, well, a data disk being represented by a text file. The project is to implement this. So this is where the number of blocks and block size come from. This function is supposed to go through the file and put whatever string is passed to it, into the file. And just for the purpose of hopefully redeeming myself, here's MY class declaration.
_____________________________________ |
adam450
Member #5,460
January 2005
|
1st, I suggest you use gamedev.net. It's alot wider of use for non-allegro questions. 2. DO you know what a class does? you have data members such as blocksize, which you have to instantiate. So there are alot of things wrong. In your constructor: Pdisk::Pdisk(int numberofblocks, int blocksize, string& diskname) change those variable names like such, and of course you need to set the data members. Basically do this and it should work Pdisk::Pdisk(int _numberofblocks, int _blocksize, string& _diskname) |
ImLeftFooted
Member #3,935
October 2003
|
Do that OR... Pdisk:Pdisk(int numberofblocks, int blocksize, string& diskname) : numberofblocks(numberofblocks), blocksize(blocksize), diskname(diskname) { int countisize = 1; .....the rest of your code. Anyway, back to this putblock function. Ugh! This stuff is getting messy.. Ok heres my first pass with observations:
Alright, now heres my version of the method:
|
|