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
Post a Comment