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());
Related
I want to write a bash script that will take an input name and create a new .cpp file that has all of the headers in it already.
tldr; I want a script that makes a .cpp file with this in it already
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
}
You could use this:
#!/bin/bash
CONTENT="#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
}"
echo "$CONTENT" > $1
Run the script using ./script.sh <target>, e.g: ./script.sh example.cpp
...Am trying to load/capture the output of system(char* command) function to a variable, a vector. can i have any possible way to push the output to my vector? I don*t want to write the output to file and read it again.
Sample code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
vector <string> dir;
system("pwd");//here i used this to print the current directory, and i want to store this out put to my vector. something like...(below )
output=output of system("pwd");//this is not a real code,just to notice i want to put the out put to other var and push.
dir.push_back(output);
return 0;
}
Can i have any scenario to do this task, thanks.
I'd recommend doing it like this:
FILE *fp = popen("fortune","r");
char line[200];
while(!feof(fp)) {
fgets(line,200,fp);
// process here
}
pclose(fp);
If it's really performance critical it's probably better to
create a child process using fork() and pipes for stdin/stdout of that child
process to write or read from.
An example of this could be found here (http://www.microhowto.info/howto/capture_the_output_of_a_child_process_in_c.html#idp21888) if you're intested. But the popen method is probably the most simple and straightforward one in your case.
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
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).
I'm planning to make a program that would work between a folder on my computer and my NAS.
It would list all the files in both folders, then determine which file is newer, and then upload it to the other device.
I know how to upload files via FTP, but I'm stuck at the start, because I don't know how to list my files. I have looked a little at using FindFirstFile() and FindNextFile() with WIN32_FIND_DATA. This way, I can get the last write data, but this doesn't let me list subdirectories.
Do you know any easy way listing all files in a folder and its subdirectory and saving the information of every file in a list?
The easy way is to use boost::recursive_directory_iterator.
#include <boost/foreach.hpp>
#include <iostream>
#include <vector>
#include <boost/filesystem.hpp>
#include <boost/date_time.hpp>
#include <algorithm>
#include <iterator>
#include <ctime>
using boost::filesystem::path;
using boost::filesystem::recursive_directory_iterator;
using boost::filesystem::directory_entry;
using boost::filesystem::filesystem_error;
using boost::filesystem::last_write_time;
using std::vector;
using std::cout;
using std::copy;
using std::ostream_iterator;
using std::time_t;
using boost::posix_time::from_time_t;
int main(int ac, const char **av)
{
vector<const char*> args(av+1, av+ac);
if(args.empty())
args.push_back(".");
vector<directory_entry> files;
BOOST_FOREACH(path p, args)
{
boost::system::error_code ec;
copy(recursive_directory_iterator(p, ec),
recursive_directory_iterator(),
back_inserter(files));
}
BOOST_FOREACH(const directory_entry& d, files)
{
if(exists(d.path()))
{
cout << from_time_t(last_write_time(d.path())) << " " << d.path() << "\n";
}
}
}
FindFirstFile() and FindNextFile() does let you list subdirectories. One of the members of WIN32_FIND_DATA is dwFileAttributes which will include FILE_ATTRIBUTE_DIRECTORY for a directory entry. Simply start another FindFirstFile() in that subdirector, rinse, repeat.
There is a sample on MSDN that shows how to use the FindFirstFile API, here.