#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
string fileTxt;
cout << "disply ok";
return 0;
}
i was working on file handling C++ ifstream but i can't get the Code to open my .txt, so i start troubleshoot and removing all the Codes to only have "hello world" program remaining...
And that still won't work, all i got is a Terminal that goes off instantly...without any text i asked it to display eg. "dsply ok";
Related
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
char fileInputPath[256] = "D:\25mb.file";
ifstream fileInput(fileInputPath, ios::in|ios::binary);
if (fileInput.is_open() != true) cout << "File not opened.\n";
return 0;
}
programm result:
File not opened
tesing system win7 x32
I would like to print and see the message in the "Output Window" in Visual Studio 2013. After running this C++ code:
#include <iostream>
using namespace std;
int main(){
cout << "hello world";
}
I cannot see the message in the output window. Instead, a black window in which the message "hello world" is written seems instantly and closes.
I disabled "Redirect all output text to the Immediate window". However, it is still not printing the message.
This behavior is expected, because the program is completing its functions. You can change the code to this:
#include <iostream>
using namespace std;
int main(){
cout << "hello world";
system("PAUSE");
}
Which will make the window pause until you hit a key to continue, but after printing hello world the program is finished running and will close.
I am making a C++ program which should be able to list the files from particular directory and save each file name as a string(which will be processed further for conversion). Do I need array of strings? Which functionality should I use. The number of files is not fixed.
Main thing is I can't enter the names manually. I must accept the names from the list generated.
In this case you want to use a vector:
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> file_names;
file_names.push_back("file1.txt");
file_names.push_back("file2.txt");
file_names.push_back("file3.txt");
file_names.push_back("file4.txt");
return 0;
}
Have you thought about using some command line tools to deal with this? Even input redirection will work for this. Example:
./Cpp < echo somedir/*
Where Cpp is the name of your compiled binary, and somedir is the directory you want to read from
Then in your c++ program, you simply use std::cin to read each filename from standard in.
#include <vector>
#include <string>
#include <iterator> // std::istream_iterator, std::back_inserter
#include <algorithm> //std::copy
#include <iostream> // std::cin
int main()
{
std::vector<string> file_names;
// read the filenames from stdin
std::copy(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>(), std::back_inserter(file_names));
// print the filenames
std::copy(file_names.begin(), file_names.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
return 0;
}
I'm going for absolute minimalism here. (It's been a while since I've worked with the Lua C API.)
#include <lua.hpp>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
lua_State* state = luaL_newstate();
luaL_openlibs(state);
string input;
while (getline(cin, input))
{
auto error = luaL_dostring(state, input.c_str());
if (error)
{
cerr << "Lua Error: " << lua_tostring(state, -1) << '\n';
lua_pop(state, 1);
}
}
lua_close(state);
return 0;
}
This program works fine as long as I feed it perfect Lua. However, if I enter something bad (such as asdf()), the program crashes! Why is it not handling my error gracefully?
I've tried breaking out the calls before. It crashes on the call to lua_pcall itself. I never make it past that line.
The binary download (5.2.1 I believe) has a bug that was corrected in 5.2.3. I rebuilt the library from source, and now my program works fine.
#include <iostream>
#include <fstream>
#include <stdlib.h> // includes the "atoi" function
#include <string>
using namespace std;
#include <sstream>;
int main()
{
std::fstream f;
f.open("file.in", std::fstream::in);
// read data
int count = 0;
std::string line = "";
getline( f, line, '\n' );
count = atoi( line.c_str() );
f.close();
f.open("file.in", std::fstream::out | std::fstream::trunc);
// write data
++count;
f << count << endl;
f.close();
return 0;
}
This works in debug mode in Visual Studio but when I run it as an application it doesn't work. I've initialized all variables so I'm not sure what else to check.
This line
f.open("file.in", std::fstream::in);
Make sure file.in is in \bin\release
I also advice your to use try/catch statements and print your errors