Loop function ifstream files

Hi there, I have a function that passes reference of infiles and outfiles from fstream and will run again if user answers yes. However, the second time through the loop I get an error from the infile.fail error that I have created in the function when trying to open the same infile. Why is this? I tried closing the infile before re-entering the loop but that has no effect. I tried placing the infile.close in many different places but I still get the file error message. Any ideas? It has to be something simple. Any help would be greatly appreciated. :-)
Last edited on
Could you post some code please?
The function is way to long. Besides, I do not want to give out my hard worked code to the world. I take it you have no experience with functions (pass by reference), fstreams, loops, and closing files within loops? If so what are your suspicions or problems that you have experienced to this related problem I'm having?
Last edited on
kk granted sorry i just trying to help to get my mind off my own code issues.

on to solution. oh wait i cant see the code so i have no reference to what is going on.
why would you want to input a file twice? input the entire file then move on. there should be no reason for a double input from file. maybe double output after something has been changed. post code maybe more help because i feel like i have no idea what i am talking about without any code to look at.
Last edited on
I DO have some experience in that and I would appreciate it if you didn't make assumptions about me. It's not a very difficult concept to learn...We really can't help you very much without seeing some of your code. For all we know it could just be some bug in the code itself or it could be the way you are moving things around but we can't know without seeing something.
Seriously. Give a link to pastebin or something, but we need to see that code, or at least the parts you're having problems with, and unless your code is completely groundbreaking, we probably won't steal it.

It's either that, or no support. Sorry.

@amaac:
Not true, and I can prove it upon request. Relax. No trolling.

-Albatross
I'm not making any assumptions dudester, just testing your knowledge of passing reference to functions with no return value.

Sorry about that, I did not mean any feelings to be hurt, I apologize. :-)

Albatross, yes the code does not have to be ground braking, but I'd rather not post it all because other people or students working on the same project will grab the code, hence their homework will be done for them! Does that make sense to you?

I have gotten support from others here that understand what I'm talking about, without posting much code, you don't have to always see code to explain the rules in functions, when passing by reference with no void return value.

What are the simple rules, effects, and options do you have for: return value, return, break, or exit for passing reference to parameters in functions?
If you don't want to post the code you can PM it to someone. I'm willing to take a look at it if you want.
What the hell? That's some audacious inference right there. How about "I take it you don't actually want any help and just felt like randomly insulting people"?

I have gotten support from others here that understand what I'm talking about, without posting much code
Each question is unique. You have gotten lucky earlier and asked about a very common issue. In this case, there's a myriad of things that could possibly go wrong. Does the file still exist? Was an error flag set before closing the file? Is the std::fstream being constructed each time through the loop or is it constructed only once outside the loop? Is there something else causing the problem, completely unrelated to the file?
@CDS:

Don't worry. They can't just copy your code. The teacher will know something's wrong.

I also accept PMs.

Here's what I think about returns:
http://cplusplus.com/doc/tutorial/functions/

And here's what I think about breaks:
http://en.wikipedia.org/wiki/Recess_(break)
http://cplusplus.com/doc/tutorial/control/

And here's what I think about passing references to functions:
http://cplusplus.com/doc/tutorial/functions2/

-Albatross
Last edited on
Thanks m4ster roshi, I almost got it.

When using a void function you cannot return a value. So if you have a switch control structure in a void function, and you have a while loop looping the function, using exit instead of break will cause you to exit the program completely. Using return instead of break will return you to main. You cannot return a value because your using a void function. So using return in one of the cases of a switch control structure that has inFile.fail will allow you to return to main without exiting the program, and the while loop will allow you to repeat. Do the files have to be closed before re-entering the loop?
exit() will always terminate the program, regardless of where it's called.
return doesn't return to main(), it returns to the caller function, which may or may not be main().
Hey thanks, I understand that part, but you forgot to mention about closing the file before re-entering the loop?
Last edited on
There's no answer for that. It depends on what you're doing.
I gather that your code looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <fstream>
using namespace std;

void function(ifstream & fin, ofstream & fout);

int main()
{
    ifstream fin("input_file.txt");
    ofstream fout("output_file.txt");

    bool do_it_again=false;

    while (true)
    {
        function(fin,fout);

        //do it again?
        //...

        if (!do_it_again) break;
    }

    return 0;
}

void function(ifstream & fin, ofstream & fout)
{
    bool done=false;
    bool error=false;

    while(true)
    {
        //...
        //work with ifstream/ofstream objects
        //...

        if (done||error) break;
    }

    return; //to main
}

Question: You work with the same files or do you change files with every call of your function?
Hey thanks for help everyone! I got it, I had to replace all of the breaks with returns in the function. I was also missing a bracket on one of the If statements for file error message. Thanks again, and thanks for the links! This is a great forum for C++ programmers! I look forward to more C++... I can't get enough! :-)
Last edited on
Topic archived. No new replies allowed.