c++ - Program that reads and writes integers to a file -




hello , thank in advance. simple question 1 getting on nerves. want ask integer write file , display every integer. i've learned how either write or display file , i've been successful @ when try both @ time asks me integer , don't display numbers. think may problem related fstream or position of pointer.

here program:

#include "stdafx.h" #include <iostream> #include <stdio.h> #include <fstream>  using std::cout; using std::cin; using std::fstream; using std::endl;  int a; int x;  int main() {     fstream in;      in.open("op.txt", std::ios::app);      cout << "write integer" << endl;     cin >> x;     in << " " << x;      while (in >> a) {         cout << << endl;         cout << in.tellg();     }      in.close();     return 0;  } 

there few things need fixed:

in.open("op.txt",std::ios::in | std::ios::out | std::ios::app); 

here why need std::ios::in , out

the second problem when switching between writing , reading file stated problem position of read pointer

in.seekg(0, std::ios::beg);//before while loop; 

this sets read position 0 program can read file beginning.here





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -