for example we have created file in c++ how to write content in this file and then output on screen?
Read this and then come back and ask if you have a more specific question, thanks.
Taken from here.
Writing:
#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;
}
Reading:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile("example.txt");
while(getline(myfile,line)) {
cout << line << endl;
}
myfile.close();
return 0;
}
You should really use Google.
Related
okay I've searched everywhere and couldn't get my hand on it so ..
i'm doing a library system where a librarian enters his username and the program checks if he is one of the librarians or not
i'm stuck on the comparing part , i tried using getline but it gave me an error , tried gets_s and used a char array instead of a string and still didn't work
kindly help me with what i should do
using namespace std;
#include <iostream>
#include <string>
#include <fstream>
int main()
{
//opening files
ifstream readUsername;
ofstream enterUsername;
//variables
string existUsername;
string enteredUsername;
//reading files
readUsername.open("librarian usernames.txt");
if (readUsername.fail())
{
cout << "can't open file" << endl;
}
enterUsername.open("entered librarian username.txt");
if (enterUsername.fail())
{
cout << "can't open file" << endl;
}
while(!readUsername.eof)
{
readUsername >> existUsername;
}
enterUsername << enteredUsername;
readUsername.close();
enterUsername.close();
enterUsername.clear();
system("pause");
return 0;
}
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
infile.open("listOfWords.txt"); //open file
for(string listOfWords; getline(infile, listOfWords, '.'); ) //read sentences including
//spaces
cout<<listOfWords; //this displays
return 0;
}
This shows you how to output the text so you should just save both files to a variable then compare the variables.
How can I read a txt file that contains non-English strings? After getting the string I will store it in a linked list, so it should be suitable for storing in a node either, then print it.
When I try the get string "türkçe" from the .txt file code below, it gives the output of:
output: tⁿrkτe
**word.txt**
türkçe
<string>
<iostream>
<fstream>
int main() {
fstream inputFile;
inputFile.open(word.txt);
string line;
getline(inputFile,line);
cout << line << endl;
return 0;
}
The solution of the problem:
#include <string>
#include <iostream>
#include <fstream>
#include <locale.h>
using namespace std;
int main() {
setlocale(LC_ALL, "turkish");
fstream inputFile;
inputFile.open("word.txt");
string line;
getline(inputFile,line);
cout << line << endl;
return 0;
}
I want the user to enter the name of a file, and if the file exists, print out all the contents of the file.
At the moment the uncommented code, takes a name of a file that the user inputs, for example. example.txt and prints out most (not the last word?) of the file. I've tried to implement this instead by using string (commented code is attempt) but clearly its incorrect.
I also wondering if i can automatically add .txt to the end of the user input, so that the console could ask - "which subject should we find more information on" user inputs "math" and it will open "math.txt"
Here is what I´ve tried:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
char filename[50];
//string getcontent;
ifstream name;
cin.getline(filename, 50);
name.open(filename);
if (!name.is_open()) {
exit(EXIT_FAILURE);
}
char word[50];
name >> word;
while (name.good()) {
cout << word << " ";
name >> word;
}
//if (!name.is_open()) {
//while (! filename).eof())
//{
//getline(name, getcontent)
//cout << getcontent << endl;
//}
//exit(EXIT_FAILURE); //comes from cstdlib
//}
//}
system("pause");
return 0;
}
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
string filename;
string getcontent;
ifstream name;
cin >> filename;
filename.append(".txt"); // add extension.
name.open(filename);
if (!name.is_open()) {
exit(EXIT_FAILURE);
}
while (true)
{
getline(name, getcontent);
if (name.eof()) break;
cout << getcontent << endl;
}
return 0;
}
I found this and it helped me with a somewhat different problem and I also thought that I might be able to help. This is coded in windows. (I'm a beginner so forgive me if I made some obvious mistakes)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin;
int main()
{
//char filename[50],word[50];
string filename,word;
//cin.getline(filename,50);
getline(cin,filename);
//strcat(filename,".txt");
filename.append(".txt");
fin.open(filename);
if(fin.is_open())
while(fin>>word)
cout<<word<<endl;
else
cout<<"No such file"<<endl;
return 0;
}
I've been trying to make a console application in Visual Studio 2015 which will read a text file to a string and then output the string, but I'm having some problems.
The first thing I tried was following the cplusplus.com tutorial:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
ifstream myfile("test.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
The program didn't open the file.
Despite multiple internet searches and trying over 20 different methods, I still haven't been able to get my program to work. The best result I was able to achieve was a row of meaningless 0s.
Where am I going wrong?
What you are doing wrong is not emitting a useful error message. Rather than printing "Unable to open file", let the computer tell you why it couldn't open the file. For example:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char **argv)
{
int rv = 0;
string line;
const char * path = argc > 1 ? argv[1] : "test.txt";
ifstream myfile(path);
if( myfile.is_open() ){
myfile.close();
} else {
perror(path);
rv = 1;
}
return rv;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile("hey.txt");
myfile >> line;
cout << line;
system("pause");
return 0;
}
Why does this not print out what is in my "hey.txt" file?
This should do the job, If you are new to these things please read http://www.cplusplus.com/doc/tutorial/files/
EDIT: in article above .good() is a bad practice, look here if you need to more detail Testing stream.good() or !stream.eof() reads last line twice
// reading a text file
#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)) {
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}