I am trying to read a file which is placed in the desktop through C++ program in linux server.I have mentioned the path correctly,but it's not reading the file. I have tried the same program in windows platform it's working fine.I'm able to read the file.
#include <stdio.h>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string line;
ifstream file;
file.open("/home/xxx/Desktop/nodeinfo.txt",ios::in);
if(!file.is_open())
{
cout<<"error";
}
getline(file,line);
cout<<line;
return 0;
}
could someone help me how to solve this problem. Is there any specific path format for linux platform. Thanks
Related
I am trying to parse a json file within my program:
#include <jsoncpp/json/value.h>
#include <jsoncpp/json/json.h>
#include <unistd.h>
#include <stdio.h>
int main(){
std::string plan { get_current_dir_name() };
plan += "directory/file.json";
read_json(plan); // A function that reads a json file using jsoncpp
}
Output:
Error: Json File not found!
However when I manually write the entire path:
#include <jsoncpp/json/value.h>
#include <jsoncpp/json/json.h>
#include <unistd.h>
#include <stdio.h>
int main(){
std::string plan { entire_file_path };
read_json(plan); // A function that reads a json file using jsoncpp
}
Output:
File found and read!
I thought maybe there is a spelling mistake but when I use std::cout on both of the paths, there is not a single difference. I'm not sure what is causing this issue.
Using std::filesystem built-in to C++17:
namespace fs = std::filesystem;
fs::path path = fs::current_path() / "directory" / "file.json";
read_json(path.string());
So I have some decoded text which I have to write as binary in a new file.
I came up with this code but it writes it as a decoded text instead of binary
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cctype>
#include <locale>
#include <vector>
#include <iterator>
#include <iomanip>
using namespace std;
int main() {
ofstream outputFile;
outputfile.open("binary.dat");
ifstream file("binary.dat", ios::binary);
outputfile <<"68656C6C6F20776F726C64";
return 0;
}
Thank you for any help!
You have to open the file in binary mode to write binary data
outputfile.open("binary.dat", ofstream::binary)
Note: This is assuming you are intending to simply write the binary representation of your text string, rather than treat your string as hexadecimal values as was mentioned in the comments
I need to read the information contained in a json file like this:
{"first":10, "second":"0", "P1":"1.e-20","P2":"1000","P3":"1000","P4":"1000","P5":"1"}
Since I do not have experience with this issue, I started by playing with the short code you can see below these lines. It does compile with no problem but it gives a segmentation fault back upon execution. The file general.json is in the same folder. The information contained in the json file is correctly printed in the screen if I comment the last line. Could anyone tell me what am I doing wrong?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fstream> // fstream.h in old versions of g++
#include <iostream> //para cout
#include <sstream>
#include <json/json.h>
using namespace std;
int main() {
struct json_object *new_json, *json_arr, *json_reg, *json_field;
string line;
stringstream jsonfile;
ifstream json("file.json", ios::in);
{getline(json, line); do {jsonfile << line;} while (getline(json, line));}
json.close();
cout << jsonfile.str().c_str();
new_json=json_tokener_parse(jsonfile.str().c_str());
json_field=json_object_object_get(json_reg, "first");
}
You are using the json_reg pointer without initializing it and the function dereferences it. You are (most likely) using json-c where:
json_object_object_get calls json_object_object_get_ex on the object
json_object_object_get_ex does switch(jso->o_type) dereferencing an invalid pointer
#include <iostream>
#include <assert.h>
#include <fstream>
#include <map>
#include <ostream>
#include "ogg.h"
using namespace std;
#pragma comment(lib,"libogg.lib")enter code hereenter
void readogg();
void readogg(){
ifstream stream;
ifstream file("invitation.ogg", ios::in | ios::binary);
ogg_sync_state state;
ogg_sync_init(&state);
ogg_page page;
if (ogg_sync_pageout(&state, &page) != 1)
{
char* buffer = ogg_sync_buffer(&state, 8092);
assert(buffer);
file.read(buffer, 8092);
int bytes = stream.gcount();
ogg_sync_wrote(&state, bytes);
}
ogg_stream_state s_state;
ogg_packet pack;
☆ ogg_stream_pagein(&s_state, &page);
ogg_page_packets(&page);
ogg_stream_packetout(&s_state, &pack);
}
This is my code what I was invisible mending. I have another code about main but it have not a problem. I debugged step by step, so I think ☆ code might have a error and the result might do not connects or saves information of 'invitation.ogg' file.
I couldn't find answer anywhere and this is my final chance keeping on this code. I'm not Thanks to read my question and I really hope to find my answer.
I am trying to read a .gz file and print the text content on screen by using boost::iostreams. This is just a simple experiment to learn about this library, and I am using the "directors.list.gz" file from IMDb (ftp://ftp.fu-berlin.de/pub/misc/movies/database/) as my input file.
My code compiles, via MSVC-10, but the process aborts when executed. There's not much information from the error message except for the error code being R6010.
Can someone please point me a direction in terms of what may have caused this and how do I make this work?
This library looks pretty neat and I do hope to use it correctly. Thanks a lot for helping.
#include <fstream> // isalpha
#include <iostream> // EOF
#include <boost/iostreams/categories.hpp> // input_filter_tag
#include <boost/iostreams/operations.hpp> // get
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/filter/zlib.hpp>
using namespace std;
namespace io = boost::iostreams;
int main()
{
if(true)
{
string infile_path = "c:\\Temp\\directors.list.gz";
ifstream infile(infile_path, ios_base::in | ios_base::binary);
io::filtering_streambuf<io::input> in; //filter
in.push(io::zlib_decompressor());
in.push(infile);
//output to cout
io::copy(in, cout);
}
return 0;
}
The gzip file format has an additional header around the zlib data, which zlib can't read.
So you want to use boost's gzip_decompressor instead of zlib_decompressor.
in.push(gzip_decompressor());
Note you'll need to include boost/iostreams/filter/gzip.h instead of boost/iostreams/filter/zlib.h.
Here's a working example of streaming a GZIP file:
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/copy.hpp>
using namespace boost::iostreams;
int main()
{
std::ifstream file("hello.gz", std::ios_base::in | std::ios_base::binary);
filtering_streambuf < input > in;
in.push(gzip_decompressor());
in.push(file);
boost::iostreams::copy(in, std::cout);
}
You'll find more information on specific boost::iostreams filters lurking here in boost's documentation: http://www.boost.org/doc/libs/1_46_1/libs/iostreams/doc/quick_reference.html#filters
I also feel I should point out that your code didn't compile with gcc: in the C++ standard library, the ifstream constructor takes a const char *, not a std::string. (I'm not sure about Microsoft's version).