0

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

}
JHk1821
  • 184
  • 1
  • 4
  • 16
  • 1
    Please trim out any useless blank lines in your question as they make this short bit of code scroll for no reason and it makes it harder to read. – tadman Apr 21 '18 at 18:54
  • 3
    [Why `while (!myfile.eof())` is wrong](https://stackoverflow.com/q/5605125/9254539). – BessieTheCookie Apr 21 '18 at 18:55
  • 1
    http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Biffen Apr 21 '18 at 18:56
  • @FeiXiang Thank you :) if u want write it as an answer... – JHk1821 Apr 21 '18 at 18:59
  • 1
    I wasn't sure that this is the only problem in your code. Debugging questions like these are generally not received well anyway since you could have figured out the problem lies in the loop statement with a debugger, and then maybe look that up and find the post I linked to. (I didn't downvote though) – BessieTheCookie Apr 21 '18 at 19:02

1 Answers1

0

The answer for reading a line of strings in a file was :

while (getline(myfile, Ciphertext))
{
            //reading the ciphertext from the file
}
JHk1821
  • 184
  • 1
  • 4
  • 16