Send data to notepad from dev c++ [closed] - c++

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.

Related

Why is my C++ code with file output not working? [closed]

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

Reading from a .txt file to a vector of doubles in C++ [closed]

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.

Issues with ifstream reading CSV data [closed]

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 6 years ago.
Improve this question
Non-working code:
#include<iostream>
#include<fstream>
#include<string>
int main(){
int id; string name;char comma ; double money;
ifstream read("testfile.csv");
while (read >> id >> comma>> name >> comma >> money)
{cout << id <<comma<<name<<comma<<money<< endl ;}
read.close();
_getch();
return 0;}
The csv file data & structure:
1,user1,999
2,user2,33
3,user3,337
But, the following works fine. Why so?
while (read >> id >>comma>>name)
{cout << id<<comma<<name <<endl ;}
When you read a string using >>, it reads a space delimited string. If there is no space in the text you read, it will read until the end of the line (as newline is a space).
Because of this the "parsing" of the input will after a little while be out of sync with the contents from the file, and will lead to an error when attempting to read one of the numbers.

How to redirect text to a file in C++ [closed]

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 7 years ago.
Improve this question
i am trying to write a small automated program to calculate some values for me and output some text to a simple .txt file. do the redirection symbols < > & << >> work the same in C++ as they do in the command line for batch scripts? When i try to search how to redirect to a .txt file in C++. All of the examples, and tutorials i have found are presented in a manner that assumes IO on the console like the following.
cout::<<"show this text on the console";
cin::>> whatever you would call here to accept user input.
what i want to know is will it work to do it this way?
#include <string>
using namespace std;
int main()
{
int X = 0;
string zero = "touchPress 0 483 652\n";
if {
(X=0)
zero>>C:\test.txt;
x+5;
} return 0;
}
Your code does not work. I am not absolutely sure what is the desired behaviour, but this code writes the string zero to a file:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int X = 0;
string zero = "touchPress 0 483 652\n";
ofstream myFile("C:\\Data\\test.txt");
//a condition which is always true
if (X==0)
{
myFile<<zero;
X + 5; //this is valid but useless
}
return 0;
}
#include <fstream>
int main(){
string zero = "touchPress 0 483 652\n";
std::ofstream fout("test.txt"); // creates new test.txt in folder where .exe is
fout << zero; //same as cout << zero;//but in the file
return 0;
}
fout as cout, i just reworked your barely alive program. is this what you wanted?

How do you make a c++ program search for a string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How do you make a c++ program search for a string?
I'm new to the programming language c++.
Is there a way so that my program will look for a string of a .txt file and do something if the program found it?
First open an ifstream to open your file then check for the string:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
if(line.find("the string to find") != string::npos)
{
//line found, do something
}
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}