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;
}
Related
This code only works for printing the first line. What should I do to print only the second or third line?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string str;
string lineFromFile;
ifstream myfile("./file.txt");
while(getline(myfile,lineFromFile)){
str = lineFromFile;
cout << str << endl;
break;}
}
You can count the lines and equate your expected line number with the counter to output your line as in the below example.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int count = 1;
int line_count;
string str;
string lineFromFile;
ifstream myfile("./file.txt");
std::cin >> line_count;
while(getline(myfile,lineFromFile)){
if(line_count == count)
{
str = lineFromFile;
std::cout << str << std::endl;
break;
}
count++;
}
}
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.
I am trying to write and read information from the same file, but I don't know actually how to do that and why it doesn't work.
When I compile the code, the string that I expect to be filled with information from the file, actually doesn't get filled.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string str;
ifstream fin("Asort.txt");
ofstream fout("Asort.txt");
fout << "hello world";
getline(fin, str);
cout << str;
}
The problem is the location of the "cursor" (thus: the marker) after the statement:
fout << "hello world";
Illustration:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string str;
ifstream fin("Asort.txt");
ofstream fout("Asort.txt");
fout << "hello world"; //output to cout this statement(fout.tellp()) to see where the marker is at this point in the stream
fout.seekp(0); //reset the marker in the fout stream to the beginning
getline(fin, str);
cout << str;
}
The cursor is now at the end of the stream. So you have to use:
fout.seekp(0);
to get it to the beginning so that the fin can start reading from the beginning of the stream.
So Lets say this is what the input file contains
12
Hello
45
54
100
Cheese
23
How would I print it out on the screen in that order.
This is what I had but it skips some lines.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int number;
string word;
int loop = 0;
ifstream infile;
infile.open("arraynumbers.txt");
while(infile >> number >> word)
{
if( infile >> number)
{
cout << number << endl;
}
if(infile >> word)
{
cout << word << endl;
}
}
return 0;
}
I suggest using www.cplusplus.com to answer these questions.
However, you are on the right track. Since you are just outputting the contents of the file to stdout, I suggest using readline() and a string. If you need to access the numeric strings as ints, use the atoi() function.
Example:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
ifstream file("arraynumber.txt");
if (file.is_open()) {
while (getline(file, line)) {
cout << line << endl;
}
file.close();
} else cout << "Error opening arraynumber.txt: File not found in current directory\n";
return 0;
#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;
}