File I/O in MingW?

This is a discussion on File I/O in MingW? within the C++ Programming forums, part of the General Programming Boards category; Hello again, everyone. I'm currently developing a ASCII text file reader, to create my game in. So I went and ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    41

    File I/O in MingW?

    Hello again, everyone.

    I'm currently developing a ASCII text file reader, to create my game in.

    So I went and found this tutorial.

    However, when I try to create my own program using either MingW or Dev C++, I get a heap of error messages like:

    Code:
    --------------------Configuration: Map_Scripter - Debug--------------------
    Compiling...
    main.cpp
    main.cpp: In function `int main(int, char**)':
    main.cpp:17: error: no match for 'operator>>' in 'banner >> title'
    C:\MinGWStudio\MinGW\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\include\c++\3.4.2\bits\istream.tcc:87: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>&(*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
    C:\MinGWStudio\MinGW\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\include\c++\3.4.2\bits\istream.tcc:93: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
    C:\MinGWStudio\MinGW\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\include\c++\3.4.2\bits\istream.tcc:102: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
    I dont really understand these errors (not using functions correctly?) and was wondering if anyone know whats happening.

    So heres my source:
    Code:
    //Map scripter -an ASCII text interpruter, supposedly able to create ASCII games, 
    //I should probaly allow it to read binary files, to hard-code Vacuus
    #include <cstdio>
    #include <cstdlib>
    //Required for I/O:
    #include <fstream>
    
    using namespace std;
    //Prototypes
    char title[20][40]={' '}; //Title, to be read from ./data/title.txt
    ifstream banner("data\title.txt"); //Banner input, should be stored into title
    //I need to add the other prototypes for the reader to, this version is meant to just read the banner
    
    	
    int main(int argc, char *argv[])
    {
    	for ( int eY; eY < 20; eY++ )
    	{
            for ( int eX; eX < 40; eX++; )
    		{
    			banner >> title[eY][eX];
    		}
    	}
    	for (int y; y < 20; y++)
    	{
    		for (int x; x < 40; x++)
    		{
    			cout<<title[y][x];
    		}
    	}
    	system("\nPAUSE");
    	return 0;
    }
    Thank you in advance
    Last edited by Nebbuchadnezzar; 07-17-2005 at 02:32 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    title is an array of null-terminated multibyte strings, you have to loop to read input and place it in each of title's elements.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    41
    Ahhh,

    So what I have to do is read each line induvidually, and then read them & store it into the Array?
    I do realize that I coul just use a string instead, but I would like to be able to use a ASCII baner for the title.


    Thank you for your help.
    Last edited by Nebbuchadnezzar; 07-17-2005 at 02:24 AM.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    41
    Meh, doesnt work.

    All I get is a blank screen saying "press any key to continue"
    (I know this is the pause command) doest even have a blank line.

    Anyway, within the text file is the followng:
    Code:
    test
    Te source can be found above

    Does anyone know whats happening?

    Thank you in advance

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    41
    [EDIT]
    I've been mucking around with File I/O for the last few hours and have come accross the following problems:
    1. When I output STR, I get nothing, as if its not outputting it.
    2. When I supposedely store the value in num into num2, num2 to always returns 0.

    Here is the source of that program:
    Code:
    #include <fstream>
    #include <iostream>
    
    
    using namespace std;
    int num=2;
    ofstream fout("sample.txt");
    ifstream fin("sample.txt");
    char str[100];
    int num2;
    
    
    int main()
    {
        	    fout<<"This is a text file containing the number \n"<<num<<"\n";
        	    cout<<num;
        	    cin.get();
    	    fin.getline(str, 100);
    	    fin>> num2;
    	    cout<< str <<"\n"<< num2<< "\n";
        	    fin.close();
        	    fout.close();
        	    system("pause");
        	    return 0;
    	    }
    It shouldnt be my compiler(s) should it? Because I've tried 2.
    So I'm guessing I didnt understand something....

    Thank you in advance

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    modifying your code:
    Code:
    #include <fstream>
    #include <iostream>
    
    int main()
    {
            //try to avoid global variables
            int num=2;
            std::ofstream fout("test.out");
            std::ifstream fin;
            char str[100];
            int num2;
    
            fout<<"This is a text file containing the number \n"<<num<<"\n";
            fout.close();   //you have to release the file
    
            std::cout<<num;
            std::cin.get();
    
            fin.open("test.out");   //you should open it after releasing it
            fin.getline(str, 100);
            fin>>num2;
            std::cout<< str <<"\n"<< num2<< "\n";
            fin.close();
    
            //system("PAUSE");      //don't use this.
            std::cin.get();         //use this instead.
    
            return 0;
    }
    the way I would do it:
    Code:
    #include <fstream>
    #include <iostream>
    
    int main()
    {
            //try to avoid global variables
            int num=2;
            //open for reading and writing, using fstream
            std::fstream file("test.out",std::ios::out|std::ios::in|std::ios::trunc);
            char str[100];
            int num2;
    
            file<<"This is a text file containing the number \n"<<num<<"\n";
    
            std::cout<<num;
            std::cin.get();
    
            file.seekg(0,std::ios::beg);    //start at the beginning of the file
            file.getline(str, 100,'\n');    //this stops at the first newline, or at 100 chars,
                                            //whichever comes first
            file>>num2;
            std::cout<< str <<"\n"<< num2<< "\n";
    
            file.close();
            std::cin.get();
    
            return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    41
    Thank you!

    So I'm guessing the rson my attempts didt work is because I didnt realese the file?

    [EDIT]
    I'm also having problems with the first program, however I dont need your help just yet, as it probaly needs major changing.
    Goodnight, and thank you for your help.
    Last edited by Nebbuchadnezzar; 07-17-2005 at 04:29 AM.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, there were quite a few things wrong in the first one... I fixed it up for you, but take a look at the comments to see what was wrong:
    Code:
    //Map scripter -an ASCII text interpruter, supposedly able to create ASCII games,
    //I should probaly allow it to read binary files, to hard-code Vacuus
    #include <fstream>
    #include<iostream>      //you forgot this
    
    //using namespace std;
    
    int main()      //if you're not using them, don't put them there.
    {
            /*** no need to initalize this, but include an extra char for the newline***/
            char title[20][41]; //Title, to be read from ./data/title.txt
    
            /*** watch your backslashes here - you need escape them.  C:\\foo\\bar ***/
            std::ifstream banner("test.in");
    
            /*** this isn't a prototype.  If anything it's a definition
                 you also forgot to initialize your Loop control variables ***/
            for ( int eY=0; eY < 20; eY++ )
            {
                    for ( int eX=0;eX < 41; eX++ )  //you had a semicolon in here
                    {
                            /*** you're going to need to take in a newline, so you
                                 need to use the get method.                       ***/
                            banner.get(title[eY][eX]);
                    }
            }
            for (int y=0; y < 20; y++)
            {
                    for (int x=0; x < 41; x++)
                    {
                            std::cout<<title[y][x];
                    }
            }
            //system("\nPAUSE"); //like I said, don't use this
            std::cin.get(); //use this
            return 0;
    }
    and the input file:
    Code:
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    ****************************************
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    41
    Thank you!

    As you can see I'm new to programming

  10. #10
    Registered User
    Join Date
    May 2005
    Posts
    41
    Meh, more problems... this time it compiles, but crashes when executed

    Here is whats in title.txt:
    Code:
                  +---------------------------------------------------------------+
                  |                                                    .          |
                  |      .                .                                       |
                  |	              .                    .                          |
                  |	     __  __                                                   |
                  |     /\ \/\ \                                       .          |
                  |     \ \ \ \ \     __  .   ___   __  __  __  __    ____        |
                  |      \ \ \ \ \  /'__`\   /'___\/\ \/\ \/\ \/\ \  / ,__\   .   |
                  |       \ \ \_/ \/\ \_\ \_/\ \__/\ \ \_\ \ \ \_\ \/\__, `\      |
                  |        \ `\___/\ \__/ \_\ \____\\ \____/\ \____/\/\____/      |
                  |         `\/__/  \/__/\/_/\/____/ \/___/  \/___/  \/___/       |
                  |	  .                                                           |
                  |		        .                      .                    . |
                  |                                                               |
                  +---------------------------------------------------------------+
    And heres the progams code:
    Code:
    //Map scripter -an ASCII text interpruter, supposedly able to create ASCII games,
    //I should probaly allow it to read binary files, to hard-code Vacuus
    #include <fstream>
    #include<iostream>      //you forgot this
    
    using namespace std;
    
    int main()
    {
            char title[15][80]; //Title, to be read from ./data/title.txt
    
            /*** watch your backslashes here - you need escape them.  C:\\foo\\bar ***/
            ifstream banner(".\\data\\title.txt");
    
            /*** this isn't a prototype.  If anything it's a definition
                 you also forgot to initialize your Loop control variables ***/
            for ( int eY=0; eY < 20; eY++ )
            {
                    for ( int eX=0;eX < 41; eX++ )
                    {
                            /*** you're going to need to take in a newline, so you
                                 need to use the get method.                       ***/
                            banner.get(title[eY][eX]);
                    }
            }
            for (int y=0; y < 20; y++)
            {
                    for (int x=0; x < 41; x++)
                    {
                            cout<<title[y][x];
                    }
            }
            cin.get(); //use this
            return 0;
    }
    Now the program was working, until I changed the '*'s to ' 's, perhaps its just that the program doesnt like reading spaces, however If so, how could I have a blank space?

    Also, adding banner.ignore(";"); would ignore until a ';' (semi colon) is found, right?

    Thank you in advance
    Last edited by Nebbuchadnezzar; 07-18-2005 at 03:26 AM.

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    first, that doesn't work because your input file is only 80x15 characters, while your program is expecting a file that's at least 41x20 characters. (the problem is in your loops)

    while you could easily fix that by either changing them by hand or (I would recommend) using variables to hold the size of the banner, you may want to reconsider your algorithm... maybe switch from using a fixed size like that to using something more dynamic, like this solution that reads in the entire file:
    Code:
    //Map scripter -an ASCII text interpruter, supposedly able to create ASCII games,
    //I should probaly allow it to read binary files, to hard-code Vacuus
    #include <fstream>
    #include<iostream>      //you forgot this
    
    //using namespace std;
    
    int main()      //if you're not using them, don't put them there.
    {
            char ch;
    
            std::ifstream banner("test.in");
            while(banner.get(ch))   //while you can read in a char
            {
                    std::cout<<ch;          //output the char
            }
            banner.close();
    
            std::cin.get();
            return 0;
    }
    second: banner.ignore(";"); is invalid. first, you need an integer, and anything in quotes is a char*, which can't be cleanly cast to an integer. put it in single quotes and it becomes a char, which can be cast to an int.

    also, you need to specify another int: how many characters it ignores. you can leave out the delim int (the semicolon, in this case), but you can't leave out the number.

    for examlpe, if you want to ignore up to the semi, you can use something like this:
    Code:
    banner.ignore(32000,';');
    that does what you want, as long as you have less than 32,000 characters before the semicolon.

    http://www.cppreference.com/cppio/ignore.html
    Last edited by major_small; 07-18-2005 at 07:23 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Registered User
    Join Date
    May 2005
    Posts
    41
    Again, thank you.
    I realy dont see how I could of missed that.

    Right now I've got more problems, this time I get a heap of error messages when trying to compile the following:
    I realize that this is something simple (as usual), but I dont have the time to figure out what it is.
    Code:
    	ifstream data(".\\data\\data.txt");
    		   	  	if (data.bad()){goto err;}
    		   	  	data.ignore(32000, ';');
    		   	  	data >> nRoom; 
    				data>> upkey;
    				data>> downkey; 
    				data>> leftkey;
    				data>> rightkey;
    		   	  	data.close();
    		   	  	sGame(nRoom);
    				break;
    Here are the errors:
    Code:
    main.cpp:63: error: ambiguous overload for 'operator>>' in 'data >> nRoom'
    C:\MinGWStudio\MinGW\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\include\c++\3.4.2\bits\istream.tcc:87: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>&(*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] <near match>
    C:\MinGWStudio\MinGW\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\include\c++\3.4.2\bits\istream.tcc:93: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] <near match>
    C:\MinGWStudio\MinGW\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\include\c++\3.4.2\bits\istream.tcc:102: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>] <near match>
    C:\MinGWStudio\MinGW\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\include\c++\3.4.2\bits\istream.tcc:417: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>] <near match>
    C:\MinGWStudio\MinGW\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\include\c++\3.4.2\istream:687: note:                 std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*) [with _Traits = std::char_traits<char>] <near match>
    C:\MinGWStudio\MinGW\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\include\c++\3.4.2\istream:692: note:                 std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*) [with _Traits = std::char_traits<char>] <near match>
    Thank you in advance

  13. #13
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Ok this was tedius but the problem lies with reading whitespaces.





    This is for your arrow key problem

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Attached Files Attached Files
    Last edited by treenef; 07-20-2005 at 06:18 AM.

  14. #14
    Registered User
    Join Date
    May 2005
    Posts
    41
    Are you sure?

    I've had no problems reading whitespaces, its just thecode above.

    I'll try it later.

    Thank you.

  15. #15
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    what is nRoom's data type?

    and don't use goto. find a different way.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-31-2004, 12:21 AM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM

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