File I/O problem

Pages: 12
Jul 18, 2012 at 3:30pm
Ok here I am back again.
I try to create a .txt file using ifstream and ofstream.

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
#pragma once
#include <fstream>
#include <iostream>

void createProfile(int selectedProfile) 
{
	char chapter[1];
	if (selectedProfile == 0) 
	{
		std::ifstream openProfile ("Player1.txt");
		if ( !openProfile.is_open()) 
		{
			std::ofstream profile ("Player1.txt");
			profile << "0";
			profile.close();
		}
		openProfile >> chapter;
		std::cout << chapter << std::endl;
		std::cin.get();
		openProfile.close();
	}
	else if (selectedProfile == 1)
	{
		std::ifstream openProfile ("Player2.txt");
		if ( !openProfile.is_open()) 
		{
			std::ofstream profile ("Player2.txt");
			profile << "0";
			profile.close();
		}
		openProfile >> chapter;
		std::cout << chapter << std::endl;
		std::cin.get();
		openProfile.close();
	}
	else if (selectedProfile == 2)
	{
		std::ifstream openProfile ("Player3.txt");
		if ( !openProfile.is_open()) 
		{
			std::ofstream profile ("Player3.txt");
			profile << "0";
			profile.close();
		}
		openProfile >> chapter;
		std::cout << chapter << std::endl;
		std::cin.get();
		openProfile.close();
	}
}



The Compiler compiles and runs it without and error, but he is not creating any .txt file nor opening it..

I realy don't have a clue why..
Last edited on Jul 18, 2012 at 3:30pm
Jul 18, 2012 at 3:39pm
Run the executable by double clicking on it, not from inside IDE, the working directories are usually different. Or use GetModuleFileName() to get full path to .exe (I see you are using windows).
Jul 18, 2012 at 3:56pm
Still no .txt file.
Tryied running the .exe file by double clicking.
Running as Admin.
Compiling as Release and then doing to same..
Everything works but there are no .txt files created.
Also, std::cout << chapter << std::endl; does not print out the 0 I put into the file earlier. Also, it seems that it never even reaches the std::cin.get(); because it never asks for any Input.


( I use VisualStudio 10 )
Jul 18, 2012 at 4:14pm
The code after "if(!openProfile.is_open())" that references openProfile will only be valid if the file existed/was opened ok. Perhaps create a function to verify the file before you try to process it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool hasProfile(const char *fn)
{
  ifstream in(fn);
  if(!in.good())
  {
    ofstream create(fn);
    if(!create.good())
    {
      return false;
    }
  }
  return true;
}

if(hasProfile("Player1.txt"))
{
  // go ahead and process file
} else {
  // no file, can't access, etc, process error
}
Last edited on Jul 18, 2012 at 4:18pm
Jul 18, 2012 at 4:16pm
Which directory are you looking for it in?
Jul 18, 2012 at 4:31pm
@Texan40:
But isn't it like this:

1
2
3
4
5
6
7
std::ifstream openProfile ("Player1.txt");
		if ( !openProfile.is_open()) 
		{
			std::ofstream profile ("Player1.txt");
			profile << "0";
			profile.close();
		}


wouldn't "ifstream openProfile" open the file if its available?
Since it wont be available on the first call, the If would return a true ( because of the ! )
and then ofstream would create a Player1.txt.
On the second call of the function ifstream would open the file because it was generated.

Or do I understand this completely wrong?

@ResidentBiscuit:

In the Visual Studio Directory
C:\Users\[UserName]\Documents\visual studio 2010\Projects\[SolutionName]\Debug
Jul 18, 2012 at 4:35pm
I don't remember where VS saves these things, but I remember having an issue like this before, and it was just in a spot that I found weird.

Have you tried giving it an absolute path?

On a side note, could just make a std::fstream object with ios::in and ios::out flags set, instead of creating two streams on the same object. That can lead to problems.
Jul 18, 2012 at 4:39pm
I didn't even knew of those options.
I'm just like a week into C++ and just finished the I/O File tutorial.
So to challenge myself I wanted to write a program that creates save files.
The Tutorial only contained addition stuff like ios::trunc, ios::app and ios::ate...


@Edit: So how can I give it a "absolute path"?
Last edited on Jul 18, 2012 at 4:40pm
Jul 18, 2012 at 5:02pm
Absolute path is just the actual path in your computer, usually started from the C drive if you're on windows. So something like C://documents//myfolder//test.txt
Jul 18, 2012 at 5:14pm
^^ If your on windows, you don't need the double slash: C:/documents/myfolder/test.txt.

Here is an example open:
std::ifstream input("test.txt");

When you run the .exe, this has your program looking in whatever folder the exe is in.

If you're running via VS "compile and run" (F5), then the program is looking in the:
VS\Projects\MyProject\MyProject folder. This is the same folder that files go when you use the "add new item" command (You know, to add "main.cpp" to your program).
Last edited on Jul 18, 2012 at 5:14pm
Jul 18, 2012 at 5:17pm
wrote in the full path to the executable.. still no .txt showing up...
I seriously don't understand it..
There is no error in the Compiler but still no Text file is being generated...
Is it possible that the Computer deletes the .txt File when the Function is over?
But that would make no sense, right?
Jul 18, 2012 at 5:27pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (selectedProfile == 0) 
{
	std::ifstream openProfile ("Player1.txt"); // fine if file exists
	if ( !openProfile.is_open())  // file doesn't exist
	{
		std::ofstream profile ("Player1.txt"); // create file
		profile << "0"; // put zero in file
		profile.close(); // close file
	}
	openProfile >> chapter; // this call will work if file already exists.  openProfile would need to be re-opened if created above for this call to be valid
	std::cout << chapter << std::endl;
	std::cin.get();
	openProfile.close();
}
Jul 18, 2012 at 5:37pm
@Texan40.
Yes, so when I call this function a second time, it should open up the file, but the problem is, it does not.
Jul 18, 2012 at 5:41pm
Ah, I didn't catch that part, sorry. Perhaps add a check to make sure the file was actually created?:
1
2
3
4
5
6
7
8
9
10
11
if ( !openProfile.is_open())
{
	std::ofstream profile ("Player1.txt");
        if(profile.good())
        {
	  profile << "0";
	  profile.close();
        } else {
          // tell user file creation failed?
        }
}
Jul 18, 2012 at 5:42pm
I would say it's too complicated, does this work?
1
2
3
4
5
6
7
#include <fstream>
int main(void)
{
  std::ofstream output("text.txt");
  output.close();
  return 0;
}


If no, then there is a problem. Otherwise something is wrong with your code.

Keep building from pieces that work until you have the performance you want.
Last edited on Jul 18, 2012 at 5:44pm
Jul 18, 2012 at 5:47pm
I did add a piece to it to check if he even enters the if statement. (by force)
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
char chapter[1],answer;
	if (selectedProfile == 0) 
	{
		std::ifstream openProfile ("layer1.txt"); // changed it to a file that never exists.
		if ( !openProfile.is_open())  // should return true and enter If statement
		{
                        system("cls");
			std::cout << " No Profile stored in this Slot " << std::endl << " Create a Profile? " << std::endl;  
                        // this is never printed on the screen so he never reaches this point.
			answer = std::cin.get();
			if (answer == 'y' || answer == 'Y') 
			{
				std::ofstream profile ("Player1.txt");
				profile << "0";
				profile.close();
			}
			else if (answer == 'n' || answer == 'N') 
			{
				return;
			}
		}
		openProfile >> chapter;
		std::cout << chapter << std::endl;
		std::cin.get();
		openProfile.close();
	}




@LowestOne
Yes your Code does create a text.txt file.
But I now don't know ( as stated above) why he never enters the If-statement and actualy creates the file.
Last edited on Jul 18, 2012 at 5:52pm
Jul 18, 2012 at 6:27pm
Try removing that space between openProfile and ("layer1.txt"). Are you sure that the value of selectedProfile is correct?
Jul 18, 2012 at 6:38pm
Okey I fixed it now
Theres been a problem in the previous code which progressed over to this one.
But still I at least learned quite a bit from this thread.
Thanks to you guys for all of your effort!


Edit:
*sigh*
Now I get
Run-Time Check Failure #2 - Stack around the variable 'chapter' was corrupted.
Last edited on Jul 18, 2012 at 6:47pm
Jul 18, 2012 at 7:00pm
char chapter[1]
Last edited on Jul 18, 2012 at 7:00pm
Jul 18, 2012 at 7:04pm
Thanks again..
But why without a array?
When I write more into the file wouldn't I need a Array anyway?
Pages: 12