C++ ifstream not working

Pages: 12
closed account (GbX36Up4)
I did both and still not opening. ):
Wow.

I'm out of options, as I went through basic stuff.

Something might be wrong with your code, or maybe

the "inFile.open" might not work with the switch statement.

If this doesn't work, something is wrong with your system or you're mistaking something on your system. This is a basic, minimal working example.

1
2
3
4
5
6
7
8
9
10
11
#include <fstream>
#include <iostream>
int main()
{
  std::fstream in("one.txt"); // note spelling of file, all lower case
  if (in.is_open())
   { std::cout << "Yes";}
  else
    { std::cout << "still no";}
  return 0;
}

closed account (GbX36Up4)
I used that code and made sure the program and fiel were in the dame folder and it still did not work. ): I am running Windows 7 and using Code::Blcoks if that helps at all.
Last edited on
When you say did not work, what is the error message ? Moschops code is so short should be easy to find out why.
Moschops code works perfectly.

I'm running MicroSoft Visual C++ 2010 Express.

Change Moschops's to use an ifstream, so you only need permission to 'read' the file (make sure tha you've got permission to 'read' the file)
Execute from a terminal, so you know the path environment.
closed account (GbX36Up4)
When I try to run it I get the message: "still no"
Try calling the underlying Win32 functions so you can get the error code.

For example (uncompiled)

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
#include <windows.h>
#include <iostream>
int main()
{
  HANDLE hFile = CreateFile(
    "one.txt", // Windows does not case about case
    GENERIC_READ,
    0, // no sharing
    NULL, // default security
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL ); // no file template

  if(INVALID_HANDLE_VALUE == hFile)
  {
    DWORD errCode = GetLastError(); // see winerror.h for meanings
    std::cout << "File wouldn't open :-(" << std::endl;
    std::cout << "Error code = " << errCode << std::endl;
  }
  else
  {
    std::cout << "File opened OK :-)" << std::endl;
    CloseHandle(hFile);
  }

  return 0;
}
Maybe you should try a different compiler.
closed account (GbX36Up4)
Well. I am now using Visual C++ 2010 and have made new code:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

int gameloop();
void gameload();
void descgetter();
void input();
void output();
void locationgetter();

int location = 10000;
int north, east, south, west;
int x = 100;
int y = 100;

string name;
string desc;


int main(){
	START:
	string option;
	cout << "Would you like to start a new game or load a previous one?" << endl;
	cout << "[N]ew Game" << endl;
	cout << "[L]oad Game" << endl;
	cin >> option;
	if(option == "N" || "n" ) system("cls"); gameloop();
	if(option == "L" || "l" ) system("cls"); gameload();
	goto START;
}

int gameloop(){
	int gameon = 0;
	while(gameon == 0){
		locationgetter();
		descgetter();
		output();
		input();
	}
	return 0;

}

void gameload(){
}

void input(){
	START:
	string input;
	cin >> input;
		if(input == "west" || west == 0) cout << "You can not move that way." << endl; goto START;
		if(input == "north" || north == 0) cout << "You can not move that way." << endl; goto START;
		if(input == "east" || east == 0) cout << "You can not move that way." << endl; goto START;
		if(input == "south" || south == 0) cout << "You can not move that way." << endl; goto START;

		if(input == "west" || west == 1){--x;}
		if(input == "north" || north == 1){++y;}
		if(input == "east" || east == 1) {++x;}
		if(input == "south" || south == 1){--y;}

	
}

void output(){
	cout << desc << endl;
}

void locationgetter(){
	if(x == 100 && y == 100) location = 1;
	if(x == 99 && y == 101) location = 2;
	if(x == 100 && y == 101)location = 3;
}

void descgetter(){
	switch(location){
	case 1: desc = "You are in a large orange room. You can move north, east, south, and west."; west = 1; south = 1; east = 1; north = 1; break;
	case 2: desc = "You are in a large blue room. You can move east."; west = 0; south = 0; east = 1; north = 0;  break;
	case 3: desc = "You are in a large grey room. You can move west and south."; west = 1; south = 1; east = 0; north = 0; break;
	}

}


For this code it seems to not leave the input function. All the if statements inside of room1 are working fine, but when I type in north nothing happens. I can still continue to type in other directions though.
closed account (GbX36Up4)
Nevermind. I got it to work.
Please mark this thread as solved. =-)

Glad your program is working.
Do you know why it works now, but not before??
Last edited on
closed account (GbX36Up4)
I didn't get the ifstream working.
closed account (GbX36Up4)
I ditched this project a while ago but I think it had something to do with:
1
2
3
4
if(input == "west" || west == 0) cout << "You can not move that way." << endl; goto START;
		if(input == "north" || north == 0) cout << "You can not move that way." << endl; goto START;
		if(input == "east" || east == 0) cout << "You can not move that way." << endl; goto START;
		if(input == "south" || south == 0) cout << "You can not move that way." << endl; goto START;


I didn't use braces for them so even if none of the if statement's arguments were met I still went to START.
Topic archived. No new replies allowed.
Pages: 12