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.
Related
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;
}
I can't read a txt file. I've tried with different pieces of code which should work and with different text files. The problem isn't that I got the wrong name (the file doesn't lack a txt or have an extra txt). Also, adding a second backwards slash \ or replacing it with forwards slash / doesn't fix it.
Here is the code:
// ConsoleApplication74.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main() {
int sum = 0;
int x;
ifstream inFile;
inFile.open("C:\Users\chaim\SkyDrive\Documents\string\text1.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (inFile >> x) {
sum = sum + x;
}
inFile.close();
cout << "Sum = " << sum << endl;
return 0;
}
Thanks!
"C:\Users\chaim\SkyDrive\Documents\string\text1.txt" should be "C:\\Users\\chaim\\SkyDrive\\Documents\\string\\text1.txt". That way you get backslashes at the appropriate places in the file name.
I have a text file of names. I want to read the text file into a stream, display it to the console. When it is done, it will prompt the user to enter their name. It should then add it to the file.
I can get it to do both of these things separately but not together.
Here is my code.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
using namespace System;
int main(array<System::String ^> ^args)
{
fstream myfile;
string line;
string name;
myfile.open("Names.txt",ios::out | ios::in | ios_base::app);
if (myfile.is_open())
{
while( getline(myfile, line) )
{
cout << line << endl;
}
cout << "Enter your name!\n";
getline (cin, name);
myfile << name;
myfile.close();
}
else
{
cout << "file was not opened\n";
}
return 0;
}
If I leave the while loop in there, it writes all the names to the console, but doesn't append the user entered name to the list. If I take out the while loop, I can add a name to the file but then of course I am not getting a list of the names that are already in that file.
My best guess is, I think it might have something to do with the fact that after I loop through the file using getline, The position is at the end of my stream, so when I try to add a name to it, there isn't any room left in the stream?
Your guess is correct.
The last call to getline() (the one that failed) set the error flags on your stream, which will fail any further IO attempts, which is why nothing is actually written in your file.
You can reset the errors flags with clear() after your reading loop :
myfile.clear();
Note:
You should also test for the returned value of your last getline() call.
Just bumped in to this issue and even though there is accepted answer here I think one can use full code that shows how to use canonical C++ file reading loop:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
using namespace System;
int main(array<System::String ^> ^args)
{
fstream myfile;
string line;
string name;
myfile.open("Names.txt",ios::out | ios::in | ios_base::app);
if (myfile.is_open())
{
while( getline(myfile, line) )
cout << line << endl;
if (file_list.eof())
file_list.clear(); //otherwise we can't do any further I/O
else if (file_list.bad()) {
std::cout << "Error occured while reading file";
return 1;
}
cout << "Enter your name!\n";
getline (cin, name);
myfile << name;
myfile.close();
}
else
{
cout << "file was not opened\n";
}
return 0;
}