I've never used string or string functions until today and I'm running into a problem that I don't understand. This program as is, should just accept a command line argument, load the file and display it to memory. However it displays it multiple times. I'm pretty sure the for loop is the problem, but it is the same technique as what is used in the programming reference I am using.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
char* getFile( char* fileName ){
std::fstream inFile( fileName );
if( !inFile ) std::cout << "Could not open " << fileName << ".\n";
else{
inFile.seekg(0,inFile.end);
int len = inFile.tellg();
inFile.seekg(0,inFile.beg);
char* buffer = new char[len];
inFile.read( buffer, len);
inFile.close();
std::cout.write(buffer,len);
return buffer;
}
}
int main(int argc, char** argv){
if(argc != 2) std::cout << "Parameter required\n";
else{
std::string f = getFile( argv[1] );
for( size_t i = f.find( 0x0A, 0 ); i != std::string::npos ; i = f.find( 0x0A, i) ){
std::cout << f.substr(0,i)<<std::endl;
i++;
}
}
}
I see at least one of the problems with my code. I re-wrote the loop as a while loop because it was easier to follow and paid a little more attention to where I am starting and stopping. However it still seems to be printing twice.
int main(int argc, char** argv){
if(argc != 2) std::cout << "Parameter required\n";
else{
std::string f = getFile( argv[1] );
size_t start = 0;
size_t end = 1;
while( end != std::string::npos ){
end = f.find( 0x0A, start );
std::cout << f.substr(start,end)<<std::endl;
start = ( end + 1 );
}
This is because you have two printing statements that are displaying the contents of the file.
The first print statement is this one:
std::cout.write(buffer,len);
The second one is this:
std::cout << f.substr(0,i)<<std::endl;
Related
I have the following code:
#include <cstdlib>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <vector>
void tokenize( const std::string& str, char delim, std::vector<std::string> &out )
{
std::size_t start;
std::size_t end = 0;
while (( start = str.find_first_not_of( delim, end )) != std::string::npos )
{
end = str.find( delim, start );
out.push_back( str.substr( start, end - start));
}
}
int main( int argc, char** argv )
{
if ( argc < 2 )
{
std::cout << "Use: " << argv[0] << " file1 file2 ... fileN" << std::endl;
return -1;
}
const char* PATH = getenv( "PATH" );
std::vector<std::string> pathFolders;
int fd = open( "/dev/null", O_WRONLY );
tokenize( PATH, ':', pathFolders );
std::string filePath;
for ( int paramNr = 1; paramNr < argc; ++paramNr )
{
std::cout << "\n" << argv[paramNr] << "\n-------------------" << std::endl;
for ( const auto& folder : pathFolders )
{
switch ( fork() )
{
case -1:
{
std::cout << "fork() error" << std::endl;
return -1;
}
case 0:
{
filePath = folder + "/" + argv[paramNr];
dup2( fd, STDERR_FILENO );
execl( "/usr/bin/file", "file", "-b", filePath.c_str(), nullptr );
break;
}
default:
{
wait( nullptr );
}
}
}
}
return 0;
}
I want to redirect messages like "cannot open `/sbin/file1' (No such file or directory)" to /dev/null, but apparently the error messages aren't redirected to /dev/null.
How can I redirect STDERR to /dev/null?
Edit: I've tested my code with an 'ls' command, and the error message that I've got there was redirected. What I think the issue is here, that the 'file' command doesn't write error message to STDERR.
You are successfully redirecting standard error to /dev/null. The reason you're seeing that message anyway is that file writes its error messages like cannot open `/sbin/file1' (No such file or directory) to standard output, not to standard error. This seems like the one place in their code they use file_printf instead of file_error. And yes, this seems like a serious wart in file, though it's been that way since 1992 with a comment saying it's on purpose, so I wouldn't count on them changing it any time soon.
This might be a basic question, but I am new to C++, so I would appreciate your understanding.
What I am trying to do is to read files from a folder and pass one of them at a time in argv[1], get some results from that file, write them in an output file specified in argv[2], then the same for the next file, and so on.
I guess it has something to do with pointers but I am not sure how to fix this.
Thank you for any input!
Here is what I have so far:
vector<string> getFilenames(string folder)
{
vector<string> names;
string search_path = folder + "/*.*";
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(search_path.c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
// read all (real) files in current folder
// , delete '!' read other 2 default folder . and ..
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
names.push_back(fd.cFileName);
}
} while (::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
return names;
}
int main(int argc, char **argv) {
ofstream fileout(argv[2], ios::out | ios::app);
int a, b, c;
vector<string> filenames = getFilenames("folder path");
istringstream iss;
for (int i = 0; i < filenames.size(); i++) {
iss.str(filenames[i]);
iss >> argv[1];
if (readFile(a, b, c, argv[1]) == 0) {
/* do something */
}
}
fileout.close();
return 0;
}
I want to complete #Florian Klemme but I cannot comment.
There is a little example of how to iterate through command line parameters :
int main(int argc, char **argv)
{
for (auto i = int{0}; i < argc; ++i) {
// do something with argv[i];
}
}
I'm not exactly sure what your problem is but maybe this little example can help as a starting point. It shows how you can
Iterate though files in a directory (you need C++17 though)
Read some number from a file
Write to another file
Hopefully you can add the rest with no problem. :)
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
namespace fs = std::filesystem;
int main(int argc, char **argv) {
std::string input_dir = argv[1];
std::string output_file = argv[2];
std::ofstream out(output_file);
// Iterate over files in directory
for(auto& file : fs::directory_iterator(input_dir)) {
std::ifstream in(file.path());
// In this example, read a integer number from the current file
if (int i; in >> i) {
std::cout << "Read " << i << " from " << file << std::endl;
// ... and write it to the output file.
out << i << std::endl;
}
}
}
I am writing a simple program to take in two files. The terminal command line looks like this.
./fileIO foo.code foo.encode
When it runs, the second file is not read in. When I enter
./fileIO foo.code foo.code
it works. I can't seem to figure out why the second one is not opening. Any ideas? Thanks!
#include <fstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <map>
#include <string>
#include <cassert>
using namespace std;
int main( int argc, char *argv[] )
{
// convert the C-style command line parameter to a C++-style string,
// so that we can do concatenation on it
assert( argc == 3 );
const string code = argv[1];
const string encode = argv[2];
string firstTextFile = code;
string secondTextFile = encode;
//manipulate the first infile
ifstream firstFile( firstTextFile.c_str(), ios::in );
if( !firstFile )
{
cerr << "Cannot open text file for input" << endl;
return 1;
}
string lineIn;
string codeSubstring;
string hexSubstring;
while( getline( firstFile, lineIn ) )
{
hexSubstring = lineIn.substr(0, 2);
codeSubstring = lineIn.substr(4, lineIn.length() );
cout << hexSubstring << ", " << codeSubstring << endl;
}
//manipulate the second infile
ifstream secondFile( secondTextFile.c_str(), ios::in );
if( !secondFile )
{
cerr << "Cannot open text file for input" << endl;
return 1;
}
char characterIn;
while( secondFile.get( characterIn ) )
{
cout << characterIn << endl;
}
return 0;
}
One thing you might want to try is adding the close() call as is standard procedure after you're done using files. Sometimes issues arise with re-opening files if they were not closed properly in a previous run.
firstFile.close();
secondFile.close();
Also, you may try restarting the computer if there is some lingering file handle that hasn't been released.
I'm running some code on Mac OSX 10.6.6 and XCode 3.2.4 and I have some pretty standard code: fork(), if pid == 0 then execvp with a command and the args (the args include the command as the first element in the array, and the array is null terminated).
We're going over this in my Operating Systems class and our assignment is to write a simple shell. Run commands with their args and switches, both redirects (< and >) and pipe (|). I'm getting several problems.
1) Sometimes I get the EXC_SOFTWARE signal while debugging (so far I haven't gotten it if I run the app outside of XCode, but I'm new to Mac and wouldn't know what that would look like if I did)
2) Sometimes the getline for the next command gets junk that seems to be printed by other couts. This begins looping forever, exponentially breaking. I have tested with printing getpid() with every prompt and only the beginning process prints these out, I don't appear to have an accidental "fork bomb."
Here's what I have so far:
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;
char** Split(char* buffer, int &count) {
count = 1;
for (int i = 0; i < strlen(buffer); i++) {
if (buffer[i] == ' ') {
count++;
}
}
const char* delim = " ";
char* t = strtok(buffer, delim);
char** args = new char*[count + 1];
for (int i = 0; i < count; i++) {
args[i] = t;
t = strtok(NULL, delim);
}
args[count] = 0;
return args;
}
void Run(char** argv, int argc) {
int pid = 0;
if ((pid = fork()) == 0) {
//for testing purposes, print all of argv
for (int i = 0; i < argc; i++) {
cout << "{" << argv[i] << "}" << endl;
}
execvp(argv[0], argv);
cout << "ERROR 1" << endl;
exit(1);
} else if (pid < 0) {
cout << "ERROR 2" << endl;
exit(2);
}
wait(NULL);
}
int main(int argc, char * const argv[]) {
char buffer[512];
char prompt[] = ":> ";
int count = 0;
while (true) {
cout << prompt;
cin.getline(buffer, 512);
char **split = Split(buffer, count);
Run(split, count);
}
}
It's exactly what I have, you should be able to cut, paste, and build.
I'm not the best at C++, and chances are there's a memory leak when I don't delete split but my main focus is the EXC_SOFTWARE signal and see what I'm doing wrong with my looping issue. Any thoughts?
EDIT:
The assignment requires very limited error checking and I'm assuming all input is correct. By correct I mean properly formatted and limited for my app to run the command, i.e. no bizarre space count, no & to run async, no multi piping commands, etc.
One problem is that you do not check the return from cin.getline(), so if you type EOF, the code goes into a tight loop. You're also leaking memory.
Try:
while (cout << prompt && cin.getline(buffer, sizeof(buffer))
{
int count = 0;
char **split = Split(buffer, count);
Run(split, count);
delete[] split;
}
The code in Split() does not really handle blank lines at all well. It seems to take an aeon to run execvp() when the only arguments are null pointers, which is what happens if you return a blank line.
I'm able to run multiple simple commands (such as 'vim makefile' and 'make shell' and 'ls -l' and 'cat shell.cpp' and so on - I even did a few with more than two arguments) OK with this, and I can quit the command (shell) with Control-D and so on. I have fixed it so it compiles with no warnings from g++ -O -Wall -o shell shell.cpp. I have not fixed the splitting code so that it handles empty lines or all blank lines correctly.
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;
char** Split(char* buffer, int &count) {
count = 1;
for (size_t i = 0; i < strlen(buffer); i++) { // #1
if (buffer[i] == ' ') {
count++;
}
}
char** args = new char*[count + 1];
const char* delim = " ";
char* t = strtok(buffer, delim);
for (int i = 0; i < count; i++) {
args[i] = t;
t = strtok(NULL, delim);
}
args[count] = 0;
return args;
}
void Run(char** argv, int argc) {
int pid = 0;
if ((pid = fork()) == 0) {
//for testing purposes, print all of argv
for (int i = 0; i < argc; i++)
{
if (argv[i] != 0) // #2
cout << "{" << argv[i] << "}" << endl;
else
cout << "{ NULL }" << endl; // #3
}
execvp(argv[0], argv);
cout << "ERROR 1" << endl;
exit(1);
} else if (pid < 0) {
cout << "ERROR 2" << endl;
exit(2);
}
wait(NULL);
}
int main(int argc, char * const argv[]) {
char buffer[512];
char prompt[] = ":> ";
while (cout << prompt && cin.getline(buffer, sizeof(buffer))) // #4
{
int count = 0;
char **split = Split(buffer, count);
if (count > 0) // #5
Run(split, count);
delete[] split; // #6
}
}
I've marked the significant changes (they mostly aren't all that big). I'm compiling with GCC 4.2.1 on MacOS X 10.6.6.
I can't readily account for the garbage characters you are seeing in the buffer.
You're making the assumption that the input line contains one more token than spaces. This assumption may fail if the input line is empty, ends or begins with a space or contains multiple consecutive spaces. In these cases, one of the calls to strtok will return NULL, and this will crash the forked process when you try to print that argument in Run. These are the only cases in which I've encountered problems; if you've encountered any others, please specify your input.
To avoid that assumption, you could do the counting with strtok the same way you do the tokenizing. That's generally a good idea: if you need two things to coincide and you can do them the same way, you introduce an additional source of errors if you do them differently instead.
I've been scratching my head for quite some time now, this code worked fine when I first used cmd to go inside the project\debug folder then run the program there. Then I added the if(in) and else part then it started giving me "debug assertion failed" errors mbstowcs.c
Expression s != NULL
It just doesn't make any sense to me..
I used this command in cmd: prog.exe test.txt nuther.txt
Both files exists inside the debug folder and the main project folder..
Any ideas?
int main(int argc, char **argv)
{
parse_opts(argc, argv); //parse the arguments
return 0;
}
void parse_opts(int argc, char **argv)
{
string compl_out;
if( argc > 1 )
{
for( int i = 1; i < argc; i++ )
{
if( argv[i][0] = '>' )
{
ofstream out_file(argv[i+1]);
out_file << compl_out;
out_file.close();
break;
}
ifstream in(argv[i]);
string buff;
if(in)
{
while(getline( in, buff ))
cout << buff << endl;
compl_out.append(buff);
}
else
{
cout << "Can't open file: " << argv[i]
<< ", file doesn't exist or is locked in use. " << endl;
}
}
}
else
{
usage();
}
}
First impressions:
if( argv[i][0] = '>' )
should be:
if( argv[i][0] == '>' )
You are assigning instead of comparing.
I think you also might have intended the compl_out.append to be inside the while loop? As it is it won't append anying to that buffer:
while(getline( in, buff ))
{
cout << "buf" << buff << endl;
compl_out.append(buff);
}