i want to read a full line of strings from a file in order to do some operations. With the bellow code the "Ciphertext" is empty.
my code :
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
ifstream myfile("C:\\encr_affine.txt");
string Ciphertext;
while (!myfile.eof())
{
getline(myfile, Ciphertext);
}
//some code here
myfile.close();
}
The answer for reading a line of strings in a file was :
while (getline(myfile, Ciphertext))
{
//reading the ciphertext from the file
}
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;
}
This is my code.
I am supposed to count the number of 'duck' in a txt file and print string like "There were 2 ducks in animals01.txt"
Now I get no error and nothing return.
Please tell me what's wrong?
#include <iostream> // for printf()
#include <cstdlib> // for exit(), perror()
#include <fstream> // for ifstream
using namespace std;
int main(int argc, char *argv[])
{
if (argc!=2) {
// if argc is not 2, print an error message and exit
cerr << "Usage: "<< argv[0] << " inputFile" << endl;
exit(1); // defined in cstdlib
}
return 0;
int num = 0;
ifstream ifs;
ifs.open(argv[1]);
string line;
do{
getline(ifs, line);
cout<<line<<endl;
if(line == "duck"){num++;}
}while(!ifs.eof());
cout<<"There were"<<num<<"ducks in"<<argv[1]<< endl;
}
You have the line return 0; before you actually did anything, and when you return main() the program is terminated.
By the way, don't use while(!ifs.eof()) because the eof flag only gets set at the first attempt to read past the end of the file, not when you read exactly to the end of the file due to a line break at the end. Do something like this. Also, fix your indenting as it is very misleading.
Read the file word by word.
Example:
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string str, strToSearch = "ducks";
int wordCount = 0;
ifstream fin("thisfile.txt");
while (fin >> str) // Will read up to eof() and stop at every whitespace it hits. (like spaces!)
{
if(str==strToSearch)
wordCount++;
}
fin.close();
cout<<"There were"<<wordCount<<"ducks in thisfile.txt"<<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 think i have a beginners question.
I try to read a file line by line.
The file is in /home/myhomedir and called text.txt .
The content of the file is
1
2
3
4
The file has access right for everyone to read and write.
I wanted: open the file and read it one line after another.
So I tried:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
try
{
ifstream myfile ;
myfile.open("/home/myhomedir/text.txt", ios::in);
if(myfile.is_open())
{
string line;
while (getline(myfile, line)) {
// do nothing, just get a line
// *
}
}
}
catch(int ex)
{
}
return 0;
}
The place marked with * is reached (used the debug feature of netbeans). however line is empty and loop seemed to be entered only once.
Like if an empty file is opened.
What am I doing wrong?
The reason is, you don't know how to use debugger. I've modified your code:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
try
{
ifstream myfile ;
myfile.open("/home/enedil/text.txt", ios::in);
if(myfile.is_open())
{
string line;
while (getline(myfile, line)) {
cout << line; // there's my modification
}
}
}
catch(int ex)
{
}
return 0;
}
The output is
1234
So everything's correct.
If the content of the file is literally
1 2 3 4
then it has only one line and it is to be expected that "getline" returns false the second time it's called.
I have the following code to read in from a file
#include <queue>
#include <iostream>
#include <fstream>
#include <string>
main(int argc,char * argv[])
{
ifstream myFile(argv[1]);
queue<String> myQueue;
if(myFile.is_open())
{
while(...
///my read here
}
}
I have input file like this
1234 345
A 2 234
B 2 345
C 3 345
I want to do the equivalent of this in the read loop
myQueue.push("1234");
myQueue.push("345");
myQueue.push("A");
myQueue.push("2");
myQueue.push("234");
myQueue.push("B");
...
Whats the best way to do this?
Thanks!
string input;
myFile >> input;
myQueue.push(input);
Untested but I believe it works.
By the way, if you want to parse the whole file:
while(myFile>>input)
Thanks to rubenvb for reminding me
#include <queue>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc,char * argv[])
{
if (argc < 2)
{
return -1;
}
ifstream myFile(argv[1]);
queue<string> myQueue;
string input;
while(myFile >> input)
{
myQueue.push(input);
}
}