Damn I/O!! ( HELP PLEASE! )

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Damn I/O!! ( HELP PLEASE! )

Post by MadPumpkin »

I need a little help, im practicing file I/O and this thing has some strange problem, and im not advanced enough to figure it out,
when i run the progrem, it prompts you to type the file name ( SUPPOSED TO HAPPEN ) BUT! after that it just closes its suppoesed to prompt you for the things to be written to it ( LINE 15 )

any help much appreciated!!

Code: Select all

#include <iostream>
#include <fstream>
using namespace std;

//VARIABLES//
char filel[512];
char filen[64];

//FUNCTIONS//

int main()
{
    cout << "FILE NAME: ";
    cin >> filen;
    cout << "FILE DATA: \n";
    cin.getline(filel, 512);
    
    ofstream fileo(filen, ios::out | ios::binary);
    fileo.write(filel, 512);
    fileo.close();

    return 0;
}
Last edited by MadPumpkin on Fri Apr 03, 2009 3:48 pm, edited 1 time in total.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
PixelP
Chaos Rift Regular
Chaos Rift Regular
Posts: 153
Joined: Tue Oct 07, 2008 12:23 pm
Programming Language of Choice: c/c++
Location: sweden
Contact:

Re: Damn I/O!! ( HELP PLEASE! )

Post by PixelP »

put

Code: Select all

cin.get();
after

Code: Select all

cin >> filen;
it should do the trick.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Damn I/O!! ( HELP PLEASE! )

Post by RyanPridgeon »

Try this

Code: Select all

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//VARIABLES//
string filel;
string filen;

//FUNCTIONS//

int main()
{
cout << "FILE NAME: ";
cin >> filen;
cout << "FILE DATA: \n";
cin >> filel;

ofstream fileo(filen.c_str(), ios::out);
fileo.write(filel.c_str(), filel.size());


fileo.close();

return 0;
}

I know it's not using char*, but as you're using C++, I don't really see a reason to.

Sorry if that's not what you wanted.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Damn I/O!! ( HELP PLEASE! )

Post by MadPumpkin »

RyanPridgeon wrote:Try this

Code: Select all

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//VARIABLES//
string filel;
string filen;

//FUNCTIONS//

int main()
{
cout << "FILE NAME: ";
cin >> filen;
cout << "FILE DATA: \n";
cin >> filel;

ofstream fileo(filen.c_str(), ios::out);
fileo.write(filel.c_str(), filel.size());


fileo.close();

return 0;
}

I know it's not using char*, but as you're using C++, I don't really see a reason to.

Sorry if that's not what you wanted.
THANK YOU!!! I am stupid! i never even thought about using string!
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Damn I/O!! ( HELP PLEASE! )

Post by MadPumpkin »

when i compile this i get an error on line 18,
it says, "18 | error: expected primary-expression before ',' token" ( LINE 18 IS LABELED )
NOTE: This is a different code

Code: Select all

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//VARIABLES//
string loader;
string filen;

//FUNCTIONS//

int main()
{
    cout << "FILE NAME: ";
    cin >> filen;

    ifstream filei(filen.c_str(), ios::in);
    filei.read(loader&, filen.size()); [size=150]//LINE 18//[/size]
    filei.gcount();

    cout << filei.gcount() << " BYTES LOADED" << endl << "FILE DATA:\n" << loader << endl;

    filei.close();

    cin.get();
    cin.get();

    return 0;
}
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

Re: Damn I/O!! ( HELP PLEASE! )

Post by Maevik »

I think it's your & after the string variable in the first argument. The buffer should not be an adress, and even if it were I would think the & would have to be in front.
My love is like a Haddoken, it's downright fierce!
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Damn I/O!! ( HELP PLEASE! )

Post by Ginto8 »

Can't you just use:

Code: Select all

string cow = "Moo";
string moose = "";
ifstream read("moose.txt");
ofstream write("cow.txt");

read >> moose;
write << cow;
much easier and simpler than using binary i/o (read()/write()).

MadPumpkin: since there is the argument that sizeof(char) may change in the future, it may be a good idea to replace filel.size() with (sizeof(char) * filel.size()). ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Damn I/O!! ( HELP PLEASE! )

Post by MadPumpkin »

Ginto8 wrote:Can't you just use:

Code: Select all

string cow = "Moo";
string moose = "";
ifstream read("moose.txt");
ofstream write("cow.txt");

read >> moose;
write << cow;
much easier and simpler than using binary i/o (read()/write()).

MadPumpkin: since there is the argument that sizeof(char) may change in the future, it may be a good idea to replace filel.size() with (sizeof(char) * filel.size()). ;)
uhmm... because im practicing Binary I/O...
but any changes to code to make it so that it will fill the whole 'loader' variable would be much appreciated
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Damn I/O!! ( HELP PLEASE! )

Post by MarauderIIC »

Maevik wrote:I think it's your & after the string variable in the first argument. The buffer should not be an adress, and even if it were I would think the & would have to be in front.
Yes. filei.read(&loader, .......
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Damn I/O!! ( HELP PLEASE! )

Post by MadPumpkin »

MarauderIIC wrote:
Maevik wrote:I think it's your & after the string variable in the first argument. The buffer should not be an adress, and even if it were I would think the & would have to be in front.
Yes. filei.read(&loader, .......
well i put the '&' before "loader" now and i get this
(line 18)
error: no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::read(std::string*, size_t)'|
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
fantastico
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Fri Mar 20, 2009 4:37 am
Location: Germany

Re: Damn I/O!! ( HELP PLEASE! )

Post by fantastico »

MadPumpkin wrote: well i put the '&' before "loader" now and i get this
(line 18)
error: no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::read(std::string*, size_t)'|
That's because you are passing the function a pointer to a std::string while it's expecting a pointer to a char, where a buffer begins (char* / array of chars). I'm not too familiar with C++ I/O but I think you should create a char array as buffer and read into that. You can copy the buffer into a string to work with it if you want.
Something like that:

Code: Select all

ifstream filei(filen.c_str(), ios::in);
char buffer = new char[100];
filei.read(buffer, 100);
loader = buffer;
filei.gcount();
This will read up to 100 bytes from the stream into the char buffer and copy it into the loader string. Btw, are you sure you wanted to do filen.size() in line 18 as filen is holding your filename, not the file stream.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Damn I/O!! ( HELP PLEASE! )

Post by Ginto8 »

fantastico wrote:
MadPumpkin wrote: well i put the '&' before "loader" now and i get this
(line 18)
error: no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::read(std::string*, size_t)'|
That's because you are passing the function a pointer to a std::string while it's expecting a pointer to a char, where a buffer begins (char* / array of chars). I'm not too familiar with C++ I/O but I think you should create a char array as buffer and read into that. You can copy the buffer into a string to work with it if you want.
Something like that:

Code: Select all

ifstream filei(filen.c_str(), ios::in);
char buffer = new char[100];
filei.read(buffer, 100);
loader = buffer;
filei.gcount();
This will read up to 100 bytes from the stream into the char buffer and copy it into the loader string. Btw, are you sure you wanted to do filen.size() in line 18 as filen is holding your filename, not the file stream.
or you could do filei.read((char*)&loader, ... ) ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
fantastico
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Fri Mar 20, 2009 4:37 am
Location: Germany

Re: Damn I/O!! ( HELP PLEASE! )

Post by fantastico »

Ginto8 wrote:or you could do filei.read((char*)&loader, ... ) ;)
The compiler would accept it, the OS and the programmer wouldn't like the result though :)
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Damn I/O!! ( HELP PLEASE! )

Post by MadPumpkin »

fantastico wrote:
MadPumpkin wrote: well i put the '&' before "loader" now and i get this
(line 18)
error: no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::read(std::string*, size_t)'|
That's because you are passing the function a pointer to a std::string while it's expecting a pointer to a char, where a buffer begins (char* / array of chars). I'm not too familiar with C++ I/O but I think you should create a char array as buffer and read into that. You can copy the buffer into a string to work with it if you want.
Something like that:

Code: Select all

ifstream filei(filen.c_str(), ios::in);
char buffer = new char[100];
filei.read(buffer, 100);
loader = buffer;
filei.gcount();
This will read up to 100 bytes from the stream into the char buffer and copy it into the loader string. Btw, are you sure you wanted to do filen.size() in line 18 as filen is holding your filename, not the file stream.
thank you for all of your help! everybody!! now, fantastico, about the filen.size() is there a way to do that so that i actually get the stream size instead of name of file?
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Damn I/O!! ( HELP PLEASE! )

Post by Ginto8 »

fantastico wrote:
Ginto8 wrote:or you could do filei.read((char*)&loader, ... ) ;)
The compiler would accept it, the OS and the programmer wouldn't like the result though :)
True... CRAP I'm stupid, didn't look at the write() parameters. :lol: :oops:
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Post Reply