xy was not declared in this scope: compiler error!

This is a discussion on xy was not declared in this scope: compiler error! within the C++ Programming forums, part of the General Programming Boards category; I just realized I never closed the file in my read_file() function. I now added the close-method, but the error ...

  1. #31
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    I just realized I never closed the file in my read_file() function. I now added the close-method, but the error remains.

    In my whole appendto_file()-Method I am doing 2 things:
    - I am opening and showing the content of a usergiven file, and close that ifstream-object named 'appendfile'.
    - I am re-opening the file, using fstream, creating an object named 'appendfiletwo', appending a usergiven
    content to that usergiven file, and close the fstream.

    I just used two different names for the ifstream-object and for the fstream-object, in order do distinguish between them.

  2. #32
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    fopen should set errno on error, so you can use strerror to get an appropriate message, I suppose. Were it me, I would double check things are closed, and that the file name is what I expect.

  3. #33
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    well, I am using the same filename the user typed in before and I saved in a string, named filename.

    So, the filename should be the same.

    Just for the sake of argument here: Am I doing something wrong? I am the kind of guy that wants to see if something works and afterwards I am thinking about security or double-checking, when the user has to deal with it or anything.

  4. #34
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by twilight View Post
    well, I am using the same filename the user typed in before and I saved in a string, named filename.

    So, the filename should be the same.

    Just for the sake of argument here: Am I doing something wrong? I am the kind of guy that wants to see if something works and afterwards I am thinking about security or double-checking, when the user has to deal with it or anything.
    Since it's not working, the answer to that is probably "yes". The exact nature of what you're doing wrong is what we're trying to determine here.

    Easy question: if it's the same file, why are you opening and closing and opening and closing and opening and closing and etc.? Why not just open the file and pass the stream around?

  5. #35
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    hmm... ok. I have thought of that, too. But could not figure out how.

    istream is just for inputing files from harddrive to my program, right? ofstream is just for outputting files from program to harddrive. Whereas fstream should be doing both as well as append, as far as I know?!

    Also, could I open a file and pass it from one function to another? I guess not because my object only has function-scope, when I open it in one function, right?

    So the only thing I could do is to open it once instead of twice in my appendto_file()-function.

    I can't use ofstream for that, I can't use ifstream for that. So I would fstream? What about fopen you mentioned? I am somewhat limitted to what I know from the 2 tutorials so far, fopen was not covered yet. How is the usage, any reference?

  6. #36
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by twilight View Post
    hmm... ok. I have thought of that, too. But could not figure out how.

    istream is just for inputing files from harddrive to my program, right? ofstream is just for outputting files from program to harddrive. Whereas fstream should be doing both as well as append, as far as I know?!

    Also, could I open a file and pass it from one function to another? I guess not because my object only has function-scope, when I open it in one function, right?

    So the only thing I could do is to open it once instead of twice in my appendto_file()-function.

    I can't use ofstream for that, I can't use ifstream for that. So I would fstream? What about fopen you mentioned? I am somewhat limitted to what I know from the 2 tutorials so far, fopen was not covered yet. How is the usage, any reference?
    fopen is C.

    Of course you can pass things from function to function. You wouldn't flinch at:
    Code:
    int my_function(int bob) {
        int joe = bob + 6;
        joe = process(joe);
        return joe;
    }
    would you? Yet you're passing a local variable, namely joe, off to another function.

    ifstreams can only be used for input, and ofstreams can only be used for output. fstreams are generic files that you can do all file-related things with. (In fact, fstream inherits all of ifstream and all of ofstream! Excitement!)

  7. #37
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    well, nope, I would not flinch :-D but what I meant is different, somewhat.

    I mean, sure I can pass objects, variabels and stuff, but when I open a file in one function, at the end the file is closed, isn't it? Thats what I meant, my stream-objects have function-scope and will be "vanished".

    Ok, so within my one function "appendto_file()" I would use fstream instead of ifstream.

    so, something like this??

    Code:
    std::fstream appendfile ( filename.c_str() );
    
    string readtext;  // to print out the file-content
    getline( appendfile, readtext );
    std::cout << readtext;
    
    std::string appendtext; // where the userinput goes into
    std::getline ( std::cin, appendtext );
    appendfile<<appendtext.c_str();
    appendfile.closoe()

  8. #38
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I mean, sure I can pass objects, variabels and stuff, but when I open a file in one function, at the end the file is closed, isn't it? Thats what I meant, my stream-objects have function-scope and will be "vanished".
    Pass the objects by reference so they are not deconstructed (therefore the stream will not be closed).
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #39
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    hmm... I have no clue how to do that.

    Also, I would have to check in every function, if the object is already created or not, if not: create it, if yes, work with it. But right now, I have no idea how ;-)

    That was certainly not covered yet in any tutorial I read. A good feeling though, to leap ahead of tutorials (which I did anyway with this program).

  10. #40
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    What I mean is this:

    Code:
    int main(void)
    {
        fstream f;
        // Set up the fstream
        foo(f);
    }
    
    void foo(fstream& f)
    {
        // Do stuff with f here.  Since f was passed by reference, it will not be deleted when this
        // function ends.
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #41
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Passing by reference means:
    Code:
    void foo(int& x) // Notice the & after int. That says it's a reference.
    {
    }
    
    int x = 0;
    foo(x);
    When you pass by reference, no copy of the object is made (if you don't pass by reference, a local copy is constructed and the object you pass is copied).

    And no, c_str() will not clear your string contents.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #42
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    thanks guys :-) I will try that tomorrow, today I am out of here :-)

    you are great :-)

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21