# 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. ## 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;
}

2. 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.

3. 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.

4. 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?

5. [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....

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.

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;
}

7. 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.

8. 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:
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************
****************************************

9. Thank you!

As you can see I'm new to programming

10. 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?

11. 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

12. 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");
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>

13. 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

14. Are you sure?