Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I didn't got any errors, but my C++ code is still not working. It's really simple:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string a;
ofstream fout("char.out");
ifstream fin("char.in");
fin >> a;
fout << a;
return 0;
}
char.in after running:
uiui
char.out after running:
Did I missed anything simple in my code?
P. S. : I got Norton Antivirus and my project folder is missed from AutoCheck.
in fact for reading and writing you should open and close file but you didn't close.
Also you have two files where you have done writing from one file and reading from another file, I wonder how you expect to get the correct output.
this is how it should be :
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string a;
ofstream fout("char.out");
// check if file is created
if(fout.is_open()){
// do writing in file
}
else
cout << "can not open file\n";
fout.close();
//-----------reading the file----------
// use the same file
ifstream fin("char.out");
if(fun.is_open()){
// do reading from file
std::cout << a << std::endl;
}
else
cout << "can not open file\n";
fin.close();
return 0;
}
And if you want to add a line of text to the end of the file, you must add:
ofstream fout("filename" , ios::app);
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I am trying to write to a .txt file within a program, and I am using the following code snippet. I want to output a line of the form:
a(space)b
but I get nothing on the txt file.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(){
string a = "string";
int b = 0;
fstream f;
f.open("filename.txt",ios::in | ios::out );
f << a << ' ' << b << endl;
f.close();
return 0;
}
If you try this piece of code:
f.open("filename.txt", ios::in | ios::out);
if (!f.is_open()) {
cout << "error";
}
you will see that the file is never opened. ios::in requires an already existing file (std::fstream::open()):
It should work if you should only pass std::ios::out to f.open():
f.open("filename.txt", ios::out);
Read here why you shouldn't be using namespace std;.
Try this version. Few changes:
Include the correct header <string> instead of <cstring>
No using namespace std;
Use std::ofstream for output
No .open(): pass the filename in the constructor
Check if the file is valid after opening
No .close(). Let the destructor do its job.
No std::endl if '\n' is enough.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string a = "string";
int b = 0;
std::ofstream f("filename.txt");
if (!f) {
return EXIT_FAILURE;
}
f << a << ' ' << b << '\n';
return EXIT_SUCCESS;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to read values from a .txt file to a vector (which is a member of a class) in C++, but despite the .txt having around 1000 lines, the vector is of size 0. I inserted a 'cout', and I know the file is opened and closed. I'm not sure what I could be doing wrong for the code not to read the contents of the .txt.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include "option_class.h"
using namespace std;
int main(){
double cprice = 0.0;
int i = 0;
string line;
ifstream is;
is.open("/Users/<USER>/Desktop/SPY.txt");
if (!is){
cout << "Unable to open file" << endl;
return(0);
}
while(!getline(is, line).eof()){
is >> cprice;
option1.price.push_back(cprice);
}
is.close();
cout << "Closing file" << endl;
}
Have you tried something simpler:
while (is >> cprice)
{
option1.price.push_back(cprice);
}
The operator>> will skip whitespace, which includes newlines. There is no need to read a line at a time.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Sir, I am creating my university project on bank database system on dev c++(object-oriented paradigm). So, I want to enter the data in c++ and wanted to save the data in the notepad file.
For Example:
Dev c++ :
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
int a;
cout<<"Enter a number"<<endl;
cin>> a;
}
:- When the user enters a number in the console of Dev C++, then the output will be saved in notepad. How will it be??
Sorry.... for unclear question...
Yes, Suraj Roa you got my logic. that show the user input number in .txt file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int a;
cout << "Enter number : " << endl;
cin >> a;
// open file stream
ofstream file;
file.open("number.txt");
file << a;
file.close();
return 0;
}
You need to create a stream to external file and then you can use it like std output stream (cout).
All you need to understand this mechanism you can find here.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I can not get Visual Studio to read in a text file. Below is the code I have. The file opens perfectly in a Unix Environment, but it does not work when copied and pasted into Visual Studio. I am using fstream to open the file. Why the file is not being read?
When I run the program, it builds but I get no output. I have an output statement cout << "inf\n". So the loop is not even being reached, which is why I believe the file is not being read. Again, when I run the same code in a Unix environment the output statement does display and the values from the file are displayed ( via tree.insert(), tree.remove() ).
I tried the solution in this link. As it suggested, I changed my working directory to $(ProjectDir)\Debug and $(ProjectDir)\Release. Also, I moved my text file from the Resources folder to my Source Folder in the Solution Explorer. However, the file still was not being read.
I also updated my code to include cerr << "Error: " << strerror(errno); directly after fstream inf ("BTREE5_1.txt"). With this line of code the output I get is
Error: No such file or directory
Can someone please explain why? My text files are in the same folder as my code as explained above.
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include "BTree.h"
using namespace std;
int main()
{
bool first = true;
BTree tree(6, 2);
int value;
char s[80], command;
ifstream inf("BTree5_1.txt");
cerr << "Error: " << strerror(errno);
inf.getline(s, 80);
while (inf >> command >> value)
{
cout << "inf\n";
if (command == 'i')
tree.insert(value);
else
{
if (first)
{
cout << "After all insertions.\n";
tree.print();
first = false;
} // if first
cout << "Deleting " << value << ". \n";
tree.remove(value);
tree.print();
// fgets(s, 80, stdin);
} // else deletion
} // while
system("PAUSE");
return 0;
} // main
The problem was that I copied and pasted my text files from the Unix Environment. To fix this I just placed the text files into my Directory from my C Drive.
ie>) C:\Users\s.proctor\Documents\Visual Studio 2015\Projects\ecs60\p2\p2\p2
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Using the tutorial here. I can't write text into file. I don't know why, please help me.
The following is the code:
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
Where does the following fail?
Does a new file named "example.txt" get created?
Try adding a couple of cout statements in between your code to see how far you get.
ofstream myfile;
cout << "here 1\n";
myfile.open ("example.txt");
cout << "here 2\n";
myfile << "Writing this to a file.\n";
cout << "here 3\n";
myfile.close();
cout << "here 4\n";
return 0;
Your code looks just like the code in the tutorial so it should work, although I haven't tested it myself.