Hi everyone I create a data name data2.bin and trying to run it trough this program,
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Mahasiswa
{
int NIM;
string nama;
string jurusan;
};
int main()
{
fstream myFile;
Mahasiswa dataBaca;
myFile.open("data2.bin", ios::in | ios::binary);
myFile.read(reinterpret_cast<char *>(&dataBaca), sizeof(Mahasiswa));
cout << dataBaca.NIM << endl;
cout << dataBaca.nama << endl;
cout << dataBaca.jurusan << endl;
myFile.close();
return 0;
}
when I run it, the program would stop at myFile.close() line and the output would be question mark symbols like (link added below). Anyone knows where's the problem?
output image from vscode terminal
here's the code to create the data2.bin
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Mahasiswa{
int NIM;
string nama;
string jurusan;
};
int main(){
fstream myFile;
myFile.open ("data2.bin", ios::trunc | ios::out | ios::in | ios::binary);
Mahasiswa mahasiswa1, mahasiswa2, mahasiswa3;
mahasiswa1.NIM = 123;
mahasiswa1.nama = "ucup";
mahasiswa1.jurusan = "memasak";
mahasiswa2.NIM = 124;
mahasiswa2.nama = "otong";
mahasiswa2.jurusan = "menjahit";
mahasiswa3.NIM = 125;
mahasiswa3.nama = "sandra";
mahasiswa3.jurusan = "mesin";
myFile.write(reinterpret_cast<char*>(&mahasiswa1),sizeof(Mahasiswa));
myFile.write(reinterpret_cast<char*>(&mahasiswa2),sizeof(Mahasiswa));
myFile.write(reinterpret_cast<char*>(&mahasiswa3),sizeof(Mahasiswa));
myFile.close();
return 0;
}
Related
I am trying to print the data located in the weapon.obj file, but it's not working.
Compiler Error: error: no matching function for call to
'getline(std::ifstream&)'|
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream render_weapon_OBJ ("weapon.obj");
render_weapon_OBJ << ("Weapon Names");
render_weapon_OBJ.close();
ifstream execute_weapon_OBJ ("weapon.obj");
while (getline(execute_weapon_OBJ))
{
cout << execute_weapon_OBJ << '\n';
}
execute_weapon_OBJ.close();
}
You must specify where to read the data and use that for printing.
#include <iostream>
#include <fstream>
#include <string> // add this to use std::string
using namespace std;
int main()
{
ofstream render_weapon_OBJ ("weapon.obj");
render_weapon_OBJ << ("Weapon Names");
render_weapon_OBJ.close();
ifstream execute_weapon_OBJ ("weapon.obj");
string weapon; // add this for read buffer
while (getline(execute_weapon_OBJ, weapon)) // add where to read
{
cout << weapon << '\n'; // print what was read instead of the stream
}
execute_weapon_OBJ.close();
}
May be this is the solution you are looking for
You need a variable to store the line read from the file and you need to print the variable not the variable used to initialize the file stream.
The error is in the while loop[The new code is below]
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string line;
ofstream render_weapon_OBJ ("weapon.obj");
render_weapon_OBJ << ("Weapon Names");
render_weapon_OBJ.close();
ifstream execute_weapon_OBJ ("weapon.obj");
while(getline(execute_weapon_OBJ,line))
{
cout << line << '\n';
}
execute_weapon_OBJ.close();
}
I've written the following in windows on Qt IDE and when I run it, it works well, however when I tried to run it on centOS with, I want to run code using threads, in which I'm just tring to load a CSV file and write the results within it in centos envirements
g++ -std=gnu++11 main.cpp -o main
I get the errors
does any solution to this issue ?
code
#include <iostream> // for standard I/O
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <ctime>
#include <future>
#include <fstream>
#include <string>
/*
*
* error This file requires compiler and library support for the ISO C++ 2011 standard.
* This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
*
*/
using namespace std;
stringstream processing (int x,int id) {
std::cout << "Calculating. Please, wait...\n";
stringstream cvsStream;
for(int i = 0 ; i < x ; ++i){
cvsStream <<i<<","<<i<<","<<i<<","<<i<<"\n";
cout <<id<< " / "<<i<< endl;
}
return cvsStream;
}
int main(int argc, char *argv[])
{
string filename = "OutputFile.csv";
ofstream myfile;
stringstream cvsStream;
myfile.open(filename);
// If file does not exist, Create new file
if (!myfile )
{
cout << "Cannot open file, file does not exist. Creating new file..";
myfile.open(filename, fstream::in | fstream::out | fstream::trunc);
myfile <<"\n";
}
// open csv file
cvsStream <<" AD_ID "<<","<<"Starts at "<<","<<"At_Frame"<<","<<"Ends at "<<"\n";
myfile << cvsStream.str();
cvsStream.str("");
auto outputRslt1 = std::async (processing,1000,1);
auto outputRslt2 = std::async (processing,1000,2);
auto outputRslt3 = std::async (processing,1000,3);
stringstream rsltThread1 = outputRslt1.get();
stringstream rsltThread2 = outputRslt2.get();
stringstream rsltThread3 = outputRslt3.get();
// close csv file
myfile << rsltThread1.str();
myfile << rsltThread2.str();
myfile << rsltThread3.str();
myfile.close();
return 0;
}
errors
main.cpp: In function ‘std::stringstream processing(int, int)’:
main.cpp:22:8: error: use of deleted function ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’
return cvsStream;
In file included from main.cpp:4:0:
/usr/include/c++/4.8.2/sstream:502:11: note: ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
class basic_stringstream : public basic_iostream<_CharT, _Traits>
In fact, Streams are not copyable, Unfortunately GCC 4.8 had not yet added the move constructor that was necessary for this to work. C++11 made them moveable and this is what makes it possible to return a local stringstream object from a function.
you can use function return the result as a string and consider as a solution or upgrade to a later version of GCC.
#include <iostream> // for standard I/O
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <ctime>
#include <future>
#include <fstream>
#include <string>
/*
*
* error This file requires compiler and library support for the ISO C++ 2011 standard.
* This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
*
*/
using namespace std;
string processing (int x,int id) {
std::cout << "Calculating. Please, wait...\n";
stringstream cvsStream;
for(int i = 0 ; i < x ; ++i){
cvsStream <<i<<","<<i<<","<<i<<","<<i<<"\n";
cout <<id<< " / "<<i<< endl;
}
return cvsStream.str();
}
int main(int argc, char *argv[])
{
string filename = "OutputFile.csv";
ofstream myfile;
stringstream cvsStream;
myfile.open(filename);
// If file does not exist, Create new file
if (!myfile )
{
cout << "Cannot open file, file does not exist. Creating new file..";
myfile.open(filename, fstream::in | fstream::out | fstream::trunc);
myfile <<"\n";
}
// open csv file
cvsStream <<" AD_ID "<<","<<"Starts at "<<","<<"At_Frame"<<","<<"Ends at "<<"\n";
myfile << cvsStream.str();
cvsStream.str("");
auto outputRsl1 = std::async (processing,1000,1);
auto outputRsl2 = std::async (processing,1000,2);
auto outputRsl3 = std::async (processing,1000,3);
string rslThread1 = outputRsl1.get();
string rslThread2 = outputRsl2.get();
string rslThread3 = outputRsl3.get();
// close csv file
myfile << rslThread1;
myfile << rslThread2;
myfile << rslThread3;
myfile.close();
return 0;
}
i m trying to read from a file and stop when i hit end of line. the thing is, it doesnt seem to work.¯_(ツ)_/¯ any ideas why?
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char a;
ifstream myfile;
myfile.open("text.txt");
while (!myfile.eof())
{
myfile>> a;
if (a=='\n')
cout << "end of line";
}
myfile.close();
}
text file i read:
Try while (myfile.get(a)) instead?
while (myfile.get(a))
{
if (a=='\n')
cout << "end of line";
}
Why make things harder than needed. If you want to parse lines, then use std::getline().
#include <iostream>
#include <fstream>
int main(int argc, char *argv[]) {
std::ifstream myfile("text.txt");
std::string line;
while (std::getline(myfile, line)) {
std::cout << "end of line" << std::endl;
}
}
using a for loop
std::ifstream ifs( "file" );
for( char chr = 0; ifs.peek() != '\n'; ifs.get( chr ) ){
std::cout << chr;
}
ifs.close();
I just rewrite your code:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char a;
ifstream myfile;
myfile.open("/Users/sijan/CLionProjects/test2/text.txt",ifstream::in);
while (myfile.get(a))
{
cout<< a;
if (a=='\n')
cout << "end of line\n";
}
if (myfile.eof())
cout << "end of file";
myfile.close();
}
I just need to write a string into a file created using ofstream, but I am getting an error.
This is the code:
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
QString aux = "Hello";
ofstream myfile ("test.txt");
if (myfile.is_open())
{
myfile << aux;
myfile.close();
}
else
{
cout << "CANT OPEN FILE";
}
return 0;
}
The error is: no match for 'operator<<' in 'myfile << aux'
P.S: I am using QT4
Thanks for your help!
You should convert to a string by doing :
myfile << aux.toStdString();
This is because the << operator does not know any conversion from qt string.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile("text.txt", ios_base::in); // this works
ofstream outfile("C:/output.txt", ios_base::out); // doesn't
outfile << "hi";
return 0;
}
No file is created. What is the problem?