Code
while (dataFile)
{
getline(dataFile, input, '$');
if (getenv("windir"))
{
cmd = "open -a 'Google Chrome' http://www.dictionary.reference.com/browse/" + input;
}
else
{
cmd = "open -a 'Safari' http://www.dictionary.reference.com/browse/" + input;
}
system(cmd.c_str());
cmd.erase(cmd.find(input));
cout << cmd;
}
When ever there are more than one words on the .txt file it will only look up the first word and than it can't do the others. It says this in the output sh: line 2: WORD: command not found. How can I get it to reset the command so the other words will work?
Initialization of cmd and input is inside the main method.
string cmd;
string input;
This is how the text file would look like:
Word1
Word2
WordETC
I can't speak for the Safari path, I'm assuming in OSX or similar, but on Windows 7 the following code will open a new tab in Google chrome
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
std::ifstream dataFile("C:\\test.txt", std::ifstream::binary);
string input;
std::string cmd;
// This path will depend on the operation system
std::string googlePath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";
while (dataFile)
{
getline(dataFile, input, '\n'); // use \n for endline
if (getenv("windir"))
{
cmd = "cmd /C start \"" + googlePath + "\" http://www.dictionary.reference.com/browse/" + input + " --newtab";
}
else
{
cmd = "open -a 'Safari' http://www.dictionary.reference.com/browse/" + input;
}
system(cmd.c_str());
cmd.erase(cmd.find(input));
cout << cmd;
}
return 0;
}
test.txt
turtle\r\n
dove\r\n
shoulder
Related
hopefully nothing too complicated :)
When I debug my application in VS 2022, I would like all the std::cout that is in the console to be saved to a .txt file, but also still show in the console itself. I added > output.txt to the command line arguments, but realized that means nothing will show in the console :/
i think u can u swich case in a do-while loop
1 is input, which u can choose to save in text file
2 is output, which read your text file
Or you can just do like this
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string s;
cout << "Enter something: ";
getline(cin, s);
fstream file;
file.open("text.txt", ios::out);
file << s;
file.close();
file.open("text.txt", ios::in);
if (file.is_open())
{
string line;
while (getline(file, line))
{
cout << line << endl;
}
}
return 0;
}
You can use OutputDebugStringA and cout at the same time.
And set Redirect all Output Window text to the Immediate Window in Tool> Option.
I want to open a file, and in each line append a string in the end.
I have this code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
//argv[1] input file
//argv[2] string to add in the end of each line
//argv[3] output file
int main(int argc, char *argv[]){
ifstream open_file(argv[1]);
if (!open_file) {
std::cerr << "Could not open input file\n";
return 0;
}
ofstream new_file(argv[3]);
if (!new_file) {
std::cerr << "Could not create output file\n";
return 0;
}
string s = argv[2];
string str;
while (getline(open_file, str)) {
new_file << str << s << "\n";
}
}
The thing is the string is not adding in the end of each line. It is creating a new line for each string trying to be appended.
So I run for example: ./appendstring.e wordlist.txt hello new_wordlist.txt
and this is the output:
I don't really know what I'm doing wrong here.
Thanks in advance.
Perhaps your first file contains \r\n sequences for End of Line..
You may have to remove the \r character that is already in your first file, because you are reading in the string with the \r on the end.
Trim the \r off the end of str, before you use this line of code:
new_file << str << s << "\n";
See here
http://www.cplusplus.com/reference/string/string/getline/
I am trying to open a file with C++ and output the text that is in the file. I cannot seem to figure out what I am doing wrong. Here is what I have so far.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
char fileName[50];
ifstream infile;
cout << "Enter the name of the file you would like to open: ";
cin.getline(fileName, 50);
infile.open(fileName);
if(!infile.is_open())
{
exit(EXIT_FAILURE);
}
char line[75];
infile >> line;
while (infile.good())
{
cout << line << " ";
infile >> line;
}
system("pause");
return 0;
}
After I input the file name and press enter the CMD prompt just closes. I know that the file exist, but I cannot figure out why it is exiting. Obviously it is because of the exit command, but it should be open. What am I doing wrong?
You don't need to read/write the file line by line; C++ already supports to copy the file in one step. You also should use string instead of char[] for your strings; on one hand it means that you don't need to restrict the maximal length of your strings to some arbitrary length (what if your file's pathname has more than 50 characters, or the file has lines with more than 75 characters?
Note also that your file copying code is erroneous: It will remove all whitespace from the file, as infile >> line does not read a line (use readline for that), but a word, discarding whitespace.
Also, your code should give an error message if it couldn't open the file, instead of just silently returning (you do provide an error return, which is very good, but unless you call it from something that actually gives you feedback on the error return, you'll never learn about it.
Finally, the system("pause") should probably be done in an RAII class, so it is guaranteed to be exited on return (however, exit will not call destructors, so unless you want to use atexit, you should use return in `main`` instead). A better idea would, however, be to not put this into the code, but instead run it in a terminal that doesn't immediately close after the program finishes.
Here's a program that implements those suggestions:
#include <iostream>
#include <fstream>
#include <cstdlib>
int main()
{
// make sure that system("pause") is called on all exit paths
struct cleanup
{
~cleanup() { std::system("pause"); }
} do_cleanup;
// get the file name
std::string filename;
std::cout << "Enter the name of the file you would like to open: ";
std::getline(std::cin,filename);
if (!std::cin)
{
std::cerr << "Failed to read the file name.\n";
return EXIT_FAILURE;
}
// open the file
std::ifstream infile(filename.c_str());
if (!infile)
{
std::cerr << "Could not open file: " << filename << "\n";
return EXIT_FAILURE;
}
// print the file
std::cout << infile.rdbuf();
// close the file
infile.close();
if (!infile)
{
std::cerr << "Could not properly close file: " << filename << "\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
There is no need to use a char[]. You've even #included string so just use that.
string fileName;
cout << "Enter the name of the file you would like to open: ";
cin >> fileName;
// or
// getline(cin, fileName);
ifstream infile(fileName);
if (infile.fail()) {
exit(EXIT_FAILURE);
}
string line;
while (infile >> line) {
cout << line << " ";
}
system("pause");
return 0;
I also modified a few things to make it a bit cleaner.
Thanks for the help. Yes the file was in the wrong folder. It was a newb oversight!
Currently I have code that uses cin to get the name of the file to be used as input when the program is executed. I would like to have it so that when i run the program i can add the file redirection and the filename and run it as such: ./a.out < file.txt. How can i feed my file into my code using redirection.
this is an example of how i am currently taking input:
int main(){
std::string filename;
std::cin >> filename;
std::ifstream f(filename.c_str());
}
Do it like this
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string line;
std::cout << "received " << std::endl;
while(std::getline(std::cin, line))
{
std::cout << line << std::endl;
}
std::cout << "on stdin" << std::endl;
}
You do not need to open the file yourself as the file content is passed to you on stdin as a result of using < on the command line.
This code has a drawback that if you do not input anything on stdin it freezes. Detecting that stdin is empty can only be achieved in a non portable manner (see here).
It would be better to accept your filename as a normal command line argument and open it inside your program.
Please see my code below.
ifstream myLibFile ("libs//%s" , line); // Compile failed here ???
I want to combine the path string and open the related file again.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("libs//Config.txt");
// There are several file names listed in the COnfig.txt file line by line.
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
// Read details lib files based on the each line file name.
string libFileLine;
ifstream myLibFile ("libs//%s" , line); // Compile failed here ???
if (myLibFile.is_open())
{
while (! myLibFile.eof() )
{
cout<< "success\n";
}
myLibFile.close();
}
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Assume my [Config.txt] include the content below. And all the *.txt files located in libs folder.
file1.txt
file2.txt
file3.txt
The ifstream constructor does not work that way. Its first parameter is a C string to the file you want to open. Its second parameter is an optional set of mode flags.
If you want to concatenate the two strings, just concatenate the two strings:
std::string myLibFileName = "libs/" + line;
ifstream myLibFile(myLibFileName.c_str());
// Or, in one line:
ifstream myLibFile(("libs/" + line).c_str());
(the call to c_str() is required because the ifstream constructor takes a const char*, not a std::string)