简体   繁体   中英

C++ read from file

when I try to debug this code to read from a file and display it, the console screen comes and goes quickly and I don't understand why it's doing this. Can anyone help me please?

#include "Questions.h"
#include <iostream>
using namespace std;

const int MAXITEMS = 10;

struct quiz
{
    string question;
    string anser;
};


int main ()
{
  string str;
  ifstream ifs("Questions2.txt.txt");
  getline (ifs,str);
  cout << "first line of the file is " << str << ".\n";
  return 1;
}

You should click some breakpoints in VS window.Then when you press F5 ,it will pause at breakpoint, then it will run continue until you press F5 again. Or,if you make sure your code is correct.You can press Ctrl + F5 .This means "Run Without Debug". This situation,your program will run to end and suggest you "Press any key to continue". Sorry for my bad english. Hope you can understand.

You can try including a pause function. This way it will display your data and then wait for a response. I've included the function I typically use.

void myPause()
{
      cout << "    Press enter to continue... ";
      char blank[8];
      cin.getline(blank,8);
      cin.sync();
}

Unless you run with some breakpoints, Visual Studio will close the window after the program terminates.

If you want the window to stay on the screen, use Debug->Start without debugging

Or, add a breakpoint at return 1;

Press F10 instead of F5 . By pressing F10 , you can go line by line

try with ifs.open, and then assure yourself by using ifs.is_open () function with an if and an error code, I always use it and it worth

and of course, use a breakpoint before the return (clicking it or using system ("pause")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM