I'm new to C++ and I want my application to duplicate itself once or twice when executed. I've searched on this forum for this topic, and had no luck.So I hope I'm not digging up something thats already been discussed. The code I've got so far is below, and I'm just experimenting and trying to see what different things i can do in C++ - the program I'm making has no practical use.
The code I've got so far as below.
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <fstream.h>
void main()
{
int id;
cout << "Hello\n";
cin >> id;
cout << "Testing" << id << endl;
int i=0;
do
{
i++;
fstream feck ("lucinada.doc", fstream::in | fstream::out);
feck << "testing";
24
}while(i<100);
int j=0;
do
{
j++;
fstream feck ("ludacris.doc", fstream::in | fstream::out);
feck << "testing";
}while(j<100);
int k=2;
do
{
k++;
fstream feck ("400l.doc", fstream::in | fstream::out);
feck << "testing";}while(k<100);}
How can i duplicate my application .exe to multiple path.
Thanks in advance
Get the path to your executable file by using GetModuleFileName then copy it using CopyFile.
This solution will only work under Windows of course but as far as it goes for filesystem operations you'll always have to implement them for each OS (unless you use something like boost::filesystem).
Use GetModuleFileName to get the path of your exe, then use CopyFile to copy it to a new destination.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363851(v=vs.85).aspx
Related
I am simply want to read text from a file and don't know why code is not working. I have already put correct text file name on folder from where program is running. I must be doing something small. Please highlight issue in code below:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include<cstdio>
using namespace std;
#ifdef WIN32
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
std::string get_working_path() {
char cwd[1024];
if (GetCurrentDir(cwd, sizeof(cwd)) != NULL)
return std::string(cwd);
else
return std::string("");
}
int main() {
string line;
//ofstream myfile;
//myfile.open("cmesymbols.txt", ios::out | ios::app | ios::binary);
ifstream myfile("cmd.txt");
if (myfile.is_open()) {
while (getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();
}
else
std::cout << "File not found in cwd: " << get_working_path();
myfile.close();
return 0;
}
Output: File not found in cwd:
This code is working fine. I found that folder settings in machine is to hide known extensions of files. I deliberately put name as "cmd.txt" however actual name came up as "cmd.txt.txt" and because of this code is not finding this file..
I corrected file name as "cmd.txt" and the code is working now.
ifstream myfile("cmd.txt"); doesn't create the file for you.
So make sure the file "cmd.txt" exists in your project directory together with your main.cpp (or main source file).
I am experimenting with some code for outputting information to a file. The file address appears to be correct and the code compiles but the file never populates. Can you see a problem?
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int size = 10;
for(int i=0; i<size; ++i)
{
ofstream outputfile;
outputfile.open("C:MyFolder\outputfile.txt", ios::app);
outputfile << "SYMBOL, STOCK_PRICE" << endl;
outputfile << i << endl;
outputfile.close();
}
}
When fixing the path to be an actual Windows path, it runs ok for me;
outputfile.open("C:\\MyFolder\\outputfile.txt", ios::app);
maybe the path is not right, you did not escape the backslashes. otherwise the code is fine, and worked for me.
Others have given answer to your problem. I also suggest you to open and close file only once (outside loop), and do only file-writing within loop.
I am working on a project where I intend on connecting to a database, grabbing a .csv file, reading it, manipulating the data and then returning it back to the database. Fairly simple and straight forward but I am still learning so if any one could point me in the right direction I would greatly appreciate it. Right now I have a simple program that is trying to read a .csv file and return the values to me printed on the console. I have been trying to find some good online resources for this but have came up short. Here is my code for what I have stumbled through so far.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
int loop = 1;
while(loop = 1)
{
cout << "Welcome! " << endl;
ifstream myfile;
myfile.open ("..\\ Source\External\\ Sample.csv", ifstream::in);
The real path to this file is C:\Documents and Settings\RHatfield\My Documents\C++\Product Catalog Creator\Source\External\Sample.csv
while (myfile.good())
cout << (char) myfile.get();
myfile.close();
system("PAUSE");
return 0;
}
}
Now the issue is it does not print the values so I do not know that they are properly being captured. I have a feeling it is my file path but that's the only way I can find to write it without it throwing errors. I think it's something with the spaces in the file path but I can't seem to find another way to make it work. I am not looking for a handout and this is not homework or just regular work. I am trying to learn and having trouble teaching myself so if someone knows what the issue is and can help me fix it or even point me to a relevant article online would be greatly appreciated.
Try the following code. I think the problem was you were making an assignment statement in the while condition statement.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
cout << "Welcome! " << endl;
ifstream myfile;
myfile.open ("C:\\Documents and Settings\\RHatfield\\My Documents\\C++\\Product Catalog Creator\\Source\\External\\Sample.csv", ifstream::in);
while (myfile.good())
cout << (char) myfile.get();
myfile.close();
system("PAUSE");
return 0;
}
Hey all, I have a problem, I don't know how to create a file in C++ in a specific place in the PC. For example a file (.txt) in C:\file.txt. Can anybody help me? Thank you :)
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream ofs("c:\\file.txt");
if (ofs) {
ofs << "hello, world!\n";
}
return 0;
}
It's probably fooling you because it's easier than you think. You just open a file for create and give it that path name. Voila.
See, eg,
// fstream::open
#include <fstream>
using namespace std;
int main () {
fstream filestr;
// You need a doubled backslash in a C string
filestr.open ("C:\\file.txt", fstream::out);
// >> i/o operations here <<
filestr.close();
return 0;
}
#include <stdio.h>
....
FILE *file;
file = fopen("c:/file.txt", "w");
I want to create a file using C++, but I have no idea how to do it. For example I want to create a text file named Hello.txt.
Can anyone help me?
One way to do this is to create an instance of the ofstream class, and use it to write to your file. Here's a link to a website that has some example code, and some more information about the standard tools available with most implementations of C++:
ofstream reference
For completeness, here's some example code:
// using ofstream constructors.
#include <iostream>
#include <fstream>
std::ofstream outfile ("test.txt");
outfile << "my text here!" << std::endl;
outfile.close();
You want to use std::endl to end your lines. An alternative is using '\n' character. These two things are different, std::endl flushes the buffer and writes your output immediately while '\n' allows the outfile to put all of your output into a buffer and maybe write it later.
Do this with a file stream. When a std::ofstream is closed, the file is created. I prefer the following code, because the OP only asks to create a file, not to write in it:
#include <fstream>
int main()
{
std::ofstream { "Hello.txt" };
// Hello.txt has been created here
}
The stream is destroyed right after its creation, so the stream is closed inside the destructor and thus the file is created.
#include <iostream>
#include <fstream>
int main() {
std::ofstream o("Hello.txt");
o << "Hello, World\n" << std::endl;
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string filename = "/tmp/filename.txt";
int main() {
std::ofstream o(filename.c_str());
o << "Hello, World\n" << std::endl;
return 0;
}
This is what I had to do in order to use a variable for the filename instead of a regular string.
Here is my solution:
#include <fstream>
int main()
{
std::ofstream ("Hello.txt");
return 0;
}
File (Hello.txt) is created even without ofstream name, and this is the difference from Mr. Boiethios answer.
If you want to create a file with some content and don't need to deal with the ofstream after that you can simply write:
#include <fstream>
int main() {
std::ofstream("file.txt") << "file content";
}
no need to manually close the file, deal with variables, etc. The file is created, written, and closed in the same line.
/*I am working with turbo c++ compiler so namespace std is not used by me.Also i am familiar with turbo.*/
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<fstream.h> //required while dealing with files
void main ()
{
clrscr();
ofstream fout; //object created **fout**
fout.open("your desired file name + extension");
fout<<"contents to be written inside the file"<<endl;
fout.close();
getch();
}
After running the program the file will be created inside the bin folder in your compiler folder itself.
use c methods FILE *fp =fopen("filename","mode");
fclose(fp);
mode means a for appending
r for reading ,w for writing
/ / using ofstream constructors.
#include <iostream>
#include <fstream>
std::string input="some text to write"
std::ofstream outfile ("test.txt");
outfile <<input << std::endl;
outfile.close();