All I need to do is a simple read from a file in the same directory, but for some reason it refuses to work.
It works perfectly fine in this quick test one I made after I had problems, and outputs the number of entries in the text file.
#include <iostream>
using std::cout;
using std::cin;
#include <cstdio>
int main()
{
int a;
int b = 0;
freopen ("7.txt", "r", stdin);
while (cin >> a)
++b;
cin.clear();
fclose (stdin);
freopen ("7.txt", "r", stdin);
cout << b << '\n';
fclose (stdin);
}
EDIT: Wow I'm sorry to everyone who tried wrapping their heads around this. It was pretty late when I posted this, but I thought I finished. Apparently not. Now upon reopening my file to post the code in it, I realize that I moved everything into a f开发者_StackOverflow社区older before, but apparently when I tried to run the actual thing, it saved back outside of the folder, so it couldn't open "7.txt".
Problem solved I guess, sad waste of space seeing as how it wasn't even complete O_o, sorry.
EDIT2: Okay now I'm confused. I had a temp account on this computer, but when I logged into this account to ask a different question, this one as I meant to post it the other night showed up. I wasn't even on this computer while asking it. Not sure why it wasn't posted like that if it was all ready to be though.
My best "guess" is that you are trying to re-read the same file. If this is the case then you could try this :
std::ifstream file("7.txt");
std::string line = "";
while(std::getline(file, line))
{
//do something
}
//reset file pointer
file.clear();
file.seekg (0, std::ios::beg);
//re-read file
while(std::getline(file, line))
{
//do something else
}
Please try and formulate better questions in the future.
I have no idea what you're trying to do, but any interaction between freopen
, fclose
and cin
is implementation defined at best (and most likely undefined behavior).
精彩评论