Thread: Another dumb question about file i/o

  1. #1
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113

    Question Another dumb question about file i/o

    Heres the question and luckily for you it's most likely a easy one, however, i must know, is it possible to use one variable such as

    ifstream myfile;

    to be a different file at different time during the program? such as,


    ifstream myfile;
    string filename = (initial value);
    for(; {
    myfile.open(filename);
    if(test for file open or not etc);
    cin >> (some value);
    filename = (whatever was input)
    myfile.close();
    }

    edit --
    how do you turn off graphic smilies?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I don't see why not as long as you close the file before trying to open another with the same stream variable. Give it a try and see for yourself.
    Under the message box there is an options frame, select the check box that says Disable Smilies in This Post.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    o i c.. man i gotta start paying attention ;) otherwise ill never make a programmer. anyways, here's the story and what i want my program to do.
    I have a program, which is sopposed to be a RPG engine - calm down its a very very simple one. All it does is. basically, what one of those choose your own adventure kind of books does; it reads you a piece of story, then gives you some choices (1 - 9, depending on the file), then you choose one, and it goes on to the next file (depending on your choice.) so basically, the data files are like pages in the book.
    Anyway, i got all the basic code down, using somewhat liek i did above in my first post. however, there is a problem with what i did. so, i didn't want to post a "4 gazillion pages of code program" and ask everyone to fix it for me; long story short, I cut the program down to 57 lines, that still give the same error(i know its still big but sorry :() and id like to ask if anyone has any ideas, cause ive been bug hunting for quite some time now.
    anyway heres the code:

    in game.cpp:
    --snip snip--
    i messed up, go look further down for the proper code
    Last edited by Cobras2; 03-13-2002 at 03:04 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It compiles nicely, but programs that work with files require me to create the files and I'm lazy. What is the problem that you're getting with the above code? Run-time error, just wacky output or something else?

    -Prelude
    My best code is written with the delete key.

  5. #5
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    The program works fine the first time through the loop. it loads up the dat variable, loads the file, and displays its contents; then lets you choose your option. however as soon as it attempts to run through the loop again, just when it gets to opening the new file, it crashes with a page fault in the exe.

    GAME caused an invalid page fault in
    module GAME.EXE at 0167:00403997.

    and of course the rest which is

    Registers:
    EAX=7250794d CS=0167 EIP=00403997 EFLGS=00010246
    EBX=0065fdf0 SS=016f ESP=0065fc40 EBP=0065fcc8
    ECX=00780d00 DS=016f ESI=00000000 FS=2baf
    EDX=00780e8c ES=016f EDI=0065fc54 GS=0000
    Bytes at CS:EIP:
    ff 50 04 84 c0 74 03 89 75 38 39 75 44 75 24 6a
    Stack dump:
    004250b2 0065fcc8 00000008 00420464 00780e30 007804c0 00780e61 00420464 004250b2 0065fdf0 0041dfa3 ffffffff 00403374 004272ac 004011b8 00780e61

    and i have no idea what it means
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The problems that I see are that you don't close the file inside your loop whereas you do try to open the file. You also said that you wanted different files to be opened and the loop doesn't change your currentfile string anywhere. So what is happening is the file is opened, read from, then the program attempts to open the same file again. This causes your page fault since the file is already open. Try moving the close command to the end of the loop and see if the error still shows up.
    Code:
      for(;;) {
        thisdat.open(currentfile.c_str()); // Where do you change this?
        cout << "Opened " << currentfile << endl;
        for(;i<10;i++) {
          if (!(thisdat.eof())) {
          for(;;) {
            ch = thisdat.peek();
            if (ch == left) break;
            thisdat.get(ch);
            cout << ch;
          }
          if (ch == left) {
            thisdat.get();
            thisdat.get(buffer, 300, '>');
            dat[i] = buffer;
            thisdat.get();
            ch = thisdat.peek();
          }
        }
        // File close should be here too
      }
      buffer = "";
      thisdat.close(); // Shouldn't this be in the loop?
    -Prelude
    My best code is written with the delete key.

  7. #7
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    thisdat is opened right at the beggining of the main loop (for (; at around line 15); then it is closed (still within the main loop, but after the section that reads the file and fills the dat array)

    further down, input is accepted from user, and then currentfile is changed to dat[input] i.e. if the user types 1 then currentfile is set to dat[1]

    then the main loop repeats, and thisdat is opened again, but with the new currentfile.

    the output of the program by the way is:


    Opened menu.dat
    Here's the choices:

    1. menu
    Choice: (and here you type 1, press enter, and then it gives an invalid page fault)

  8. #8
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113

    oops..

    I see why you thought the file was closed outside the main loop.. sorry, i messed up the formatting
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  9. #9
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    okay, heres an update. I messed around a bit and discovered that if I attempt to open menu.dat again (after the first time i open it), the game crashes. however, if i open any other file(s), in any prder, back and forth, open them twice, open them eight times in a row, it works. but menu.dat only works once.

    ???
    hmm weird
    here's the current code(43 lines right now and absolutely barebones :);

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	string dat[10], currentfile("menu.dat");
    	char* buffer = "";
    	char ch, input = ' ';
    	int chtoint; //just giving things values
    	ifstream thisdat;
    	
    	for(;;) {
    		thisdat.open(currentfile.c_str());
    		cout << "Opened " << currentfile.c_str() << endl;//for debugging
    		for(;;) {
    			thisdat.get(ch);
    			if (ch == '<') break;
    			cout << ch;
    		}
    		if (ch == '<') {
    			thisdat.get(buffer, 300, '>');
    			dat[1] = buffer;
    		}
    		buffer = "";
    		thisdat.close();
    
    		cout << "\nChoice: ";
    		cin.get(input);
    		cin.get();
    		switch (input) {
    		case '1':
    			chtoint = input - 48;  //0-9 are 48-57 in ascii code, we want them to be 0-9 
    			currentfile = dat[chtoint];
    			break;
    		default:
    			break;
    		}
    	}
    	return 1;
    }
    heres menu.dat:
    Code:
    Here's choices:
    
    1. goto A<a.dat>
    a.dat:
    Code:
    We're in A
    
    1. goto B<b.dat>
    b.dat:
    Code:
    We're in B
    
    1. goto menu<menu.dat>
    when the prog executes, it reads out all of menu.dat (except whats in the angle brackets, which goes into dat[1]);
    i pick choice 1
    it enters a.dat
    i pick choice 1 again
    it enters b.dat
    i pick choice 1 again
    it exits with an invalid page fault
    any ideas on why i cant go back to menu.dat????
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  10. #10
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Well, folks, here we are. I got my code.. originally 110 lines.. down to 23 - yes, you read me right, 23 lines of code, which still gives me the same error as before. heres the code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string currentfile("data/menu.dat");
    	char* buffer = "";
    	ifstream thisdat;
    	
    	for(;;) {
    		thisdat.open(currentfile.c_str());
    		thisdat.get(buffer, 5000, '<');
    		cout << buffer;
    		thisdat.get();
    		thisdat.get(buffer, 300, '>');
    		currentfile = buffer;
    		thisdat.close();
    		cin.get(); 
    	}
    	return 1;
    }
    however.. if I change the line;
    char* buffer = "";

    to char* buffer = " ";

    or char* buffer = "menu.dat";

    or even char* buffer = "Hi how are you";

    it works fine. obviously I don't understand something about pointers.. can anyone please tell me why this is??
    BTW the contents of the currently working datafiles follow.

    menu.dat:
    Code:
    Going to a.dat...
    <a.dat>
    a.dat:
    Code:
    This is A.dat; going back to menu.dat...
    <menu.dat>
    And the output when the program is run:
    Code:
    Going to a.dat...(<-here i press enter)
    
    This is a.dat; going back to menu.dat...
    which repeats as much as you want.

    ps heres the datafiles, exe, and 23 lines of code
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Ack! How embarrassing, I can't believe I missed that. You're buffer was initialized as a string literal, you can't assign data to a string literal so when you attempted a get, it resulted in an access violation. I changed
    char *buffer = " ";
    to
    char buffer[5000] = " ";
    and it worked fine.

    Excuse me while I go whack myself with a book for a while for not seeing something so simple :P

    -Prelude
    My best code is written with the delete key.

  12. #12
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    drat.. and here i thought maybe it was something complicated ;p
    oh well i will learn eventually so just to understand..

    why does it not work as a char* with "" assigned, but it does work if I actually assign something to it? (i.e. " " or "hi" or "menu.dat") ?

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but it does work if I actually assign something to it
    I don't see why it would, and it doesn't for me.

    -Prelude
    My best code is written with the delete key.

  14. #14
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    that's weird....
    like I siad I guess I don't understand *something* about the way pointers work..
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  15. #15
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Just a thought.. does "" in your book equal " " ?
    because in the code i had in the zip file, char* buffer was initialised to "" (no whitespace) but if I changed the "" to " " (i.e. one empty space), it worked. It also worked if I changed the buffer to a character array, like you did.

    You said I was creating a string literal. if I initialise the buffer with an actual value, am I still creating a string literal? or does it work differently?

    Unfortunately I still don't get it, but I want to so any explanation you can offer would be appreciated.. as is all the help you've given me so far, Prelude.


    thx again,
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File i/o and ASCII question
    By muzihc in forum C Programming
    Replies: 13
    Last Post: 11-04-2008, 11:46 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM