Cannot Output Data Into a Text File - c++

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.

Related

I have a problem with reading from a file

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
fstream file, save_file;
vector<string> words;
for (int i = 0; i < 1000; i++)
file >> words[i];
cout << words[0];
return 0;
}
I want to save these words in a vector, but I can't. I have a message: zad1.exe is already runing! Please close it first to compile successfully! I don;t know why.
Here is my solution:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
int main()
{
std::fstream file("test.txt"), save_file;
std::vector<std::string> words;
std::string line;
for (int i = 0; i < 1000; i++){
file >> line;
words.push_back(line);
}
std::cout << words[0];
return 0;
}
Instead of test.txt write your file path
On some operating systems you cannot write to a file if that file is a running program. That is what the error message is telling you. You are creating a program called zad1.exe and it is currently running so you cannot create a new version of zad1.exe until you stop the version that is already running.
Are you working on Windows? If so then use the task manager to kill any versions of zad1.exe that you can see.
Plus you have many problems with the code as pointed out in the comments above. But the first task is to kill those running programs.

How Do I Make .Exe Duplicate Itself in C++?

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

C++ ifstream will not open any files

Whenever I try to open a file with ifstream, it compiles fine, but will not open the file.
The file in this example doesn't exist, but ifstream *s*should*s* create the file for me.
i have some example code that i think should work, but does not open or create the file
"foo.txt". Is there something that i'm missing, or is my IDE just messed up?
i'm using visual studio 2008 VC++ , btw
thanks
here's the code:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream in;
string hold;
in.open("foo.txt",ios::in);
if(!in){
cerr << "Couldn't open file!" << endl;
}
in >> hold;
cout << hold << endl;
system("pause");
return 0;
}
The problem is you are using an in stream instead of an out stream, as Adam Liss mentioned(ios::out rather than ios::in). You also need to make sure you close the file before return 0; to make sure everything from the buffer is actually written to the file.
The open function will not create files in ios::in mode; you need to use ios::out.

How create file in C++ in a specific place in the PC

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");

Creating files in C++

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();