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

Hello and thank you in advance. This is a very simple question but one that is getting on my nerves. What I want is just to ask for an integer to write to a file and then display every integer. I've learned how to either write to or display from a file and I've been successful at it but when I try to do both at a time it just asks me for the integer and don't display the numbers.
I think it may be a problem related to fstream or to the position of the pointer.
Here is the 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 an integer" << endl;
cin >> x;
in << " " << x;
while (in >> a) {
cout << a << endl;
cout << in.tellg();
}
in.close();
return 0;
}

There are a few things that are need to be fixed:
in.open("op.txt",std::ios::in | std::ios::out | std::ios::app);
here is why you need to do std::ios::in and out
the second problem is when you are switching between writing and reading from the file like you stated the problem is with the position of the read pointer
in.seekg(0, std::ios::beg);//before the while loop;
this sets the read position to 0 so the program can read from the file from the beginning.here

Related

Why does tellp() give -1?

I'm trying to learn about fstream and here is the code that I'm running on VS2019:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main() {
//cout << "lOOKING IN FILE";
string s;
fstream dictionary("C:/Users/source/repos/Test2/Test2/Text.txt");
if (!dictionary) // were there any errors on opening?
exit(-1);
while (dictionary >> s) cout << s << '\n'; // Print all names in file
dictionary.seekp(0, ios::beg); // Go back to beginning of file
cout << dictionary.tellp() << endl;
dictionary >> s;
cout << s; // Print the first name
return 0;
}
The output is:
abc
acb
cab
-1
cab
Why does tellp give -1 and not go to beginning of file?
You need to clear the state of the stream.
Once the state of a stream have changed from good (i.e. when it reaches end-of-file or there's a failure) then you can't operate on the stream again without clearing the state.

Program failing to open file from variable

I know i'm making a stupid mistake somewhere and even though i've been reading old questions i'm unable to catch it. I'm hoping someone would point me in the right direction. As you can probably tell i'm new to C++.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
//local variables
string answer, filePath, wordtest;
fstream openFile;
***stuff removed for space reasons***
cout << "Enter the full file path" << endl;
getline(cin, filePath);
openFile.open(filePath, ios::in); //Open file
while (openFile.peek() != EOF)
{
cin >> wordtest;
cout << wordtest;
//getline(cin, wordtest);
{
//wordCount = wordCount + 1;
}
}
openFile.close();
openFile.clear(std::ios_base::goodbit);
cout << "Loaded file and read " << wordCount << " words";
}
You are neither reading nor writing to openFile.
You need to use operator<< or operator>> with openFile.

C++ File I/O--can't read/write simultaneously?

I'm writing some simple code that's supposed to read every other character, as well as overwriting their adjacent characters with '?'s in a random text file.
eg.
test.txt contains "Hello World";
after running the program, it'd be "H?l?o?W?r?d"
My code below allows me to read every other character from the text file in the console window, but after the program ends and when I open up test.txt, nothing has been changed. Need help to figure out why...
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream data("test.txt", ios::in | ios::out); //here the test.txt can be any random text file
while (!data.eof())
{
if (!data.eof())
{
char ch;
data.get(ch);
cout << "ch is now " << ch << endl;
}
if (!data.eof())
data.put('?');
}
data.close();
return 0;
}
You forgot to consider that you have 2 streams, istream and ostream.
You need to synchronize the location of these 2 streams to achieve what you want. I modified your code a bit to show what I mean.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char ch;
fstream data("test.txt", ios::in | ios::out); //here the test.txt can be any random text file
while (data.get(ch))
{
cout << "ch is now " << ch << endl;
data.seekg(data.tellp()); //set ostream to point to the new location that istream set
data.put('?');
data.seekp(data.tellg()); //set istream to point to the new location that ostream set
}
data.close(); // not required, as it's part of `fstream::~fstream()`
return 0; // not required, as 0 is returned by default
}
You are misusing eof(). Do it like this instead:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream data("test.txt", ios::in | ios::out); //here the test.txt can be any random text file
char ch;
while (data.get(ch))
{
cout << "ch is now " << ch << endl;
data.put('?');
}
data.close();
return 0;
}

Cant get correct output in program

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
fstream file("file.txt");
file << "this is new line" << endl;
file.flush();
string c;
file >> c;
cout << c << endl;
file.close();
}
when i run this output is empty, if i remove line file << "this is new line" << endl; I'm getting correct output, why ?
By writing to the file, you are moving the internal file pointer to the end of it. This means the the next time you read, you will be at the end of the file, and so nothing will be read.
Look at seek() for moving the file pointer.

Is it possible to "prepare" input from cin?

In his answer, specifically in the linked Ideone example, #Nawaz shows how you can change the buffer object of cout to write to something else. This made me think of utilizing that to prepare input from cin, by filling its streambuf:
#include <iostream>
#include <sstream>
using namespace std;
int main(){
streambuf *coutbuf = cout.rdbuf(cin.rdbuf());
cout << "this goes to the input stream" << endl;
string s;
cin >> s;
cout.rdbuf(coutbuf);
cout << "after cour.rdbuf : " << s;
return 0;
}
But this doesn't quite work as expected, or in other words, it fails. :| cin still expects user input, instead of reading from the provided streambuf. Is there a way to make this work?
#include <iostream>
#include <sstream>
int main()
{
std::stringstream s("32 7.4");
std::cin.rdbuf(s.rdbuf());
int i;
double d;
if (std::cin >> i >> d)
std::cout << i << ' ' << d << '\n';
}
Disregard that question, while further investigating it, I made it work. What I did was actually the other way around than planned; I provided cin a streambuf to read from instead of filling its own.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main(){
stringstream ss;
ss << "Here be prepared input for cin";
streambuf* cin_buf = cin.rdbuf(ss.rdbuf());
string s;
while(cin >> s){
cout << s << " ";
}
cin.rdbuf(cin_buf);
}
Though it would still be nice to see if it's possible to provide prepared input without having to change the cin streambuf directly, aka writing to its buffer directly instead of having it read from a different one.