Page 1 of 2

Damn I/O!! ( HELP PLEASE! )

Posted: Fri Apr 03, 2009 8:16 am
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;
}

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

Posted: Fri Apr 03, 2009 10:07 am
by PixelP
put

Code: Select all

cin.get();
after

Code: Select all

cin >> filen;
it should do the trick.

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

Posted: Fri Apr 03, 2009 11:23 am
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.

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

Posted: Fri Apr 03, 2009 3:07 pm
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!

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

Posted: Fri Apr 03, 2009 3:47 pm
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;
}

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

Posted: Fri Apr 03, 2009 4:24 pm
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.

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

Posted: Fri Apr 03, 2009 4:44 pm
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()). ;)

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

Posted: Fri Apr 03, 2009 9:34 pm
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

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

Posted: Fri Apr 03, 2009 10:07 pm
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, .......

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

Posted: Sat Apr 04, 2009 2:21 am
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)'|

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

Posted: Sat Apr 04, 2009 4:59 am
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.

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

Posted: Sat Apr 04, 2009 8:57 am
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, ... ) ;)

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

Posted: Sat Apr 04, 2009 10:17 am
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 :)

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

Posted: Sat Apr 04, 2009 1:46 pm
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?

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

Posted: Sat Apr 04, 2009 1:47 pm
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: