Question:
Please help with c++ code?
?
2012-08-09 11:46:49 UTC
I'm very new to c++ and am trying this script, it is not compiling, help editing?

#include
#include

using namespace std;

ofstream file("data.bin", ios::app | ios::binary);

ifstream::pos_type size;
char * memblock;
int main () {
ifstream file ("data.bin", ios::in|ios::binary|ios::ate);
if (file.is_open())
{

int c;
cout<<"read or write data? \n";

cin >> c;

if(c == "read"){
goto read;
}
else if(c == "write"){
goto write;
};
return 0;
else {
cin.get();
}

write:
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
char buffer[100];
ofstream file("data.bin", ios::app | ios::out | ios::binary);
file.write (buffer, 100);
file.close();
cout << "the complete file content is in memory";
delete[] memblock;
cin.get();
}


read:
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout << "the complete file content is in memory";
delete[] memblock;
cin.get();
}
else cout << "Unable to open file";
cin.get();
return 0;
}

Thanks in advance!!
Three answers:
Bob
2012-08-09 19:27:12 UTC
include

#include

using namespace std;



ifstream::pos_type size;

char *memblock;



int main ()

{

bool valid = true;



ifstream ifile("dataIn.bin", ios::in | ios::binary | ios::ate);



if (ifile.is_open())

{

string response;



do

{

cout << "read or write data? ";



cin >> response;



if(response.compare("read") == 0)

{

valid = true;

size = ifile.tellg();

memblock = new char[size];

ifile.seekg (0, ios::beg);

ifile.read (memblock, size);

ifile.close();

cout << "read from file:" << endl;



int i = 0;



while(i < size)

{

cout << *memblock;

memblock++;

i++;

}



delete[] memblock;

}

else if(response.compare("write") == 0)

{

valid = true;

ofstream ofile("dataOut.bin", ios::app | ios::binary);

size = ifile.tellg();

memblock = new char [size];

ifile.seekg (0, ios::beg);

ifile.read (memblock, size);

ofile.write (memblock, size);

ifile.close();

ofile.close();

cout << "wrote to file";

delete[] memblock;

}

else

{

cout << "invalid entry, enter 'read' or 'write'" << endl;

valid = false;

}

} while(!valid);

}

else

cout << "Unable to open file";



cout << endl << endl;



system("pause");



return 0;

}



Try this program out. I'm not sure if it does what you are trying to do but it will compile and runs without any errors on my computer. I ran this program with the Dev C++ compiler.
2017-01-15 21:00:35 UTC
this is actual that in case you create an empty C++ console project and manually rename your cpp report to grant it .c extension, seen Studio *will* execute the Microsoft C compiler, although this is a doubly-out of date 1989 C compiler. Microsoft abandoned C a protracted time in the past. in case you desire to stay with this IDE and deliver mutually C, substitute the underlying compiler. Intel C/C++ is a competent decision.
TheMadProfessor
2012-08-09 12:03:20 UTC
So, what compile errors do you get?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...