-----update-----
I managed to fix the issue by not exiting mid-function but returning to main and exiting there.
Can you explain why it worked?
tnx
I keep getting still reachable valgrind error on my code.
Already tried:
close file before exit, and before function readCSV ends
declare iss once, and using iss.str, iss.clear
(all destructors are default)
I think this function is causing me problems. I's called from the main function, and reads the csv file into a deque:
static std::deque<Fractal *> readCSV(const std::string &path)
{
std::deque<Fractal *> fractals;
std::ifstream ifs(path);
if (ifs.fail())
{
std::cerr << INV_INPUT << std::endl;
exit(1);
}
std::string line;
while (getline(ifs, line))
{
std::istringstream iss(line);
std::string arg;
int type, height;
getline(iss, arg, ',');
type = strToN(arg);//parse int from string, -1 if not a valid integer
getline(iss, arg, ',');
height = strToN(arg);
if (height <= 0 || height > MAX_HEIGHT)
{
std::cerr << INV_INPUT << std::endl;
deleteFractalArr(fractals);
exit(1);
}
if (getline(iss, arg, ','))
{
std::cerr << INV_INPUT << std::endl;
deleteFractalArr(fractals);
exit(1);
}
switch (type)
{
case 1:
fractals.push_front(new SCarpet(height));
break;
case 2:
fractals.push_front(new SSieve(height));
break;
case 3:
fractals.push_front(new CDust(height));
break;
default:
std::cerr << INV_INPUT << std::endl;
deleteFractalArr(fractals);
exit(1);
}
}
return fractals;
}
the deleteFractalArr is as follows:
static void deleteFractalArr(const std::deque<Fractal *> &fractals)
{
for (Fractal *f:fractals)
{
delete f;
f = nullptr;
}
}
Related
I'm writing a program, which takes the lines of text to work with from the file, the name of which the user passes as an argument, e.g. program <name of the file>. But if the name is not provided, the input is taken dynamically from std::cin. What I've tried:
Redirecting the buffer (somewhy causes segfault)
if (argc == 2) {
std::ifstream ifs(argv[1]);
if (!ifs)
std::cerr << "couldn't open " << argv[1] << " for reading" << '\n';
std::cin.rdbuf(ifs.rdbuf());
}
for (;;) {
std::string line;
if (!std::getline(std::cin, line)) // Here the segfault happens
break;
Creating a variable, in which the input source is stored
std::ifstream ifs;
if (argc == 2) {
ifs.open(argv[1]);
if (!ifs)
std::cerr << "couldn't open " << argv[1] << " for reading" << '\n';
} else
ifs = std::cin; // Doesn't work because of the different types
for (;;) {
std::string line;
if (!std::getline(ifs, line))
break;
Now I'm thinking of doing something with file structures/descriptors. What to do?
UPD: I would like to have the possibility to update the input source in the main loop of the program (see below).
The seg fault in your first example is due to a dangling pointer; right after you call std::cin.rdbuf(ifs.rdbuf()), ifs is destroyed.
You should do what #NathanOliver suggests and write a function which takes an istream&:
#include <iostream>
#include <fstream>
#include <string>
void foo(std::istream& stream) {
std::string line;
while (std::getline(stream, line)) {
// do work
}
}
int main(int argc, char* argv[]) {
if (argc == 2) {
std::ifstream file(argv[1]);
foo(file);
} else {
foo(std::cin);
}
}
I'm new to C++, and I'm trying to write a project that interacts through command line. Right now, whenever I run my main (which is the executable), I always receive a segmentation fault error when the main program finished.
Edit comment:
I'm told by tutor to use as little as C++ features such as vectors or strings ... I'm also very new to C++, so i'm trying to utilize as many basic C functions as I can.
I'm
My main function looks like this:
int main(int argc, char** argv) {
cout << "starting mvote..." << endl;
int run_flag = 1;
char* actionBuffer = (char*)malloc(100 * sizeof(char));
char* action = (char*)malloc(16 * sizeof(char));
char* readPtr;
char exit[4] = { 'e','x','i','t' };
//parse command line argumentand get the filename
char* filename = argv[2];
cout << filename;
FILE* fp;
char line[64];
//from here, I'm opening the file and read it by lines
fp = fopen(filename, "r");
if (fp == NULL) {
cout << "file not exists";
return -1;
}
while (fgets(line, 64, fp) != NULL) {
cout << line << "\n";
}
fclose(fp);
while (run_flag == 1) {
cout << "what do you want?\n " << endl;
cin.getline(actionBuffer, 1024);
if (strcmp(actionBuffer, exit) == 0) {
cout << "bye!";
run_flag = 0;
break;
}
//if not exit, Look for the space in the input
readPtr = strchr(actionBuffer, ' ');
int size = readPtr - actionBuffer;
//extract the operation
strncpy(action, actionBuffer, size);
for (int i = 0; i < size; i++) {
cout << "operation:" << action[i];
}
// depend on the operation specified before the first empty space
run_flag = 0;
}
free(actionBuffer);
free(action);
return 0;
}
Description:
I first try to open up a csv file which lies in the same folder as main, and I read the file line by line. Then, I just implement a simple command where you can type exit and quit the program.
I allocate two memory, actionBuffer and action, which are used to hold command
Problem: a segmentation fault [core dumped] always exists when I type exit and hit enter, and then the process finished.
Research: So I learned that segmentation fault is due to accessing a memory that does not belongs to me. But where in my program am I trying to access such a memory?
Any advice is appreciated! Thank you.
Just to give you an idea, this would be an example of C++ code
#include<iostream>
#include<fstream>
#include<string_view>
#include<string>
#include<sstream>
#include<exception>
int main(int argc, char** argv) {
std::cout << "starting mvote...\n";
//parse command line argumentand get the filename
std::string filename = argv[2]; // NO CHECKS!
std::cout << filename <<'\n';
//from here, I'm opening the file and read it by lines
{
std::ifstream ifs(filename);
if (!ifs) {
throw std::invalid_argument("file not exists");
}
std::string line;
while (std::getline(ifs, line)) {
std::cout << line << '\n';
}
}
bool run_flag = true;
while (run_flag) {
std::cout << "what do you want?\n";
std::string userInput;
std::getline(std::cin, userInput);
if (userInput == "exit") {
std::cout << "bye!\n";
return 0;
}
std::stringstream userInputSs(userInput);
std::string operation;
while(userInputSs >> operation){
std::cout << "operation: " << operation << '\n';
}
}
}
I want to use protobuf and to create the serialization output file in text format for testing and for a replacement of json. I can't figure out how to write it on my own and am looking for examples.
Here is the one on binary output :
#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"
using namespace std;
// This function fills in a Person message based on user input.
void PromptForAddress(tutorial::Person* person) {
cout << "Enter person ID number: ";
int id;
cin >> id;
person->set_id(id);
cin.ignore(256, '\n');
cout << "Enter name: ";
getline(cin, *person->mutable_name());
cout << "Enter email address (blank for none): ";
string email;
getline(cin, email);
if (!email.empty()) {
person->set_email(email);
}
while (true) {
cout << "Enter a phone number (or leave blank to finish): ";
string number;
getline(cin, number);
if (number.empty()) {
break;
}
tutorial::Person::PhoneNumber* phone_number = person->add_phones();
phone_number->set_number(number);
cout << "Is this a mobile, home, or work phone? ";
string type;
getline(cin, type);
if (type == "mobile") {
phone_number->set_type(tutorial::Person::MOBILE);
} else if (type == "home") {
phone_number->set_type(tutorial::Person::HOME);
} else if (type == "work") {
phone_number->set_type(tutorial::Person::WORK);
} else {
cout << "Unknown phone type. Using default." << endl;
}
}
}
// Main function: Reads the entire address book from a file,
// adds one person based on user input, then writes it back out to the same
// file.
int main(int argc, char* argv[]) {
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2) {
cerr << "Usage: " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
return -1;
}
tutorial::AddressBook address_book;
{
// Read the existing address book.
fstream input(argv[1], ios::in | ios::binary);
if (!input) {
cout << argv[1] << ": File not found. Creating a new file." << endl;
} else if (!address_book.ParseFromIstream(&input)) {
cerr << "Failed to parse address book." << endl;
return -1;
}
}
// Add an address.
PromptForAddress(address_book.add_people());
{
// Write the new address book back to disk.
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
if (!address_book.SerializeToOstream(&output)) {
cerr << "Failed to write address book." << endl;
return -1;
}
}
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
Can I just do some minor changes in this one to output in text format or something else needs to be done? Please either suggest the changes required or any link where code exists (in any language).
The debug string output is guaranteed to be valid text-serialized format, but does not care about whether the protocol message is actually valid:
std::string s = msg.DebugString(); // or ShortDebugString
If you want to validate, use TextFormat::PrintToString:
#include <google/protobuf/text_format.h>
if (std::string s; google::protobuf::TextFormat::PrintToString(msg, &s)) {
std::cout << "Your message: " << s;
} else {
std::cerr << "Message not valid (partial content: "
<< msg.ShortDebugString() << ")\n";
}
Tools for JSON interop are available in json_util.h.
This code will serialise protobuf messages to JSON and deserialise JSON to protobuf messages.
This is lifted straight out of production code (which I own and hereby grant you licence to use, but please credit me).
This is linked against protobuf 3.
Header:
struct pretty_json_type {
void operator()(google::protobuf::util::JsonOptions& opts) const {
opts.add_whitespace = true;
}
};
static constexpr pretty_json_type pretty_json{};
struct compact_json_type {
void operator()(google::protobuf::util::JsonOptions& opts) const {
opts.add_whitespace = false;
}
};
static constexpr compact_json_type compact_json{};
struct include_defaults_type {
void operator()(google::protobuf::util::JsonOptions& opts) const {
opts.always_print_primitive_fields = true;
}
};
static constexpr include_defaults_type include_defaults{};
template<class...Options>
auto json_options(Options&&...options)
{
google::protobuf::util::JsonOptions opts;
using expand = int [];
void(expand{
0,
((options(opts)),0)...
});
return opts;
}
std::string as_json(const google::protobuf::Message& msg,
google::protobuf::util::JsonOptions opts = json_options(pretty_json,
include_defaults));
std::string as_json(const google::protobuf::Message* msg,
google::protobuf::util::JsonOptions opts = json_options(pretty_json,
include_defaults));
google::protobuf::Message& from_json(google::protobuf::Message& msg,
const char* first,
std::size_t size);
inline
decltype(auto) from_json(google::protobuf::Message& msg,
const std::string& json)
{
return from_json(msg, json.data(), json.length());
}
Implementation
std::string as_json(const google::protobuf::Message& msg,
google::protobuf::util::JsonOptions opts)
{
namespace pb = google::protobuf;
namespace pbu = google::protobuf::util;
auto buffer = msg.SerializeAsString();
std::string result;
pb::io::ArrayInputStream zistream(buffer.data(), buffer.size());
auto resolver = std::unique_ptr<pbu::TypeResolver> {
pbu::NewTypeResolverForDescriptorPool("",
pb::DescriptorPool::generated_pool())
};
auto status = google::protobuf::util::BinaryToJsonString(resolver.get(),
"/" + msg.GetDescriptor()->full_name(),
buffer,
std::addressof(result),
opts);
if (!status.ok())
{
std::ostringstream ss;
ss << status;
throw std::runtime_error(ss.str());
}
return result;
}
std::string as_json(const google::protobuf::Message* msg,
google::protobuf::util::JsonOptions opts)
{
return as_json(*msg, opts);
}
google::protobuf::Message& from_json(google::protobuf::Message& msg,
const char* first,
std::size_t size)
{
namespace pb = google::protobuf;
namespace pbu = google::protobuf::util;
auto resolver = std::unique_ptr<pbu::TypeResolver> {
pbu::NewTypeResolverForDescriptorPool("", pb::DescriptorPool::generated_pool())
};
auto zistream = std::make_unique<pb::io::ArrayInputStream>(first,
size);
auto binary_buffer = std::string {};
binary_buffer.reserve(size);
auto zostream = std::make_unique<pb::io::StringOutputStream>(std::addressof(binary_buffer));
auto status = pbu::JsonToBinaryStream(resolver.get(),
"/" + msg.GetDescriptor()->full_name(),
zistream.get(), zostream.get());
zistream.reset();
zostream.reset();
if (msg.ParseFromString(binary_buffer))
{
return msg;
}
throw std::runtime_error("invalid message");
}
To convert a message to JSON in three lines of code, do this -
#include <google/protobuf/util/json_util.h>
static std::string ProtoToJson(const google::protobuf::Message& proto)
{
std::string json;
google::protobuf::util::MessageToJsonString(proto, &json);
return json;
}
This is my code
/*
Asks the user for their ID, depending on the ID depends on the results. It either goes to maintanance
or it asks the user to return DVD's or check DVD's out and changes the stock of the DVD's.
Cody Close
*/
#include <iostream>
#include <fstream>
#include <conio.h>
#include <sstream>
#include <string>
using namespace std;
void custID();
void sales();
void returns();
void discounts();
void maint();
void createAcc(string* filename, string* newID);
bool checkID(string* filename, string* search);
int main()
{
//Declares all the variables for the program
int mainID= 99959, menuChoice;
bool close = false;
bool done = false;
string vidId;
//Declares and input file and opens a file
fstream inFile;
inFile.open("dayin00.dat");
do{
do{
cout << "accountID: " << endl;
cin >> mainID;
stringstream out;
out << mainID;
mainid = out.str();
checkID("IDlist.txt", mainid);
}while(mainid.length() < 5 || mainid.length() > 9);
if(mainID!= 99959)
{
do
{
cout << "MENU:" << endl;
cout << "(1)Purchase\n(2)Return\n(3)Exit" << endl;
cin >> menuChoice;
switch(menuChoice)
{
case 1:
case 2:
case 3:
done = true;
}
}while(done == false);
}else{
maint();
}
close = true;
}while(close == false);
return 0;
}
void maint()
{
int maintChoice;
cout << "\n(1)Summary\n(2)Withdrawl\n(3)Close Down\n(4)Back to >main\n(0)Help" << endl;
cin >> maintChoice;
switch (maintChoice)
{
case 1:
case 2:
case 3:
case 4:
default:
cout << "1 for summary, 2 for withdrawl, 3 to close down, 4 to >go back to main" << endl;
}
}
void createAcc(string* filename, string* newID)
{
fstream newFile;
newFile.open(filename);
newFile << newID;
}
void checkID(string* filename, string* ID)
{
fstream infile;
infile.open("IDlist.txt");
string word;
infile >> word;
while (!infile.eof()){
if(word == ID)
{
cout << "ID FOUND!" << endl;
}else{
createAcc(infile, ID);
}
}
}
The text file only contains the ID 99959. How do I check if the ID the user types in already exists in the text file and if it doesn't, then it goes to createAcc(),setting up a new account using the ID that the user has entered.
The code opens file with users ID in read mode, reads it line by line and tries to finde ID. If ID not found in file, it opens file in write mode and add user ID in file.
#include <iostream>
#include <fstream>
#include <stdexcept>
void createAcc(const std::string& filename, const std::string& id)
{
std::ofstream os(filename);
if (os)
os << id;
else
throw std::runtime_error("Open file error: " + filename);
}
bool isStringContainsID(const std::string& line, const std::string& id)
{
if (line.find(id) == std::string::npos)
return false;
else
return true;
}
bool isFileContainsID(const std::string& filename, const std::string& id)
{
std::ifstream is(filename);
if (!is)
throw std::runtime_error("Open file error: " + filename);
std::string line;
while (is)
{
std::getline(is, line);
if (isStringContainsID(line, id))
return true;
}
return false;
}
int main() {
std::string id("99959");
std::string file_name("IDlist.txt");
if (isFileContainsID(file_name, id))
std::cout << "ID FOUND!" << std::endl;
else
createAcc(file_name, id);
return 0;
}
Note that all users ID should have the same length in string representation, otherwise the code can find shorter ID in file that contains larger ID with shorter ID as sub-string.
I'm getting this error on my program and I don't understand why. The code essentially has to check for tags stored in a set which is declared as a global variable. If it's a valid tag it stores it in the stack if not returns a error message. Then it checks (if its a valid tag)if the closing tags are in order. This is all for the is_well_formed method. For the print_well_formed_file method it essentially checks if the given file is well formed if it is it'll display the file.:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr
What can I do to fix this error ?
This is part of the code:
bool is_well_formed(ifstream& ifs, string& error_msg) {
// your code goes here
string fname, line;
Token tok;
Lexer lexer;
tags.insert("blue");
tags.insert("red");
tags.insert("cyan");
tags.insert("white");
tags.insert("yellow");
tags.insert("magenta");
tags.insert("dim");
tags.insert("underline");
tags.insert("bold");
while (getline(cin, fname)) {
// tries to open the file whose name is in string fname
string name = fname.substr(1, fname.length() - 2);
cout << "Name" + name;
ifs.open(name.c_str());
if (ifs.fail()) {
cerr << "ERROR: Failed to open file " << fname << endl;
ifs.clear();
} else {
while (getline(ifs, line)) {
lexer.set_input(line);
while (lexer.has_more_token()) {
tok = lexer.next_token();
string tmpTok = tok.value;
switch (tok.type) {
case TAG:
// If it has /, remove / from tmpTok
if (tok.value[0] == '/') {
tmpTok = tmpTok.substr(1, tmpTok.length() - 1);
}
if (tags.find(tmpTok) == tags.end()) {
// Check whether the encountered tag is valid
error_return("Tag " + tmpTok + " is invalid!");
} else {
// Valid Tag encountered
stack < string > tagstack;
tagstack.push(tmpTok);
// Check if the tags are formed properly
if (tok.value[0] == '/') {
// Remove / from tmpTok
string closingTag = tmpTok;
string openingTag = tagstack.top();
tagstack.pop();
if (closingTag.compare(openingTag) != 0) {
error_return(
closingTag + "doesn't match"
+ openingTag);
} //else
// return true; // if the file is well formed
}
}
break;
case IDENT:
cout << "IDENT: " << tok.value << endl;
break;
case ERRTOK:
error_return("Syntax error on this line\n");
//cout << "Syntax error on this line\n";
break;
case ENDTOK:
break;
}
}
}
}
}
return true; // if the file is well-formed
}
void print_well_formed_file(ifstream& ifs) {
//Check if file is well formed.
string line;
Lexer command;
if (is_well_formed(ifs, line)) { //if well formed display
command.set_input(line);
display(command);
}
}
void display(Lexer cmd_lexer) {
string file_name;
if (!parse_input(cmd_lexer, file_name)) {
error_return("Syntax error: display <filename>");
return;
}
ifstream ifs(file_name.c_str());
string error_msg;
if (ifs) {
if (!is_well_formed(ifs, error_msg)) {
error_return(error_msg);
} else {
ifs.clear(); // clear EOF flag
ifs.seekg(0, ios::beg); // go back to the very beginning
print_well_formed_file(ifs);
}
} else {
error_return("Can't open " + file_name + " for reading");
}
ifs.close();
}
Example of user input:
validate <file name>
display <file name>
exit
string name = fname.substr(1, fname.length() - 2);
Will throw this kind of an exception if fname's length is <= 1 . I bet, this is the case. The simplest(not the best) solution is to skip such lines.